Skip to content

Commit ebc72b4

Browse files
committed
Add bump implementation for GentooVersion
Implements GentooVersion.bump() to compute the branch-bump upper bound for Gentoo ebuild versions. This is required for translating GLSA advisory unaffected revision ranges (rge/rgt) into fixed version ranges in VulnerableCode GentooImporter. Closes #180 Assisted-by: Antigravity Signed-off-by: Adnan9-63 <muhammad1414114@gmail.com>
1 parent 92b1a24 commit ebc72b4

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎src/univers/gentoo.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,40 @@ def vercmp(ver1, ver2):
181181
# Our versions had different strings but ended up being equal.
182182
# The revision holds the final difference.
183183
return cmp(rev1, rev2)
184+
185+
186+
def bump(ver):
187+
"""
188+
Return a new version string where the next to the last revision number
189+
is one greater (e.g., 5.3.1 => 5.4) i.e., incrementing this GentooVersion
190+
last numeric segment.
191+
"""
192+
if not ver:
193+
return None
194+
195+
ver = remove_spaces(ver)
196+
if ":" in ver:
197+
ver = ver.split(":")[0]
198+
if ver.endswith("*"):
199+
ver = ver[:-1]
200+
201+
version, _ = parse_version_and_revision(ver)
202+
203+
base = version.split("_")[0]
204+
segments = base.split(".")
205+
206+
207+
if segments and segments[-1] and segments[-1][-1].isalpha():
208+
segments[-1] = segments[-1][:-1]
209+
210+
int_segments = [int(s) for s in segments if s]
211+
212+
if not int_segments:
213+
return None
214+
215+
if len(int_segments) > 1:
216+
int_segments.pop()
217+
218+
int_segments[-1] += 1
219+
return ".".join(str(s) for s in int_segments)
220+

‎src/univers/versions.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ def __gt__(self, other):
429429
return NotImplemented
430430
return gentoo.vercmp(self.value, other.value) == 1
431431

432+
def bump(self):
433+
bumped = gentoo.bump(self.value)
434+
return self.__class__(bumped) if bumped else None
435+
436+
432437

433438
class AlpineLinuxVersion(GentooVersion):
434439
@classmethod

‎tests/test_gentoo.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,23 @@ def comparison(self):
3131
def test_gentoo_vers_cmp(test_case):
3232
avc = GentooVersionComp.from_data(data=test_case)
3333
avc.assert_result()
34+
35+
36+
def test_gentoo_bump():
37+
assert GentooVersion("2.23.3").bump() == GentooVersion("2.24")
38+
assert GentooVersion("2.24.3").bump() == GentooVersion("2.25")
39+
assert GentooVersion("2.25.4").bump() == GentooVersion("2.26")
40+
assert GentooVersion("2.26.2").bump() == GentooVersion("2.27")
41+
assert GentooVersion("1.0.5").bump() == GentooVersion("1.1")
42+
assert GentooVersion("5.3").bump() == GentooVersion("6")
43+
assert GentooVersion("5").bump() == GentooVersion("6")
44+
assert GentooVersion("1.3.8b").bump() == GentooVersion("1.4")
45+
assert GentooVersion("9.9_p2").bump() == GentooVersion("10")
46+
assert GentooVersion("0.13.4-r2").bump() == GentooVersion("0.14")
47+
assert GentooVersion("2.23.3-r1").bump() == GentooVersion("2.24")
48+
# Handling slots and wildcards during bump
49+
from univers import gentoo
50+
assert gentoo.bump("3.24.48:3") == "3.25"
51+
assert gentoo.bump("1.3*") == "2"
52+
53+

0 commit comments

Comments
 (0)