Skip to content

Commit a0fafed

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 a0fafed

6 files changed

Lines changed: 859 additions & 0 deletions

File tree

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 = "alpine"
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ class InvalidVersion(ValueError):
3434
pass
3535

3636

37+
def is_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+
For example:
43+
>>> is_valid_alpine_version("006")
44+
False
45+
>>> is_valid_alpine_version("1.2.3")
46+
True
47+
>>> is_valid_alpine_version("02-r1")
48+
False
49+
"""
50+
left, _, _ = s.partition(".")
51+
# hanlde the suffix case
52+
left, _, _ = left.partition("-")
53+
if not left.isdigit():
54+
return True
55+
i = int(left)
56+
return str(i) == left
57+
58+
3759
@attr.s(frozen=True, order=False, hash=True)
3860
class Version:
3961
"""
@@ -299,3 +321,25 @@ def __lt__(self, other):
299321
if not isinstance(other, self.__class__):
300322
return NotImplemented
301323
return gentoo.vercmp(self.value, other.value) == -1
324+
325+
326+
@attr.s(frozen=True, order=False, eq=False, hash=True)
327+
class AlpineLinuxVersion(Version):
328+
@classmethod
329+
def is_valid(cls, string):
330+
return is_valid_alpine_version(string) and gentoo.is_valid(string)
331+
332+
def __eq__(self, other):
333+
if not isinstance(other, self.__class__):
334+
return NotImplemented
335+
return gentoo.vercmp(self.value, other.value) == 0
336+
337+
def __lt__(self, other):
338+
if not isinstance(other, self.__class__):
339+
return NotImplemented
340+
return gentoo.vercmp(self.value, other.value) < 0
341+
342+
def __gt__(self, other):
343+
if not isinstance(other, self.__class__):
344+
return NotImplemented
345+
return gentoo.vercmp(self.value, other.value) > 0

0 commit comments

Comments
 (0)