From 2d0739005aed5620a8a8816b8db81b966cc9fd32 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 13 Dec 2023 18:35:43 -0600 Subject: [PATCH] parse_boot_out helper. change DiskBackend() to take boot_out string instead of version info tuple --- circup/backends.py | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/circup/backends.py b/circup/backends.py index 4b4c51a..72bcd34 100644 --- a/circup/backends.py +++ b/circup/backends.py @@ -161,6 +161,22 @@ def is_device_present(self): """ raise NotImplementedError + @staticmethod + def parse_boot_out_file(boot_out_contents): + """ + Parse the contents of boot_out.txt + Returns: circuitpython version and board id + """ + lines = boot_out_contents.split("\n") + version_line = lines[0] + circuit_python = version_line.split(";")[0].split(" ")[-3] + board_line = lines[1] + if board_line.startswith("Board ID:"): + board_id = board_line[9:].strip() + else: + board_id = "" + return circuit_python, board_id + class WebBackend(Backend): """ @@ -468,9 +484,14 @@ def is_device_present(self): class DiskBackend(Backend): """ Backend for interacting with a device via USB Workflow + + :param String device_location: Path to the device + :param logger: logger to use for outputting messages + :param String boot_out: Optional mock contents of a boot_out.txt file + to use for version information. """ - def __init__(self, device_location, logger, version_info=None): + def __init__(self, device_location, logger, boot_out=None): if device_location is None: raise ValueError( "Auto locating USB Disk based device failed. " @@ -481,7 +502,9 @@ def __init__(self, device_location, logger, version_info=None): self.LIB_DIR_PATH = "lib" self.device_location = device_location self.library_path = self.device_location + "/" + self.LIB_DIR_PATH - self.version_info = version_info + self.version_info = None + if boot_out is not None: + self.version_info = self.parse_boot_out_file(boot_out) def get_circuitpython_version(self): """ @@ -505,13 +528,10 @@ def get_circuitpython_version(self): "r", encoding="utf-8", ) as boot: - version_line = boot.readline() - circuit_python = version_line.split(";")[0].split(" ")[-3] - board_line = boot.readline() - if board_line.startswith("Board ID:"): - board_id = board_line[9:].strip() - else: - board_id = "" + boot_out_contents = boot.read() + circuit_python, board_id = self.parse_boot_out_file( + boot_out_contents + ) except FileNotFoundError: click.secho( "Missing file boot_out.txt on the device: wrong path or drive corrupted.", @@ -519,7 +539,7 @@ def get_circuitpython_version(self): ) self.logger.error("boot_out.txt not found.") sys.exit(1) - + print((circuit_python, board_id)) return circuit_python, board_id return self.version_info