8000 tools/normalise_mpconfigport.py: Add script to normalise config. · micropython/micropython@048bc42 · GitHub
[go: up one dir, main page]

Skip to content

Commit 048bc42

Browse files
committed
tools/normalise_mpconfigport.py: Add script to normalise config.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 6430cd3 commit 048bc42

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

tools/normalise_mpconfigport.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Normalise an mpconfigport.h file by ordering config options, and removing any
3+
that are the defaults. The standard order and defaults come from py/mpconfig.h.
4+
5+
Run from the root of the repository.
6+
7+
Example usage:
8+
9+
python3 tools/normalise_mpconfigport.py ports/stm32/mpconfigport.h
10+
11+
"""
12+
13+
import collections, re, sys
14+
15+
16+
regexs = (
17+
("define", re.compile(r"#define (MICROPY_[A-Z0-9_]+) +(.+)")),
18+
("comment", re.compile(r"// (Extended modules)")),
19+
("comment_strip", re.compile(r"/\* (.+) \*/")),
20+
)
21+
22+
23+
def match_line(line):
24+
for kind, regex in regexs:
25+
m = regex.match(line)
26+
if m:
27+
return kind, m
28+
return None, None
29+
30+
31+
def parse_config(filename, parse_section_names=False):
32+
config = collections.OrderedDict()
33+
section_num = 0
34+
section_comment = None
35+
with open(filename) as f:
36+
for line in f:
37+
kind, match = match_line(line)
38+
if kind == "define":
39+
if section_comment:
40+
if parse_section_names:
41+
config[f"_section{section_num}"] = section_comment
42+
section_num += 1
43+
section_comment = None
44+
config[match.group(1)] = match.group(2)
45+
elif kind == "comment":
46+
section_comment = match.group(1)
47+
elif kind == "comment_strip":
48+
section_comment = match.group(1).strip()
49+
50+
return config
51+
52+
53+
def main():
54+
defaults = parse_config("py/mpconfig.h", True)
55+
port = parse_config(sys.argv[1])
56+
57+
# Print out the standard config options in order.
58+
for k, v in defaults.items():
59+
if k.startswith("_section"):
60+
print()
61+
print(f"// {v}")
62+
continue
63+
if k in port:
64+
if port[k] != v:
65+
print("#define {:40}{}".format(k, port[k]))
66+
del port[k]
67+
68+
# Print out any remaining config options that are not standard ones.
69+
if port:
70+
print()
71+
for k, v in port.items():
72+
print("#define {:40}{}".format(k, v))
73+
74+
75+
main()

0 commit comments

Comments
 (0)
0