From 8761077eacdb44f117d22871399b6fe7e7a7531c Mon Sep 17 00:00:00 2001 From: mikee47 Date: Sun, 14 Mar 2021 09:33:52 +0000 Subject: [PATCH] Refactor hwconfig.py, using functions to handle each command --- .../Storage/Tools/hwconfig/hwconfig.py | 92 ++++++++++--------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/Sming/Components/Storage/Tools/hwconfig/hwconfig.py b/Sming/Components/Storage/Tools/hwconfig/hwconfig.py index 29970095a4..e2b3d90ddb 100644 --- a/Sming/Components/Storage/Tools/hwconfig/hwconfig.py +++ b/Sming/Components/Storage/Tools/hwconfig/hwconfig.py @@ -20,6 +20,53 @@ def openOutput(path): return open(path, 'wb') +def handle_validate(args, config, part): + # Validate resulting hardware configuration against schema + try: + from jsonschema import Draft7Validator + inst = json.loads(config.to_json()) + schema = json.load(open(args.expr)) + v = Draft7Validator(schema) + errors = sorted(v.iter_errors(inst), key=lambda e: e.path) + if errors != []: + for e in errors: + critical("%s @ %s" % (e.message, e.path)) + sys.exit(3) + except ImportError as err: + critical("\n** WARNING! %s: Cannot validate '%s', please run `make python-requirements **\n\n" % (str(err), args.input)) + + +def handle_flashcheck(args, config, part): + # Expect list of chunks, such as "0x100000=/out/Esp8266/debug/firmware/spiff_rom.bin 0x200000=custom.bin" + list = args.expr.split() + if len(list) == 0: + raise InputError("No chunks to flash!") + for e in list: + addr, filename = e.split('=') + addr = int(addr, 0) + part = config.partitions.find_by_address(config.devices[0], addr) + if part is None: + raise InputError("No partition contains address 0x%08x" % addr) + if part.address != addr: + raise InputError("Address 0x%08x is within partition '%s', not at start (0x%08x)" % (addr, part.name, part.address)) + filesize = os.path.getsize(filename) + if filesize > part.size: + raise InputError("File '%s' is 0x%08x bytes, too big for partition '%s' (0x%08x bytes)" % (os.path.basename(filename), filesize, part.name, part.size)) + + +def handle_partgen(args, config, part): + # Generate partition table binary + if not args.no_verify: + status("Verifying partition table...") + config.verify(args.secure) + return config.partitions.to_binary(config.devices) + + +def handle_expr(args, config, part): + # Evaluate expression against configuration data + return str(eval(args.expr)).encode() + + def main(): parser = argparse.ArgumentParser(description='Sming hardware configuration utility') @@ -49,53 +96,14 @@ def main(): config = Config.from_name(args.input) partitions = config.partitions - # Define local so it's available for eval() + # Locate any supplied partition by name + part = None if args.part is not None: part = partitions.find_by_name(args.part) if part is None: return - if args.command == 'validate': - # Validate resulting hardware configuration against schema - try: - from jsonschema import Draft7Validator - inst = json.loads(config.to_json()) - schema = json.load(open(args.expr)) - v = Draft7Validator(schema) - errors = sorted(v.iter_errors(inst), key=lambda e: e.path) - if errors != []: - for e in errors: - critical("%s @ %s" % (e.message, e.path)) - sys.exit(3) - except ImportError as err: - critical("\n** WARNING! %s: Cannot validate '%s', please run `make python-requirements **\n\n" % (str(err), args.input)) - elif args.command == 'flashcheck': - # Expect list of chunks, such as "0x100000=/out/Esp8266/debug/firmware/spiff_rom.bin 0x200000=custom.bin" - list = args.expr.split() - if len(list) == 0: - raise InputError("No chunks to flash!") - for e in list: - addr, filename = e.split('=') - addr = int(addr, 0) - part = config.partitions.find_by_address(config.devices[0], addr) - if part is None: - raise InputError("No partition contains address 0x%08x" % addr) - if part.address != addr: - raise InputError("Address 0x%08x is within partition '%s', not at start (0x%08x)" % (addr, part.name, part.address)) - filesize = os.path.getsize(filename) - if filesize > part.size: - raise InputError("File '%s' is 0x%08x bytes, too big for partition '%s' (0x%08x bytes)" % (os.path.basename(filename), filesize, part.name, part.size)) - elif args.command == 'partgen': - # Generate partition table binary - if not args.no_verify: - status("Verifying partition table...") - config.verify(args.secure) - output = config.partitions.to_binary(config.devices) - elif args.command == 'expr': - # Evaluate expression against configuration data - output = str(eval(args.expr)).encode() - else: - raise InputError('Unknown command: %s' % args.command) + output = globals()['handle_' + args.command](args, config, part) if output is not None: openOutput(args.output).write(output)