-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqpm.py
executable file
·79 lines (65 loc) · 2.73 KB
/
qpm.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
#!/usr/bin/env python
import gevent.greenlet
import argparse, sys, os, os.path, importlib, json
actions_list = ['init', 'config', 'install', 'archive', 'publish', 'develop', 'run', 'test', 'list']
parser = argparse.ArgumentParser()
parser.add_argument('action', help='action to perform', choices=actions_list)
parser.add_argument('--path', help='root path of quark', nargs='?', default=os.getcwd())
parser.add_argument('-j', '--json', help='show json result output', action='store_true')
parser.add_argument('-n', '--noninteractive', help='Do not ask for user input. Command may fail if options are missing.', action='store_true')
def validate_path(pathname, must_exist=True):
pathname = os.path.expandvars(os.path.expanduser(pathname))
# @TODO make win safe
if not(pathname[0] == '/'):
pathname = os.path.join(os.getcwd(), pathname)
if must_exist and not(os.path.exists(pathname)):
raise IOError('Path doesn\'t exist. (%s)' % pathname)
else:
return pathname
def get_action_class(name):
action_module = importlib.import_module('.' + name, 'qpmlib.actions')
action = action_module.action
return action
def exec_action(parameters):
quark_path = parameters['path']
TheAction = get_action_class(parameters['action'])
if TheAction:
options = parameters
options['quark_path'] = validate_path(quark_path)
options['json'] = True
options['interactive'] = False
action = TheAction(options)
gl = gevent.Greenlet(action.do)
gl.start()
gl.join()
return action.get_result()
if __name__ == '__main__':
cmd_line = sys.argv[1:]
parameters, unknown = parser.parse_known_args(cmd_line)
quark_path = parameters.path
TheAction = get_action_class(parameters.action)
if TheAction:
options = TheAction.cmd_line_to_options(unknown)
options['quark_path'] = validate_path(quark_path)
options['interactive'] = not(parameters.noninteractive)
action = TheAction(options)
gl = gevent.Greenlet(action.do)
gl.start()
gl.join()
result = action.get_result()
if parameters.json:
if result:
print json.dumps(result)
else:
print '{}'
else:
if not(result['completed']):
print '%s was not completed!\nReason: %s' % (action, result['reason'])
for m in result['messages']: print m.message
elif not(result['success']):
print '%s was not successful.\nReason: %s' % (action, result['reason'])
for m in result['messages']: print m.message
else:
for m in result['messages']: print m.message
if result['reason']:
print '%s' % result['reason']