Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 7ff00a6

Browse files
JohnVillalovosgrgustaf
authored andcommitted
[build] scripts/analyze: Use a main() function
Previously the main code execution path weaved its way between function definitions. Change it so that we now have a main() function. Also: * Sort the imports alphabetically. * Separate top-level functions by two blank lines per PEP8. Signed-off-by: John L. Villalovos <[email protected]>
1 parent c620fac commit 7ff00a6

File tree

1 file changed

+110
-92
lines changed

1 file changed

+110
-92
lines changed

scripts/analyze

Lines changed: 110 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/usr/bin/env python3
22

3-
import os
4-
import sys
53
import json
4+
import os
5+
import pprint
66
import re
77
import shutil
8-
import pprint
8+
import sys
9+
10+
# Globals
11+
args = {}
12+
board = None
13+
outdir = ""
14+
915

1016
def usage():
1117
print("Usage: ./analyze <options>")
@@ -20,58 +26,114 @@ def usage():
2026
print(" PROFILE= Where to write the JerryScript profile")
2127
print(" JS_OUT= Output file if any JS modules are found in SCRIPT")
2228
print(" O= Output directory")
23-
quit()
24-
25-
args = {}
26-
27-
for i in sys.argv[1:]:
28-
option, value = i.split('=')[:2]
29-
args[option] = value
29+
sys.exit()
30+
31+
32+
def main():
33+
global args, board, outdir
34+
for i in sys.argv[1:]:
35+
option, value = i.split('=')[:2]
36+
args[option] = value
37+
json_tree = {}
38+
deps_list = []
39+
zconf = []
40+
zjs_conf = []
41+
jrs_conf = []
42+
lsrc = []
43+
js_modules = []
44+
board = args['BOARD']
45+
if board == 'linux':
46+
req_options = ['BOARD', 'JSON_DIR']
47+
else:
48+
req_options = ['BOARD', 'JSON_DIR', 'PRJCONF', 'CMAKEFILE']
49+
for i in req_options:
50+
if i not in args:
51+
print("%s must be specified" % i)
52+
usage()
53+
if 'SCRIPT' not in args:
54+
print("Generating modules header")
55+
else:
56+
script = args['SCRIPT']
57+
print("Analyzing %s for %s" % (script, board))
58+
if 'O' not in args:
59+
outdir = 'outdir'
60+
else:
61+
outdir = args['O']
3062

31-
json_tree = {}
63+
# Linux is special cased because it does not use a JS script to build
64+
if board == 'linux':
65+
restrict = args['RESTRICT'].split(',')
66+
restrict = [i.strip() for i in restrict]
67+
build_tree(json_tree, restrict)
68+
do_print(json_tree)
69+
all_list = []
70+
create_dep_list(all_list, board, json_tree)
71+
for i in sorted(all_list):
72+
print("Using module: " + i)
73+
write_modules(all_list, json_tree)
74+
# normal case
75+
else:
76+
if 'JS_OUT' in args:
77+
with open(script, 'r') as s:
78+
js = s.read()
79+
with open(args['JS_OUT'], 'w') as f:
80+
for i in os.listdir("modules/"):
81+
if match_require(js, i):
82+
js_modules += [i]
83+
for i in js_modules:
84+
with open('modules/' + i) as mod:
85+
f.write(mod.read())
86+
f.write("\n")
87+
f.write(js)
88+
js_file = args['JS_OUT']
89+
else:
90+
js_file = script
3291

33-
deps_list = []
34-
zconf = []
35-
zjs_conf = []
36-
jrs_conf = []
37-
lsrc = []
38-
js_modules = []
92+
restrict = args.get('RESTRICT', '')
93+
if restrict != '':
94+
restrict = restrict.split(',')
95+
restrict = [i.strip() for i in restrict]
96+
do_print("Only using modules: " + str(restrict))
97+
else:
98+
restrict = []
3999

40-
board = args['BOARD']
100+
force = args.get('FORCE', '')
101+
if force != '':
102+
force = force.split(',')
103+
do_print("Forcing inclusion of: " + str(force))
104+
else:
105+
force = []
41106

