forked from mushkevych/grazer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_starter.py
81 lines (70 loc) · 2.75 KB
/
process_starter.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
# -*- coding: utf-8 -*-
__author__ = 'Bohdan Mushkevych'
import sys
import types
from synergy.conf.process_context import ProcessContext
def get_class(kls):
"""
:param kls - string of fully identified starter function or starter method path
for instance:
- workers.abstract_worker.AbstractWorker.start
- workers.example_script_worker.main
:return tuple (type, object, starter)
for instance:
- (FunctionType, <function_main>, None)
- (ClassType, <Class_...>, 'start')
"""
parts = kls.split('.')
try:
# First, try to import module hosting starter function
module = ".".join(parts[:-1])
m = __import__(module)
except ImportError:
# If first try fails, try to import module hosting Class with a starter method
module = ".".join(parts[:-2])
m = __import__(module)
t = None
starter = None
for i in range(1, len(parts)):
comp = parts[i]
starter = parts[i:]
m = getattr(m, comp)
if isinstance(m, (type, types.ClassType)):
t = types.ClassType
starter = None if len(parts[i:]) == 1 else ".".join(parts[i + 1:])
break
if isinstance(m, (type, types.FunctionType)):
t = types.FunctionType
starter = None
break
return t, m, starter
def start_by_process_name(process_name, *args):
"""
Function starts the process by:
1. retrieving its fully specified path name
2. if the path name ends with starter method - then creates an instance of the wrapping class and calls <code>starter(*args)</code> method on it
3. if the path name ends with starter function - then retrieves its module and calls <code>starter(*args)</code> function on it
"""
sys.stdout.write('INFO: Starter path %r \n' % ProcessContext.get_classname(process_name))
t, m, starter = get_class(ProcessContext.get_classname(process_name))
if isinstance(m, (type, types.ClassType)):
sys.stdout.write('INFO: Starting process by calling starter method %r \n' % starter)
instance = m(process_name)
method = getattr(instance, starter)
method(*args)
elif isinstance(m, (type, types.FunctionType)):
sys.stdout.write('INFO: Starting module.\n')
function = m
function(*args)
else:
raise ValueError('Improper starter path %r' % ProcessContext.get_classname(process_name))
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.stderr.write('ERROR: no Process Name specified to start \n')
elif len(sys.argv) == 2:
process_name = sys.argv[1]
start_by_process_name(process_name, None)
else:
process_name = sys.argv[1]
args = sys.argv[2:]
start_by_process_name(process_name, args)