Skip to content

Commit 759d46e

Browse files
committed
Add alpine support in univers
Currently univers don't have a VersionClass for alpine linux, so add a version class for alpine linux and use same vercmp method that has been used by gentoo, avoid cases for now which have a leading 0 in them. Verify the current implementation of vercmp with the test data from here https://git.alpinelinux.org/apk-tools/tree/tests/version.data add ABOUT file and license for same Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 2186416 commit 759d46e

10 files changed

Lines changed: 947 additions & 124 deletions

src/univers/rpm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ def compare_rpm_versions(a: Union[RpmVersion, str], b: Union[RpmVersion, str]) -
131131

132132

133133
class Vercmp:
134-
R_NONALNUMTILDE_CARET = re.compile(br"^([^a-zA-Z0-9~\^]*)(.*)$")
135-
R_NUM = re.compile(br"^([\d]+)(.*)$")
136-
R_ALPHA = re.compile(br"^([a-zA-Z]+)(.*)$")
134+
R_NONALNUMTILDE_CARET = re.compile(rb"^([^a-zA-Z0-9~\^]*)(.*)$")
135+
R_NUM = re.compile(rb"^([\d]+)(.*)$")
136+
R_ALPHA = re.compile(rb"^([a-zA-Z]+)(.*)$")
137137

138138
@classmethod
139139
def compare(cls, first, second):

src/univers/version_range.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,11 @@ class EbuildVersionRange(VersionRange):
721721
version_class = versions.GentooVersion
722722

723723

724+
class AlpineLinuxVersionRange(VersionRange):
725+
scheme = "alpinelinux"
726+
version_class = versions.AlpineLinuxVersion
727+
728+
724729
class ArchLinuxVersionRange(VersionRange):
725730
scheme = "archlinux"
726731
version_class = versions.ArchLinuxVersion

src/univers/versions.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ class InvalidVersion(ValueError):
3434
pass
3535

3636

37+
def valid_alpine_version(s):
38+
"""
39+
Return True is the string `s` is a valid Alpine version.
40+
We do not support yet version strings that start with
41+
non-significant zeros.
42+
"""
43+
left, _, _ = s.partition(".")
44+
# hanlde the suffice case
45+
left, _, _ = left.partition("-")
46+
if not left.isdigit():
47+
return True
48+
i = int(left)
49+
return str(i) == left
50+
51+
3752
@attr.s(frozen=True, order=False, hash=True)
3853
class Version:
3954
"""
@@ -299,3 +314,25 @@ def __lt__(self, other):
299314
if not isinstance(other, self.__class__):
300315
return NotImplemented
301316
return gentoo.vercmp(self.value, other.value) == -1
317+
318+
319+
@attr.s(frozen=True, order=False, eq=False, hash=True)
320+
class AlpineLinuxVersion(Version):
321+
@classmethod
322+
def is_valid(cls, string):
323+
return valid_alpine_version(string) and gentoo.is_valid(string)
324+
325+
def __eq__(self, other):
326+
if not isinstance(other, self.__class__):
327+
return NotImplemented
328+
return gentoo.vercmp(self.value, other.value) == 0
329+
330+
def __lt__(self, other):
331+
if not isinstance(other, self.__class__):
332+
return NotImplemented
333+
return gentoo.vercmp(self.value, other.value) < 0
334+
335+
def __gt__(self, other):
336+
if not isinstance(other, self.__class__):
337+
return NotImplemented
338+
return gentoo.vercmp(self.value, other.value) > 0

0 commit comments

Comments
 (0)