Skip to content

Commit fbbfa3a

Browse files
committed
ports/espressif/tools/check-sdkconfig.py: use click
1 parent c64630a commit fbbfa3a

File tree

1 file changed

+56
-34
lines changed

1 file changed

+56
-34
lines changed
Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,60 @@
11
#!/usr/bin/env python3
22
import sys
33

4-
sdk_config = {}
5-
6-
config_h = sys.argv[-1]
7-
with open(config_h) as f:
8-
for row in f:
9-
if row.startswith("#define "):
10-
_, k, v = row.strip().split(None, 2)
11-
# ad-hoc handle lines like '#define CONFIG_TCP_MSL CONFIG_LWIP_TCP_MSL'
4+
import click
5+
6+
7+
def int_or_string(s):
8+
try:
9+
return int(s)
10+
except ValueError:
11+
return s.strip('"')
12+
13+
14+
def collect_definitions(file):
15+
"""Collect all definitions in supplied sdkconfig.h."""
16+
sdk_config = {}
17+
for line in file:
18+
if line.startswith("#define "):
19+
_, k, v = line.strip().split(None, 2)
20+
# Handle transitive definitions like '#define CONFIG_TCP_MSL CONFIG_LWIP_TCP_MSL'
1221
v = sdk_config.get(k, v)
13-
if v[0] == '"':
14-
v = eval(v) # Assume it is a simple C string
15-
16-
# ad-hoc convert to integer
17-
try:
18-
v = int(v)
19-
except ValueError:
20-
pass
21-
sdk_config[k] = v
22-
23-
del sys.argv[-1]
24-
25-
circuitpy_config = {}
26-
for arg in sys.argv[1:]:
27-
k, v = arg.split("=", 1)
28-
circuitpy_config[k] = int(v)
29-
30-
partition_table = sdk_config.get("CONFIG_PARTITION_TABLE_FILENAME")
31-
for var in ("CIRCUITPY_STORAGE_EXTEND", "CIRCUITPY_DUALBANK"):
32-
if circuitpy_config.get(var):
33-
with open(partition_table) as f:
34-
content = f.read()
35-
if not "ota_1" in content:
36-
raise SystemExit(f"{var} is incompatible with {partition_table=} (no ota_1 partition)")
37-
38-
# Add more checks here
22+
sdk_config[k] = int_or_string(v)
23+
return sdk_config
24+
25+
26+
def validate(sdk_config, circuitpy_config):
27+
partition_table = sdk_config.get("CONFIG_PARTITION_TABLE_FILENAME")
28+
for var in ("CIRCUITPY_STORAGE_EXTEND", "CIRCUITPY_DUALBANK"):
29+
if circuitpy_config.get(var):
30+
with open(partition_table) as f:
31+
content = f.read()
32+
if not "ota_1" in content:
33+
raise SystemExit(
34+
f"{var} is incompatible with {partition_table=} (no ota_1 partition)"
35+
)
36+
37+
# Add more checks here for other things we want to verify.
38+
return
39+
40+
41+
@click.command()
42+
@click.argument("definitions", nargs=-1, metavar="CIRCUITPY_X=1 CIRCUITPY_Y=0 ...")
43+
@click.argument(
44+
"sdkconfig_h", required=True, nargs=1, type=click.File("r"), metavar="<path to sdkconfig.h>"
45+
)
46+
def run(definitions, sdkconfig_h):
47+
sdk_config = collect_definitions(sdkconfig_h)
48+
49+
# Parse definitions arguments
50+
circuitpy_config = {}
51+
for definition in definitions:
52+
k, v = definition.split("=", 1)
53+
circuitpy_config[k] = int_or_string(v)
54+
55+
# Validate.
56+
validate(sdk_config, circuitpy_config)
57+
58+
59+
if __name__ == "__main__":
60+
run()

0 commit comments

Comments
 (0)