forked from manly-man/moodle-destroyer-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdt.py
executable file
·104 lines (83 loc) · 2.46 KB
/
mdt.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
98
99
100
101
102
103
104
#!/usr/bin/env python3
import glob
import os
import re
import subprocess
import sys
from frontend import commands
from persistence.worktree import NotInWorkTree
def exec_path_to_dict(paths):
"""
Aggregate subcommands and their respective execution path.
:returns dict with subcmd:full_path
"""
mdt_pattern = re.compile('mdt-\S+')
matches = mdt_pattern.findall(" ".join(paths)) # find all like mdt-*
cmd_names = [re.compile('^mdt-').sub('', m) for m in matches] # strip ^mdt-
return dict(zip(cmd_names, paths))
def external_subcmds():
"""
this gets all files that are in $PATH and match mdt-*
:returns dict with subcmd:full_path
"""
exec_paths = os.get_exec_path()
mdt_executable_paths = []
for path in exec_paths:
mdt_executable_paths += glob.glob(path + '/mdt-*')
return exec_path_to_dict(mdt_executable_paths)
def execute_external(sub_command):
"""
execute sub_command
"""
extern = external_subcmds()
argv = sys.argv[1:]
argv[0] = extern[sub_command]
subprocess.run(argv)
def internal_cmd():
return commands.__all__
def check_for_sub_command():
if 1 >= len(sys.argv):
return None
else:
return sys.argv[1]
def print_known_external_commands():
print('additional external commands:')
[print(' ' + cmd) for cmd in sorted(external_subcmds().keys())]
def main():
sub_command = check_for_sub_command()
if sub_command is None:
commands.make_config_parser().print_help()
print_known_external_commands()
raise SystemExit(1)
elif sub_command in internal_cmd():
parser = commands.make_config_parser()
args, unknown = parser.parse_known_args()
if 'func' in args:
kwargs = vars(args)
func = kwargs.pop('func')
func(**kwargs)
else:
call = getattr(commands, sub_command)
call()
elif sub_command in external_subcmds():
execute_external(sub_command)
else:
commands.make_config_parser().print_help()
print_known_external_commands()
raise SystemExit(1)
if __name__ == '__main__':
try:
main()
print('exiting…')
except KeyboardInterrupt:
print('exiting…')
sys.exit(1)
except SystemExit:
raise
except NotInWorkTree as e:
print(e)
raise SystemExit(1)
except Exception as e:
print('onoz…')
print(e)
raise