|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | + |
| 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' |
| 21 | + v = sdk_config.get(k, v) |
| 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