-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
96 lines (90 loc) · 4.09 KB
/
setup.py
File metadata and controls
96 lines (90 loc) · 4.09 KB
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
import getopt
import json
import os
import sys
def parseOptions():
commandLineArgs = sys.argv[1:]
shortOptions = "ch:"
longOptions = ["conf=", "help"]
try:
args, vals = getopt.getopt(commandLineArgs, shortOptions, longOptions)
except getopt.GetoptError as err:
print(err)
printHelp()
nextArg = 0
configuration = None
for currArg, currVal in args:
nextArg += 1
if currArg in("-c", "--conf"):
if currArg == "-c" and nextArg < len(commandLineArgs):
currVal = commandLineArgs[nextArg]
if currVal == "":
print("No config file provided.")
printHelp()
configuration = currVal
elif currArg in ("-h", "--help"):
printHelp()
elif currArg in ("-p", "--profile"):
configuration["profileMode"] = True
return configuration
def printHelp():
print("Usage:\n\tpython setup.py --conf config.json\n\nOptions:\n\t-c,--conf\tUse specified config file for simulation settings.\n\t-h,--help\tDisplay this message.")
exit(0)
def replaceMakefileSettings(settings, currPython):
if settings["pathToPython"] != currPython:
print("The setting for the Python path is replaced in the Makefile.\nYou will be prompted to allow in-file replacement if you changed this setting.\nDenying the change will cause the given option to be reverted to its previous value.")
replace = input("Replace Python setting in the Makefile to {0} (currently: {1}) [Y/n]: ".format(settings["pathToPython"], currPython))
if replace.upper() == 'Y':
os.system("sed -i 's/PYTHON = {0}/PYTHON = {1}/g' Makefile".format(currPython, settings["pathToPython"]))
else:
settings["pathToPython"] = currPython
def selectSettings(settings, mode=None):
newSettings = {}
settingKey = "top-level"
if mode == "sugarscape":
settingKey = "Sugarscape simulation"
print("You will be prompted with each configurable {0} setting in the software.\nFor each setting, enter the value(s) you would like.\nMulti-value settings should be comma-separated (with no spaces).\nNote: Given value will overwrite current value, not add to it.\nSimply hit enter for no change.".format(settingKey))
i = 1
settingsLen = len(settings) if mode == "sugarscape" else len(settings) - 1
for setting in settings:
if mode != "sugarscape" and setting == "sugarscapeOptions":
continue
value = input("{0}/{1} {2} (currently: {3}): ".format(i, settingsLen, setting, settings[setting]))
if value != "":
if isinstance(settings[setting], list):
valType = None
listValues = list(value.split(','))
if isinstance(value[0], float):
value = list(map(float, listValues))
elif isinstance(value[0], int):
value = list(map(int, listValues))
elif isinstance(value[0], bool):
value = list(map(bool, listValues))
elif isinstance(value[0], str):
value = listValues
elif isinstance(settings[setting], float):
value = float(value)
elif isinstance(settings[setting], int):
value = int(value)
elif isinstance(value[0], bool):
value = list(map(bool, listValues))
else:
value = settings[setting]
newSettings[setting] = value
i += 1
return newSettings
if __name__ == "__main__":
configuration = parseOptions()
if configuration == None:
configuration = "config.json"
configFile = open(configuration)
settings = json.loads(configFile.read())
configFile.close()
makefilePython = settings["pathToPython"]
newSettings = selectSettings(settings)
newSettings["sugarscapeOptions"] = selectSettings(settings["sugarscapeOptions"], "sugarscape")
replaceMakefileSettings(newSettings, makefilePython)
outfile = open("setup.json", 'w')
outfile.write(json.dumps(newSettings, sort_keys=True))
outfile.close()
exit(0)