Skip to content

Commit 2834922

Browse files
committed
Subclass semver for nginx
* Because nginx versions are not exactly semver. * Also add convenience properties to the semver version class to avoid leaking internals too much. Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 5a514f0 commit 2834922

2 files changed

Lines changed: 62 additions & 20 deletions

File tree

src/univers/version_range.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ class NginxVersionRange(VersionRange):
857857
"""
858858

859859
scheme = "nginx"
860-
version_class = versions.SemverVersion
860+
version_class = versions.NginxVersion
861861

862862
vers_by_native_comparators = {
863863
"==": "=",
@@ -919,12 +919,10 @@ def from_native(cls, string):
919919
# suffixed version
920920
vs = clauses.rstrip("+")
921921
version = cls.version_class(vs)
922-
is_stable = is_even(version.value.minor)
923-
924-
if is_stable:
922+
if version.is_stable:
925923
# we have a start and end in stable ranges
926924
start_version = cls.version_class(vs)
927-
end_version = cls.version_class(str(start_version.value.next_minor()))
925+
end_version = cls.version_class(str(start_version.next_minor()))
928926
vstart = VersionConstraint(comparator=">=", version=start_version)
929927
vend = VersionConstraint(comparator="<", version=end_version)
930928
constraints.extend([vstart, vend])
@@ -1036,21 +1034,6 @@ def build_range_from_github_advisory_constraint(scheme: str, string: str):
10361034
return vrc(constraints=constraints)
10371035

10381036

1039-
def is_even(s):
1040-
"""
1041-
Return True if the string "s" is an even number and False if this is an odd
1042-
number. For example:
1043-
1044-
>>> is_even(4)
1045-
True
1046-
>>> is_even(123)
1047-
False
1048-
>>> is_even(0)
1049-
True
1050-
"""
1051-
return (int(s) % 2) == 0
1052-
1053-
10541037
RANGE_CLASS_BY_SCHEMES = {
10551038
"npm": NpmVersionRange,
10561039
"deb": DebianVersionRange,

src/univers/versions.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def __ge__(self, other):
160160

161161
@attr.s(frozen=True, order=False, hash=True)
162162
class GenericVersion(Version):
163+
163164
@classmethod
164165
def is_valid(cls, string):
165166
# generic implementation ...
@@ -219,6 +220,64 @@ def is_valid(cls, string):
219220
except ValueError:
220221
return False
221222

223+
@property
224+
def major(self):
225+
return self.value and self.value.major
226+
227+
@property
228+
def minor(self):
229+
return self.value and self.value.minor
230+
231+
@property
232+
def patch(self):
233+
return self.value and self.value.patch
234+
235+
@property
236+
def prerelease(self):
237+
return self.value and self.value.prerelease
238+
239+
@property
240+
def build(self):
241+
return self.value and self.value.build
242+
243+
def next_major(self):
244+
return self.value and self.value.next_major()
245+
246+
def next_minor(self):
247+
return self.value and self.value.next_minor()
248+
249+
def next_patch(self):
250+
return self.value and self.value.next_patch()
251+
252+
253+
def is_even(s):
254+
"""
255+
Return True if the string "s" is an even number and False if this is an odd
256+
number. For example:
257+
258+
>>> is_even(4)
259+
True
260+
>>> is_even(123)
261+
False
262+
>>> is_even(0)
263+
True
264+
"""
265+
return (int(s) % 2) == 0
266+
267+
268+
@attr.s(frozen=True, order=False, eq=False, hash=True)
269+
class NginxVersion(SemverVersion):
270+
"""
271+
Semver with 3 segments and extra attribute for stable vs. unstable branches
272+
"""
273+
274+
@property
275+
def is_stable(self):
276+
"""
277+
True if this is a "stable "version
278+
"""
279+
return is_even(self.minor)
280+
222281

223282
@attr.s(frozen=True, order=False, eq=False, hash=True)
224283
class RubygemsVersion(Version):

0 commit comments

Comments
 (0)