Skip to content

Commit

Permalink
fix comparison for OpenSSL pre-release
Browse files Browse the repository at this point in the history
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed May 1, 2022
1 parent 033e23f commit fa50194
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/univers/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,19 @@ class LegacyOpensslVersion(Version):
univers.versions.InvalidVersion: '3.0.2' is not a valid <class 'univers.versions.LegacyOpensslVersion'>
"""

major = attr.ib(type=int, default=None)
minor = attr.ib(type=int, default=None)
build = attr.ib(type=int, default=None)
patch = attr.ib(type=str, default=None)

def __attrs_post_init__(self):
Version.__attrs_post_init__(self)
major, minor, build, patch = self.parse(self.string)
object.__setattr__(self, "major", major)
object.__setattr__(self, "minor", minor)
object.__setattr__(self, "build", build)
object.__setattr__(self, "patch", patch)

@classmethod
def is_valid(cls, string):
return bool(cls.parse(string))
Expand Down Expand Up @@ -445,7 +458,29 @@ def build_value(cls, string):
return cls.parse(string)

def __str__(self):
return f"{self.value[0]}.{self.value[1]}.{self.value[2]}{self.value[3]}"
return f"{self.major}.{self.minor}.{self.build}{self.patch}"

def __lt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
# Check if versions have the same base, and `one and only one` of them is a pre-release.
if (self.major, self.minor, self.build) == (other.major, other.minor, other.build) and (
self.is_prerelease() != other.is_prerelease()
):
return self.is_prerelease()
return self.value.__lt__(other.value)

def __gt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
if (self.major, self.minor, self.build) == (other.major, other.minor, other.build) and (
self.is_prerelease() != other.is_prerelease()
):
return other.is_prerelease()
return self.value.__gt__(other.value)

def is_prerelease(self):
return self.patch.startswith(("-beta", "-alpha"))


@attr.s(frozen=True, order=False, eq=False, hash=True)
Expand Down

0 comments on commit fa50194

Please sign in to comment.