Skip to content

Commit 498c403

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 033e23f commit 498c403

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
@@ -161,6 +161,7 @@ def __ge__(self, other):
161161

162162
@attr.s(frozen=True, order=False, hash=True)
163163
class GenericVersion(Version):
164+
164165
@classmethod
165166
def is_valid(cls, string):
166167
# generic implementation ...
@@ -220,6 +221,64 @@ def is_valid(cls, string):
220221
except ValueError:
221222
return False
222223

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

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

0 commit comments

Comments
 (0)