mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-10 14:24:16 +02:00
[arch] fix -> regressions from commit db5eeabf
This commit is contained in:
@@ -1,35 +1,51 @@
|
||||
from typing import Optional
|
||||
import re
|
||||
|
||||
from packaging.version import parse
|
||||
from packaging.version import LegacyVersion
|
||||
|
||||
RE_VERSION_WITH_RELEASE = re.compile(r'^(.+)-\d+$')
|
||||
RE_VERSION_WITH_EPOCH = re.compile(r'^\d+:(.+)$')
|
||||
|
||||
|
||||
def normalize_version(version: str) -> Optional[str]:
|
||||
if version:
|
||||
final_version = version.strip()
|
||||
def normalize_version(version: str) -> LegacyVersion:
|
||||
final_version = version.strip()
|
||||
|
||||
if final_version:
|
||||
if ':' not in final_version:
|
||||
final_version = '0:{}'.format(final_version)
|
||||
if not RE_VERSION_WITH_EPOCH.match(final_version):
|
||||
final_version = '0:{}'.format(final_version)
|
||||
|
||||
if '-' not in final_version:
|
||||
final_version = '{}-1'.format(final_version)
|
||||
if not RE_VERSION_WITH_RELEASE.match(final_version):
|
||||
final_version = '{}-1'.format(final_version)
|
||||
|
||||
return final_version
|
||||
return LegacyVersion(final_version)
|
||||
|
||||
|
||||
def compare_versions(version_a: str, operator: str, version_b: str) -> bool:
|
||||
if operator:
|
||||
a_obj, b_obj = parse(normalize_version(version_a)), parse(normalize_version(version_b))
|
||||
def match_required_version(current_version: str, operator: str, required_version: str) -> bool:
|
||||
final_required, final_current = required_version.strip(), current_version.strip()
|
||||
|
||||
if operator == '>':
|
||||
return a_obj > b_obj
|
||||
elif operator == '>=':
|
||||
return a_obj >= b_obj
|
||||
elif operator == '<':
|
||||
return a_obj < b_obj
|
||||
elif operator == '<=':
|
||||
return a_obj <= b_obj
|
||||
elif operator == '==':
|
||||
return a_obj == b_obj
|
||||
required_has_epoch, current_no_epoch = bool(RE_VERSION_WITH_EPOCH.match(final_required)), RE_VERSION_WITH_EPOCH.split(final_current)
|
||||
current_has_epoch = len(current_no_epoch) > 1
|
||||
|
||||
raise Exception("compare: invalid string operator {}".format(operator))
|
||||
if required_has_epoch and not current_has_epoch:
|
||||
final_current = '0:{}'.format(final_current)
|
||||
elif current_has_epoch and not required_has_epoch:
|
||||
final_current = current_no_epoch[1]
|
||||
|
||||
required_has_release, current_no_release = bool(RE_VERSION_WITH_RELEASE.match(final_required)), RE_VERSION_WITH_RELEASE.split(final_current)
|
||||
current_has_release = len(current_no_release) > 1
|
||||
|
||||
if required_has_release and not current_has_release:
|
||||
final_current = '{}-1'.format(final_current)
|
||||
elif current_has_release and not required_has_release:
|
||||
final_current = current_no_release[1]
|
||||
|
||||
final_required, final_current = LegacyVersion(final_required), LegacyVersion(final_current)
|
||||
|
||||
if operator == '==' or operator == '=':
|
||||
return final_current == final_required
|
||||
elif operator == '>':
|
||||
return final_current > final_required
|
||||
elif operator == '>=':
|
||||
return final_current >= final_required
|
||||
elif operator == '<':
|
||||
return final_current < final_required
|
||||
elif operator == '<=':
|
||||
return final_current <= final_required
|
||||
|
||||
Reference in New Issue
Block a user