Skip to content

Commit 657b0f7

Browse files
committed
Add composer and golang versions
And also support gitlab native ranges for them Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 56990bf commit 657b0f7

5 files changed

Lines changed: 19925 additions & 51 deletions

File tree

src/univers/version_range.py

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,16 @@ class ComposerVersionRange(VersionRange):
664664
# TODO composer may need its own scheme see https//github.com/nexB/univers/issues/5
665665
# and https//getcomposer.org/doc/articles/versions.md
666666
scheme = "composer"
667-
version_class = versions.SemverVersion
667+
version_class = versions.ComposerVersion
668+
669+
vers_by_native_comparators = {
670+
"==": "=",
671+
"<=": "<=",
672+
">=": ">=",
673+
"<": "<",
674+
">": ">",
675+
"=": "=", # This is not a native composer-semver comparator, but is used in the gitlab version range for composer packages.
676+
}
668677

669678

670679
class RpmVersionRange(VersionRange):
@@ -757,7 +766,16 @@ class GolangVersionRange(VersionRange):
757766
"""
758767

759768
scheme = "golang"
760-
version_class = versions.SemverVersion
769+
version_class = versions.GolangVersion
770+
771+
vers_by_native_comparators = {
772+
"==": "=",
773+
"<=": "<=",
774+
">=": ">=",
775+
"<": "<",
776+
">": ">",
777+
"=": "=", # This is not a native golang-semver comparator, but is used in the gitlab version range for go packages.
778+
}
761779

762780

763781
class GenericVersionRange(VersionRange):
@@ -973,49 +991,47 @@ def from_native(cls, string):
973991
return cls(constraints=constraints)
974992

975993

976-
class GitLabVersionRange(VersionRange):
977-
@classmethod
978-
def from_gitlab_native(cls, scheme, string):
979-
vrc = RANGE_CLASS_BY_SCHEMES[scheme]
980-
constraint_items = []
981-
constraints = []
982-
split = " "
983-
if scheme == "pypi":
984-
split = ","
985-
pipe_separated_constraints = string.split("||")
986-
for pipe_separated_constraint in pipe_separated_constraints:
987-
space_seperated_constraints = pipe_separated_constraint.split(split)
988-
constraint_items.extend(space_seperated_constraints)
989-
comparator = ""
990-
# A constraint item can be a comparator or a version or a version with comparator
991-
# If it's empty continue
992-
# If it's in `vers_by_native_comparators`, append it with the comparator and continue
993-
# If it's a version, make version constraint from the version and use the comparator from the previous item and make comparator empty
994-
# If it's a version with comparator, use split_req to get version and comparator to form constraint and make comparator empty
995-
for constraint_item in constraint_items:
996-
if not constraint_item:
997-
continue
998-
if "".join([comparator, constraint_item]) in vrc.vers_by_native_comparators:
999-
comparator = "".join([comparator, constraint_item])
1000-
comparator = vrc.vers_by_native_comparators[comparator]
1001-
continue
1002-
if comparator:
1003-
constraints.append(
1004-
VersionConstraint(
1005-
comparator=comparator, version=vrc.version_class(constraint_item)
1006-
)
1007-
)
1008-
else:
1009-
comparator, version_constraint = split_req(
1010-
constraint_item, vrc.vers_by_native_comparators
1011-
)
1012-
constraints.append(
1013-
VersionConstraint(
1014-
comparator=comparator, version=vrc.version_class(version_constraint)
1015-
)
994+
def from_gitlab_native(gitlab_scheme, string):
995+
purl_scheme = PURL_TYPE_BY_GITLAB_SCHEME[gitlab_scheme]
996+
vrc = RANGE_CLASS_BY_SCHEMES[purl_scheme]
997+
constraint_items = []
998+
constraints = []
999+
split = " "
1000+
split_by_comma_schemes = ["pypi", "composer"]
1001+
if purl_scheme in split_by_comma_schemes:
1002+
split = ","
1003+
pipe_separated_constraints = string.split("||")
1004+
for pipe_separated_constraint in pipe_separated_constraints:
1005+
space_seperated_constraints = pipe_separated_constraint.split(split)
1006+
constraint_items.extend(space_seperated_constraints)
1007+
comparator = ""
1008+
# A constraint item can be a comparator or a version or a version with comparator
1009+
# If it's empty continue
1010+
# If it's in `vers_by_native_comparators`, append it with the comparator and continue
1011+
# If it's a version, make version constraint from the version and use the comparator from the previous item and make comparator empty
1012+
# If it's a version with comparator, use split_req to get version and comparator to form constraint and make comparator empty
1013+
for constraint_item in constraint_items:
1014+
if not constraint_item:
1015+
continue
1016+
if "".join([comparator, constraint_item]) in vrc.vers_by_native_comparators:
1017+
comparator = "".join([comparator, constraint_item])
1018+
comparator = vrc.vers_by_native_comparators[comparator]
1019+
continue
1020+
if comparator:
1021+
constraints.append(
1022+
VersionConstraint(comparator=comparator, version=vrc.version_class(constraint_item))
1023+
)
1024+
else:
1025+
comparator, version_constraint = split_req(
1026+
constraint_item, vrc.vers_by_native_comparators
1027+
)
1028+
constraints.append(
1029+
VersionConstraint(
1030+
comparator=comparator, version=vrc.version_class(version_constraint)
10161031
)
1017-
comparator = ""
1018-
return vrc(constraints=constraints)
1032+
)
1033+
comparator = ""
1034+
return vrc(constraints=constraints)
10191035

10201036

10211037
vers_by_github_native_comparators = {
@@ -1101,3 +1117,13 @@ def build_range_from_github_advisory_constraint(scheme: str, string: str):
11011117
"nginx": NginxVersionRange,
11021118
"openssl": OpensslVersionRange,
11031119
}
1120+
1121+
PURL_TYPE_BY_GITLAB_SCHEME = {
1122+
"gem": "gem",
1123+
"go": "golang",
1124+
"maven": "maven",
1125+
"npm": "npm",
1126+
"nuget": "nuget",
1127+
"pypi": "pypi",
1128+
"packagist": "composer",
1129+
}

src/univers/versions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,20 @@ def __gt__(self, other):
427427
return gentoo.vercmp(self.value, other.value) > 0
428428

429429

430+
@attr.s(frozen=True, order=False, eq=False, hash=True)
431+
class ComposerVersion(SemverVersion):
432+
@classmethod
433+
def build_value(cls, string):
434+
return semantic_version.Version.coerce(string.lstrip("vV"))
435+
436+
437+
@attr.s(frozen=True, order=False, eq=False, hash=True)
438+
class GolangVersion(SemverVersion):
439+
@classmethod
440+
def build_value(cls, string):
441+
return semantic_version.Version.coerce(string.lstrip("vV"))
442+
443+
430444
@attr.s(frozen=True, order=False, eq=False, hash=True)
431445
class LegacyOpensslVersion(Version):
432446
"""

0 commit comments

Comments
 (0)