-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.py
77 lines (63 loc) · 2.08 KB
/
run.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
# -*- coding: utf-8 -*-
import gevent.monkey; gevent.monkey.patch_all() # noqa
import sys
import os
import click
def import_obj(obj_path, hard=False):
"""
import_obj imports an object by uri, example::
>>> import_obj("module:main")
<function main at x>
:param obj_path: a string represents the object uri.
;param hard: a boolean value indicates whether to raise an exception on
import failures.
"""
try:
# ``__import__`` of Python 2.x could not resolve unicode, so we need
# to ensure the type of ``module`` and ``obj`` is native str.
module, obj = str(obj_path).rsplit(':', 1)
m = __import__(module, globals(), locals(), [obj], 0)
return getattr(m, obj)
except (ValueError, AttributeError, ImportError):
if hard:
raise
@click.command(
context_settings={
"ignore_unknown_options": True,
"allow_extra_args": True
},
add_help_option=True)
@click.argument('script_or_uri', required=True)
@click.pass_context
@click.option('-i', '--force-import', type=bool, default=False, is_flag=True,
help=("force to import as an object, don't try as executing"
" a script"))
def run(ctx, script_or_uri, force_import):
"""
Run a script or uri.
"""
group_name = ctx.parent.command.name + ' ' if ctx.parent else ''
prog_name = "{}{}".format(group_name, ctx.command.name)
sys.argv = [prog_name] + ctx.args
try:
ret = None
entry = import_obj(script_or_uri, hard=force_import)
if entry:
ret = entry()
# Backward compatibility: if ret is int,
# means it's cli return code.
if isinstance(entry, int):
sys.exit(ret)
else:
execfile(script_or_uri, {
'__name__': '__main__',
'__file__': os.path.realpath(script_or_uri),
})
except SystemExit:
raise
except BaseException:
raise
cmds = [run]
if __name__ == '__main__':
# pylint: disable=E1120
run()