-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlauncher.py
118 lines (77 loc) · 2.8 KB
/
launcher.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import argparse
import subprocess
import json
def combineVars(env):
"""Combines the environment variables and ensures there are no duplicates.
Arguments:
env -- Dict, dictionary of variables and values (list) to use.
Return:
Dict, processedVars.
"""
processedVars = {}
for var, paths in env.iteritems():
if var in os.environ:
combineVars = os.environ[var] + ';' + ';'.join(paths)
processedVars[var] = ';'.join(list(set(combineVars.split(';'))))
else:
combineVars = ';'.join(paths)
processedVars[var] = ';'.join(list(set(combineVars.split(';'))))
return processedVars
def getApps():
"""Gets all the available apps from the 'apps' directory.
Return:
Dict, app name as the key and the file name as the value.
"""
appsDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'apps')
apps = {}
for eachFile in os.listdir(appsDir):
if eachFile.endswith('.json'):
apps[eachFile.split('.')[0]] = os.path.join(appsDir, eachFile)
return apps
def getEnvs():
"""Gets all the available environments from the 'envs' directory.
Return:
Dict, env name as the key and the file name as the value.
"""
envsDir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'envs')
envs = {}
for eachFile in os.listdir(envsDir):
if eachFile.endswith('.json'):
envs[eachFile.split('.')[0]] = os.path.join(envsDir, eachFile)
return envs
def launch(app, env):
"""Launches the application with specified environment.
Arguments:
app -- String, name of the application to launch.
env -- String, name of the environment to use.
Return:
True if successful.
"""
appData = None
with open(app) as appFile:
appData = json.load(appFile)
envData = None
with open(env) as envFile:
envData = json.load(envFile)
combinedVars = combineVars(envData)
for k, v in combinedVars.iteritems():
os.environ[k] = v
subprocess.Popen('%s' % (appData['executable']), shell=True)
return True
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Parse arguments for launcher.')
parser.add_argument('app', help="The application you'd like to launch.")
parser.add_argument('env', help="The environment you'd like to launch with.")
args = parser.parse_args()
app = args.app
env = args.env
apps = getApps()
envs = getEnvs()
if app not in apps.keys():
raise IOError("Application '" + app + "' not found!")
if env not in envs.keys():
raise IOError("Environment '" + env + "' not found!")
print "Launching Application: " + app
print "Launching Environment: " + env
launch(apps[app], envs[env])