-
Notifications
You must be signed in to change notification settings - Fork 448
/
run_python_script.py
50 lines (41 loc) · 1.56 KB
/
run_python_script.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
import cProfile
import sys
import logging
from cd4ml.logger import cd4ml_logging
script_names = ["pipeline", "register_model", "acceptance"]
def run_python_script(script_name, *args, **kwargs):
logger = logging.getLogger(__name__)
if 'profiler' in kwargs:
use_profiler = True
else:
use_profiler = False
if script_name == 'show':
logger.info("\nAvailable scripts\n----------------")
for s in script_names:
logger.info(s)
exit(0)
if script_name == "pipeline":
from scripts import pipeline as executable_script
elif script_name == "register_model":
from scripts import register_model as executable_script
elif script_name == "acceptance":
from scripts import acceptance as executable_script
else:
message = "Error, script_name ({}) must be one of {}".format(script_name, script_names)
raise ValueError(message)
if profiler:
logger.info("running with profiler")
command = "script.main(*args)"
filename = "%s.prof" % script_name
cProfile.runctx(command, None, locals(), filename=filename)
logger.info("To see profiler result, run:\nsnakeviz %s" % filename)
else:
executable_script.main(list(args[0]))
if __name__ == "__main__":
cd4ml_logging.init()
profiler = ' -p' in ' '.join(sys.argv)
script = sys.argv[1]
arguments = sys.argv[2:]
# remove the profile flag now that profiler is on
arguments = [i for i in arguments if i != '-p']
run_python_script(script, arguments, profiler=profiler)