From 9b2d28c74a358ad8c69aff21c87e4a7606c043bb Mon Sep 17 00:00:00 2001 From: Kevin Weiss Date: Fri, 2 May 2025 12:30:45 +0200 Subject: [PATCH] fix: adapt FirmwareID to work with origin --- src/lob_hlpr/lib_types.py | 15 +++++++++++++++ tests/test_firmware_id.py | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/lob_hlpr/lib_types.py b/src/lob_hlpr/lib_types.py index 8844885..bdb1516 100644 --- a/src/lob_hlpr/lib_types.py +++ b/src/lob_hlpr/lib_types.py @@ -98,12 +98,27 @@ class FirmwareID: # Date group, matches everything inside the parentheses, made entirely optional r"(?:\s*\((?P.+?)\))?$", ) + _IDENTIFIER_RE_ORIGIN = re.compile( + # Name group, non-greedy match up to the first + + r"^(?P.+?)\+" + # Version group, matches a semantic versioning pattern + r"(?P[0-9]+(?:\.[0-9]+){2}(?:-[\w]+)?(?:-\d+-g[0-9a-f]+)?)" + # Optional variant group, matches anything after a '+' until a space or end + r"(?:\+(?P[^\s]+))?" + # Optional additional group, + # matches before parentheses if not directly next to variant + r"(?:\s+(?P[^\(\)]+?))?" + # Date group, matches everything inside the parentheses, made entirely optional + r"(?:\s*\((?P.+?)\))?$", + ) """Precompiled regex for parsing all parts of a firmware identifier string.""" def __post_init__(self): """Parses the firmware identifier string and updates attributes accordingly.""" try: m = self._IDENTIFIER_RE.match(self.id_string) + if not m: + m = self._IDENTIFIER_RE_ORIGIN.match(self.id_string) groups = m.groupdict() self.name = groups["name"] self.version = FirmwareVersion(groups["version"]) diff --git a/tests/test_firmware_id.py b/tests/test_firmware_id.py index 3bc87b3..10b1df6 100644 --- a/tests/test_firmware_id.py +++ b/tests/test_firmware_id.py @@ -19,6 +19,17 @@ def test_valid_firmware_id_with_variants(): assert firmware_id.built == "2024-03-11T13:57:40" +def test_valid_firmware_id_with_origin_pass(): + """Tests if the firmware ID can be created with 'origin' in the version string.""" + id_str = "app-nrf91-origin+0.3.7+hw4 TZ3 (Mar 5 2025 08:36:26)" + firmware_id = FirmwareID(id_str) + assert firmware_id.name == "app-nrf91-origin" + assert isinstance(firmware_id.version, FirmwareVersion) + assert firmware_id.version.version_string == "0.3.7" + assert firmware_id.variants == ["hw4"] + assert firmware_id.built == "2025-03-05T08:36:26" + + def test_valid_firmware_id_unknown(): """Tests if the firmware ID can be created with 'unknown' in the version string.""" id_str = "app-nrf9160-wmbus v0.0.0-unknown+hw3 TZ2 (Oct 12 2023 10:45:49)"