refactor(host-status): compare one version segment in a helper of its own
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 56s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 1m8s
driver / lowest (pull_request) Successful in 40s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Successful in 2m30s
driver / publish (pull_request) Skipped

The quality ratchet refused _version_cmp at complexity 12. Comparing one
segment moves to _segment_cmp, which also takes 01 and 1 as equal, as
rpm does.
This commit is contained in:
christianmanivong committed 2026-11-12 11:05:49 +01:00
1 parent cf555d006d
commit c736fb30d0
1 file changed
+13 -9
+13 -9
View File
@@ -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)