feat: add version normalize and compare for the update swarm
This commit is contained in:
1 parent
f4ed6ac880
commit
8db8d00196
4 files changed
+60
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,33 @@
|
||||
from __future__ import annotations
|
||||
import re
|
||||
|
||||
_LANG = re.compile(r"\s*\((EN|ES|FR|DE|IT|PT|JP)\)\s*", re.I)
|
||||
_LEADING_V = re.compile(r"^\s*v", re.I)
|
||||
_NUMERIC = re.compile(r"^(\d+(?:\.\d+)*)")
|
||||
|
||||
def normalize_version(s: str) -> str:
|
||||
s = _LANG.sub(" ", s or "")
|
||||
s = _LEADING_V.sub("", s)
|
||||
return re.sub(r"\s+", " ", s).strip()
|
||||
|
||||
def _tuple(s: str) -> tuple[int, ...] | None:
|
||||
m = _NUMERIC.match(normalize_version(s))
|
||||
if not m:
|
||||
return None
|
||||
return tuple(int(p) for p in m.group(1).split("."))
|
||||
|
||||
def compare_versions(a: str, b: str) -> str:
|
||||
na, nb = normalize_version(a), normalize_version(b)
|
||||
if na.lower() == nb.lower() and na != "":
|
||||
return "eq"
|
||||
ta, tb = _tuple(a), _tuple(b)
|
||||
if ta is None or tb is None:
|
||||
return "incomparable"
|
||||
n = max(len(ta), len(tb))
|
||||
ta = ta + (0,) * (n - len(ta))
|
||||
tb = tb + (0,) * (n - len(tb))
|
||||
if ta < tb:
|
||||
return "lt"
|
||||
if ta > tb:
|
||||
return "gt"
|
||||
return "eq"
|
||||
Reference in new issue
Block a user