42-
if board == 'linux':
43-
req_options = ['BOARD', 'JSON_DIR']
44-
else:
45-
req_options = ['BOARD', 'JSON_DIR', 'PRJCONF', 'CMAKEFILE']
107+
build_tree(json_tree, restrict)
108+
do_print(json_tree)
109+
parse_json_tree(js_file, deps_list, zconf, zjs_conf, jrs_conf, lsrc,
110+
json_tree, force=force)
111+
for i in sorted(deps_list):
112+
print("Using module: " + i)
46113

47-
for i in req_options:
48-
if i not in args:
49-
print("%s must be specified" % i)
50-
usage()
114+
write_zconf(zconf, args['PRJCONF'])
115+
write_cmakefile(lsrc, zjs_conf, args['CMAKEFILE'])
116+
write_jrsconfig(jrs_conf)
51117

52-
if 'SCRIPT' not in args:
53-
print("Generating modules header")
54-
else:
55-
script = args['SCRIPT']
56-
print("Analyzing %s for %s" % (script, board))
118+
# building for arc does not need zjs_modules_gen.h to be created
119+
if board != 'arc':
120+
write_modules(deps_list, json_tree)
57121

58-
if 'O' not in args:
59-
outdir = 'outdir'
60-
else:
61-
outdir = args['O']
62122

63123
def do_print(s):
64124
"""Debug print controlled with V option"""
65125
if 'V' in args and args['V'] == '1':
66126
p = pprint.PrettyPrinter(indent=1)
67127
p.pprint(s)
68128

129+
69130
def merge_lists(l1, l2):
70131
"""Merge l2 into l1, excluding duplicates"""
71132
for i in l2:
72133
if i not in l1:
73134
l1.append(i)
74135

136+
75137
def parse_list(tree, list):
76138
"""Parse a list of JSON files to create a JSON tree
77139
@@ -100,13 +162,15 @@ def parse_list(tree, list):
100162
else:
101163
tree[data['module']] = data;
102164

165+
103166
def build_tree(tree, restrict):
104167
"""Wrapper for parse_list() if a restricted list of JSON files is used"""
105168
if restrict != []:
106169
parse_list(tree, restrict)
107170
else:
108171
parse_list(tree, os.listdir(args['JSON_DIR']))
109172

173+
110174
def create_dep_list_helper(dlist, dep, board, tree):
111175
"""Recursive helper for create_dep_list()"""
112176
if dep in tree:
@@ -121,6 +185,7 @@ def create_dep_list_helper(dlist, dep, board, tree):
121185
for i in entry['depends']:
122186
create_dep_list_helper(dlist, i, board, tree)
123187

188+
124189
def create_dep_list(dlist, board, tree):
125190
"""Find all dependencies of a list. This is only used by the Linux target
126191
where a JSON list is manually used rather than parsing a script
@@ -134,6 +199,7 @@ def create_dep_list(dlist, board, tree):
134199
merge_lists(dlist, [dep])
135200
create_dep_list_helper(dlist, dep, board, tree)
136201

202+
137203
def match_require(js, module, child=None):
138204
# requires: js is a string chunk of JS code, module is a string module name
139205
# we're searching for a require() for, child is an optional
@@ -144,6 +210,7 @@ def match_require(js, module, child=None):
144210
regex = r"\brequire *\([\'\"]%s[\"\']\)%s" % (module, child_regex)
145211
return re.search(regex, js)
146212

