-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
62 lines (51 loc) · 1.77 KB
/
main.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
'''
Created on Jun 25, 2014
Main file from where the different scripts are called.
@author amarfurt
'''
import os, sys, json, importlib
def instantiate(modules, name, *args):
''' Imports the module and instantiates the class '''
mod = importlib.import_module(modules[name]['module'])
cls = getattr(mod, modules[name]['class'])
return cls(*args)
def main(command, product=None):
# base directory is project home (current working directory)
baseDir = os.getcwd()
# get used modules
with open('scripts/modules.json', 'r') as reader:
modules = json.load(reader, 'utf8')
# load specified (otherwise all) modules and instantiate respective classes
instances = []
if product in modules:
instances.append((product, instantiate(modules, product, baseDir)))
else:
# append instances in correct starting order
for productName in sorted(modules, key=lambda name: modules[name]['startOrder']):
instances.append((productName, instantiate(modules, productName, baseDir)))
# possibly execute general operations
args = []
if command == 'setup':
# create logs directory
logDir = os.path.join(baseDir, 'logs')
if not os.path.exists(logDir):
os.mkdir(logDir)
# get product versions
with open('scripts/versions.json', 'r') as reader:
versions = json.load(reader, 'utf8')
args.append(versions)
# execute given method for each instance
for productName, instance in instances:
try:
method = getattr(instance, command)
except AttributeError:
print "[ERROR]: Class '" + instance.__class__.__name__ + "'' has no method '" + command + "'."
continue
else:
curArgs = [arg[productName] for arg in args if productName in arg]
try:
method(*curArgs)
except:
print "[ERROR]: '" + command + "' failed for '" + productName + "'."
continue
main(*sys.argv[1:])