forked from FMCorz/mdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdk.py
executable file
·97 lines (81 loc) · 3.07 KB
/
mdk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Moodle Development Kit
Copyright (c) 2013 Frédéric Massart - FMCorz.net
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
http://github.com/FMCorz/mdk
"""
import sys
import argparse
import os
import re
import logging
from lib.command import CommandRunner
from lib.commands import getCommand, commandsList
from lib.config import Conf
from lib.tools import process
from version import __version__
C = Conf()
try:
debuglevel = getattr(logging, C.get('debug').upper())
except AttributeError:
debuglevel = logging.WARNING
logging.basicConfig(format='%(message)s', level=debuglevel)
availaliases = [str(x) for x in C.get('aliases').keys()]
choices = sorted(commandsList + availaliases)
parser = argparse.ArgumentParser(description='Moodle Development Kit', add_help=False)
parser.add_argument('-h', '--help', action='store_true', help='show this help message and exit')
parser.add_argument('-l', '--list', action='store_true', help='list the available commands')
parser.add_argument('-v', '--version', action='store_true', help='display the current version')
parser.add_argument('command', metavar='command', nargs='?', help='command to call', choices=choices)
parser.add_argument('args', metavar='arguments', nargs=argparse.REMAINDER, help='arguments of the command')
parsedargs = parser.parse_args()
cmd = parsedargs.command
args = parsedargs.args
# There is no command, what do we do?
if not cmd:
if parsedargs.version:
print 'MDK version %s' % __version__
elif parsedargs.list:
for c in sorted(commandsList):
print '{0:<15} {1}'.format(c, getCommand(c)._description)
else:
parser.print_help()
sys.exit(0)
# Looking up for an alias
alias = C.get('aliases.%s' % cmd)
if alias != None:
if alias.startswith('!'):
cmd = alias[1:]
i = 0
# Replace $1, $2, ... with passed arguments
for arg in args:
i += 1
cmd = cmd.replace('$%d' % i, arg)
# Remove unknown $[0-9]
cmd = re.sub(r'\$[0-9]', '', cmd)
result = process(cmd, stdout=None, stderr=None)
sys.exit(result[0])
else:
cmd = alias.split(' ')[0]
args = alias.split(' ')[1:] + args
cls = getCommand(cmd)
Cmd = cls(C)
Runner = CommandRunner(Cmd)
try:
Runner.run(args, prog='%s %s' % (os.path.basename(sys.argv[0]), cmd))
except Exception as e:
import traceback
info = sys.exc_info()
logging.error('%s: %s', e.__class__.__name__, e)
logging.debug(''.join(traceback.format_tb(info[2])))