Skip to content

Commit

Permalink
Refactor hwconfig.py, using functions to handle each command
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Mar 14, 2021
1 parent 6932420 commit 8761077
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 deletions Sming/Components/Storage/Tools/hwconfig/hwconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8761077

Please sign in to comment.