Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ Changelog
=========


Version v30.7.0
----------------
- Fix Npm version range


Version v30.6.0
----------------

- Add support for gitlab in pypi, npm, gem version ranges
- Subclass semver for nginx


Version v30.5.1
----------------

Expand Down
114 changes: 70 additions & 44 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,16 @@ class ComposerVersionRange(VersionRange):
# TODO composer may need its own scheme see https//github.com/nexB/univers/issues/5
# and https//getcomposer.org/doc/articles/versions.md
scheme = "composer"
version_class = versions.SemverVersion
version_class = versions.ComposerVersion

vers_by_native_comparators = {
"==": "=",
"<=": "<=",
">=": ">=",
"<": "<",
">": ">",
"=": "=", # This is not a native composer-semver comparator, but is used in the gitlab version range for composer packages.
}


class RpmVersionRange(VersionRange):
Expand Down Expand Up @@ -757,7 +766,16 @@ class GolangVersionRange(VersionRange):
"""

scheme = "golang"
version_class = versions.SemverVersion
version_class = versions.GolangVersion

vers_by_native_comparators = {
"==": "=",
"<=": "<=",
">=": ">=",
"<": "<",
">": ">",
"=": "=", # This is not a native golang-semver comparator, but is used in the gitlab version range for go packages.
}


class GenericVersionRange(VersionRange):
Expand Down Expand Up @@ -973,49 +991,47 @@ def from_native(cls, string):
return cls(constraints=constraints)


class GitLabVersionRange(VersionRange):
@classmethod
def from_gitlab_native(cls, scheme, string):
vrc = RANGE_CLASS_BY_SCHEMES[scheme]
constraint_items = []
constraints = []
split = " "
if scheme == "pypi":
split = ","
pipe_separated_constraints = string.split("||")
for pipe_separated_constraint in pipe_separated_constraints:
space_seperated_constraints = pipe_separated_constraint.split(split)
constraint_items.extend(space_seperated_constraints)
comparator = ""
# A constraint item can be a comparator or a version or a version with comparator
# If it's empty continue
# If it's in `vers_by_native_comparators`, append it with the comparator and continue
# If it's a version, make version constraint from the version and use the comparator from the previous item and make comparator empty
# If it's a version with comparator, use split_req to get version and comparator to form constraint and make comparator empty
for constraint_item in constraint_items:
if not constraint_item:
continue
if "".join([comparator, constraint_item]) in vrc.vers_by_native_comparators:
comparator = "".join([comparator, constraint_item])
comparator = vrc.vers_by_native_comparators[comparator]
continue
if comparator:
constraints.append(
VersionConstraint(
comparator=comparator, version=vrc.version_class(constraint_item)
)
)
else:
comparator, version_constraint = split_req(
constraint_item, vrc.vers_by_native_comparators
)
constraints.append(
VersionConstraint(
comparator=comparator, version=vrc.version_class(version_constraint)
)
def from_gitlab_native(gitlab_scheme, string):
purl_scheme = PURL_TYPE_BY_GITLAB_SCHEME[gitlab_scheme]
vrc = RANGE_CLASS_BY_SCHEMES[purl_scheme]
constraint_items = []
constraints = []
split = " "
split_by_comma_schemes = ["pypi", "composer"]
if purl_scheme in split_by_comma_schemes:
split = ","
pipe_separated_constraints = string.split("||")
for pipe_separated_constraint in pipe_separated_constraints:
space_seperated_constraints = pipe_separated_constraint.split(split)
constraint_items.extend(space_seperated_constraints)
comparator = ""
# A constraint item can be a comparator or a version or a version with comparator
# If it's empty continue
# If it's in `vers_by_native_comparators`, append it with the comparator and continue
# If it's a version, make version constraint from the version and use the comparator from the previous item and make comparator empty
# If it's a version with comparator, use split_req to get version and comparator to form constraint and make comparator empty
for constraint_item in constraint_items:
if not constraint_item:
continue
if "".join([comparator, constraint_item]) in vrc.vers_by_native_comparators:
comparator = "".join([comparator, constraint_item])
comparator = vrc.vers_by_native_comparators[comparator]
continue
if comparator:
constraints.append(
VersionConstraint(comparator=comparator, version=vrc.version_class(constraint_item))
)
else:
comparator, version_constraint = split_req(
constraint_item, vrc.vers_by_native_comparators, default="="
)
constraints.append(
VersionConstraint(
comparator=comparator, version=vrc.version_class(version_constraint)
)
comparator = ""
return vrc(constraints=constraints)
)
comparator = ""
return vrc(constraints=constraints)


vers_by_github_native_comparators = {
Expand Down Expand Up @@ -1101,3 +1117,13 @@ def build_range_from_github_advisory_constraint(scheme: str, string: str):
"nginx": NginxVersionRange,
"openssl": OpensslVersionRange,
}

PURL_TYPE_BY_GITLAB_SCHEME = {
"gem": "gem",
"go": "golang",
"maven": "maven",
"npm": "npm",
"nuget": "nuget",
"pypi": "pypi",
"packagist": "composer",
}
14 changes: 14 additions & 0 deletions src/univers/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,20 @@ def __gt__(self, other):
return gentoo.vercmp(self.value, other.value) > 0


@attr.s(frozen=True, order=False, eq=False, hash=True)
class ComposerVersion(SemverVersion):
@classmethod
def build_value(cls, string):
return semantic_version.Version.coerce(string.lstrip("vV"))


@attr.s(frozen=True, order=False, eq=False, hash=True)
class GolangVersion(SemverVersion):
@classmethod
def build_value(cls, string):
return semantic_version.Version.coerce(string.lstrip("vV"))


@attr.s(frozen=True, order=False, eq=False, hash=True)
class LegacyOpensslVersion(Version):
"""
Expand Down
Loading