-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathautogen.py
106 lines (85 loc) · 3.02 KB
/
autogen.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
from os.path import isdir, join, split
from glob import glob
import yaml
# ugly hack to get .travis.yaml closer to manual version
from collections import OrderedDict
class UnsortableList(list):
def sort(self, *args, **kwargs):
pass
class UnsortableOrderedDict(OrderedDict):
def items(self, *args, **kwargs):
return UnsortableList(OrderedDict.items(self, *args, **kwargs))
yaml.add_representer(
UnsortableOrderedDict, yaml.representer.SafeRepresenter.represent_dict
)
def read_option(options, default=0):
for i, opt in enumerate(options):
print("\t\t", i, opt)
opt = None
while opt is None:
try:
sel = int(input("Select option number [default: %s]: " % default))
opt = options[sel]
except IndexError:
print("invalid index!")
except ValueError:
print("selecting default: " + default)
opt = options[default]
return opt
def find_targets(auto=False):
# TODO: should belong to engine
dirs_to_engines_exts = {
"NEURON": {"engine": "NEURON", "extension": ".hoc"},
"NeuroML2": {"engine": "jNeuroML", "extension": ".nml"},
}
targets = []
for d, eng_ext in dirs_to_engines_exts.items():
if isdir(d):
engine = dirs_to_engines_exts[d]["engine"]
ext = dirs_to_engines_exts[d]["extension"]
print("Default directory for {0} engine found.".format(engine))
print(" Will look for scripts with {0} extension".format(ext))
scripts = glob(join(d, "*" + ext))
if scripts:
if auto:
print("selecting default: ", scripts[0])
script = scripts[0]
else:
script = read_option(scripts)
targets.append((engine, script))
return targets
def create_dryrun(engine, target):
print(
" ".join(("Generating dry run test for file", target, ", using engine", engine))
)
dirname, fname = split(target)
omt = {"target": fname, "engine": engine}
with open(target + ".dry.omt", "w") as fh:
yaml.dump(omt, fh, default_flow_style=False)
def generate_dottravis(targets):
engines = [t[0] for t in targets]
engines = ["OMV_ENGINE=" + be for be in engines]
# TODO: softcode
repo = "git+https://github.com/borismarin/osb-model-validation.git"
travis = UnsortableOrderedDict(
[
("language", "python"),
("python", 2.7),
("env", engines),
("install", ["pip install " + repo]),
("script", ["omv all"]),
]
)
with open(".travis.yml", "w") as fh:
fh.write(yaml.dump(travis, default_flow_style=False))
def autogen(auto=False, dry=True):
targets = find_targets(auto)
if targets:
for engine, target in targets:
if dry:
create_dryrun(engine, target)
generate_dottravis(targets)
else:
print("No target scripts found!")
if __name__ == "__main__":
autogen()