-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
executable file
·90 lines (73 loc) · 2.07 KB
/
generate.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
#!/usr/bin/env python2.7
import sys, json, os, re
sys.dont_write_bytecode = True
def instantiate(root, node=None, parent_desc=None, answers=[]):
""" Traverses the given JSON schema tree, and generates a JSON object for it """
if node is None:
node = root
try:
desc = node["description"]
except:
pass
try:
for p in node["$ref"].split("/"):
if p == "#":
definition = root
else:
definition = definition[p]
return instantiate(root, definition, desc, answers)
except KeyError:
pass
try:
try:
node["enum"]
type = "enum"
except KeyError:
type = node["type"]
if type == "object":
obj = {}
for p, n in node["properties"].iteritems():
v = instantiate(root, n, parent_desc, answers)
if not v is None and not v == "" and not v == {}:
obj[p] = v
return obj
if type == "boolean" or type == "integer" or type == "number" or type == "string" or type == "enum":
if not parent_desc is None:
desc = "%s / %s" % (parent_desc, desc)
if type == "enum":
info = "constant: %s" % ",".join(node["enum"])
else:
info = type
v = raw_input("Enter '%s' (%s) []: " % (desc, info)).strip()
answers.append(v)
return v
if type == "array":
arr = []
while True:
v = instantiate(root, node["items"], parent_desc, answers)
if not v is None and not v == "" and not v == {}:
arr.append(v)
else:
break
return arr
except KeyError:
pass
return None
if __name__ == '__main__':
with open('dependency.json') as f:
schema = json.load(f)
answers = []
version = instantiate(schema, None, None, answers)
with open("answers.txt", "w") as f:
f.write("\n".join(answers))
print("Stored answers to answers.txt")
name = version["name"]
dirname = "manifests/%s" % "/".join([i for i in re.sub("[^a-zA-Z0-9]*", "", name[:4])])
if not os.path.exists(dirname):
os.makedirs(dirname)
target = "%s/%s.json" % (dirname, name)
if os.path.exists(target):
system.exit("File '%s' already exists!")
with open(target, "w") as f:
f.write(json.dumps(version, indent=2))
print("\nCreated manifest file: %s" % target)