Skip to content

Commit

Permalink
parse_boot_out helper. change DiskBackend() to take boot_out string i…
Browse files Browse the repository at this point in the history
…nstead of version info tuple
  • Loading branch information
FoamyGuy committed Dec 14, 2023
1 parent 31270a0 commit 2d07390
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions circup/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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. "
Expand All @@ -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):
"""
Expand All @@ -505,21 +528,18 @@ 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.",
fg="red",
)
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
Expand Down

0 comments on commit 2d07390

Please sign in to comment.