diff --git a/napalm_device_types/host_status.py b/napalm_device_types/host_status.py index c690794..f2bc5be 100644 --- a/napalm_device_types/host_status.py +++ b/napalm_device_types/host_status.py @@ -177,6 +177,17 @@ def _sections(output: str) -> dict[str, list[str]]: _VERSION_SEGMENT = re.compile(r"~|\d+|[A-Za-z]+") +def _segment_cmp(x: str, y: str) -> int: + """One segment against another, as :func:`_version_cmp` takes them; 0 when equal.""" + if "~" in (x, y): + return (y == "~") - (x == "~") + if x.isdigit() and y.isdigit(): + return (int(x) > int(y)) - (int(x) < int(y)) + if x.isdigit() or y.isdigit(): + return 1 if x.isdigit() else -1 + return (x > y) - (x < y) + + def _version_cmp(a: str, b: str) -> int: """rpm's ``rpmvercmp``: below 0 when ``a`` is older, above when newer. @@ -188,15 +199,8 @@ def _version_cmp(a: str, b: str) -> int: """ left, right = _VERSION_SEGMENT.findall(a), _VERSION_SEGMENT.findall(b) for x, y in zip(left, right, strict=False): - if x == y: - continue - if "~" in (x, y): - return -1 if x == "~" else 1 - if x.isdigit() != y.isdigit(): - return 1 if x.isdigit() else -1 - if x.isdigit(): - return (int(x) > int(y)) - (int(x) < int(y)) - return 1 if x > y else -1 + if order := _segment_cmp(x, y): + return order if len(left) == len(right): return 0 longer, sign = (left, 1) if len(left) > len(right) else (right, -1)