213+
147214
def parse_json_tree_helper(js, dep, dlist, zlist, zjslist, jrslist, srclist,
148215
tree):
149216
"""Recursive helper for parse_json_tree()"""
@@ -219,6 +286,7 @@ def parse_json_tree_helper(js, dep, dlist, zlist, zjslist, jrslist, srclist,
219286
if 'zjs_config' in entry:
220287
merge_lists(zjslist, entry['zjs_config'])
221288

289+
222290
def parse_json_tree(file, dlist, zlist, zjslist, jrslist, srclist, tree,
223291
force=[]):
224292
"""Using a JS file as input, gather dependencies and generate separate lists
@@ -292,6 +360,7 @@ def parse_json_tree(file, dlist, zlist, zjslist, jrslist, srclist, tree,
292360
parse_json_tree_helper(text, default, dlist, zlist, zjslist,
293361
jrslist, srclist, tree)
294362

363+
295364
def write_jrsconfig(list):
296365
"""Write a JerryScript profile file"""
297366
if 'PROFILE' in args:
@@ -304,6 +373,7 @@ def write_jrsconfig(list):
304373
feature = feature.replace(i, "#" + i)
305374
p.write(feature)
306375

376+
307377
def write_cmakefile(src, zconf, file):
308378
"""Write out a src CMake file"""
309379
with open(file, "w") as f:
@@ -320,6 +390,7 @@ def write_cmakefile(src, zconf, file):
320390
f.write("add_definitions(" + i + ")\n")
321391
f.write("\n")
322392

393+
323394
def expand_match(match):
324395
"""Expand regexp match as a shell variable, if possible"""
325396
# requires: match should be VARNAME from $(VARNAME) string
@@ -332,6 +403,7 @@ def expand_match(match):
332403
return '$(' + varname + ')'
333404
return expanded
334405

406+
335407
def write_zconf(list, file):
336408
"""Write Zephyr config options to a file"""
337409
with open(file, "w") as f:
@@ -340,6 +412,7 @@ def write_zconf(list, file):
340412
expanded = re.sub(r"\$\((\w+)\)", expand_match, i)
341413
f.write(expanded + '\n')
342414

415+
343416
def write_modules(list, tree):
344417
"""Write zjs_modules_gen.h file"""
345418
headers = []
@@ -377,61 +450,6 @@ typedef struct gbl_module {
377450
file.write("},\n")
378451
file.write("};\n")
379452

380-
# Linux is special cased because it does not use a JS script to build
381-
if board == 'linux':
382-
restrict = args['RESTRICT'].split(',')
383-
restrict = [i.strip() for i in restrict]
384-
build_tree(json_tree, restrict)
385-
do_print(json_tree)
386-
all_list = []
387-
create_dep_list(all_list, board, json_tree)
388-
for i in sorted(all_list):
389-
print("Using module: " + i)
390-
write_modules(all_list, json_tree)
391-
# normal case
392-
else:
393-
if 'JS_OUT' in args:
394-
with open(script, 'r') as s:
395-
js = s.read()
396-
with open(args['JS_OUT'], 'w') as f:
397-
for i in os.listdir("modules/"):
398-
if match_require(js, i):
399-
js_modules += [i]
400-
for i in js_modules:
401-
with open('modules/' + i) as mod:
402-
f.write(mod.read())
403-
f.write("\n")
404-
f.write(js)
405-
js_file = args['JS_OUT']
406-
else:
407-
js_file = script
408453

409-
restrict = args.get('RESTRICT', '')
410-
if restrict != '':
411-
restrict = restrict.split(',')
412-
restrict = [i.strip() for i in restrict]
413-
do_print("Only using modules: " + str(restrict))
414-
else:
415-
restrict = []
416-
417-
force = args.get('FORCE', '')
418-
if force != '':
419-
force = force.split(',')
420-
do_print("Forcing inclusion of: " + str(force))
421-
else:
422-
force = []
423-
424-
build_tree(json_tree, restrict)
425-
do_print(json_tree)
426-
parse_json_tree(js_file, deps_list, zconf, zjs_conf, jrs_conf, lsrc,
427-
json_tree, force=force)
428-
for i in sorted(deps_list):
429-
print("Using module: " + i)
430-
431-
write_zconf(zconf, args['PRJCONF'])
432-
write_cmakefile(lsrc, zjs_conf, args['CMAKEFILE'])
433-
write_jrsconfig(jrs_conf)
434-
435-
# building for arc does not need zjs_modules_gen.h to be created
436-
if board != 'arc':
437-
write_modules(deps_list, json_tree)
454+
if '__main__' == __name__:
455+
sys.exit(main())

0 commit comments

Comments
 (0)