Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
119 changes: 92 additions & 27 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,40 @@ def from_osv_v1(data, scheme):
"""


def get_allof_constraints(cls, clause):
"""
Return a list of VersionConstraint given an AllOf ``clause``.
"""
if not isinstance(clause, AllOf):
raise ValueError(f"Unknown clause type: {clause!r}")
allof_constraints = []
for constraint in clause.clauses:
comparator = cls.vers_by_native_comparators[constraint.operator]
version = cls.version_class(str(constraint.target))
constraint = VersionConstraint(comparator=comparator, version=version)
allof_constraints.append(constraint)
return allof_constraints


def get_npm_version_constraints_from_semver_npm_spec(string, cls):
"""
Return a VersionConstraint for the provided ``string``.
"""
spec = semantic_version.NpmSpec(string)
clause = spec.clause.simplify()
if isinstance(clause, (AnyOf, AllOf)):
anyof_constraints = []
if isinstance(clause, AnyOf):
for allof_clause in clause.clauses:
anyof_constraints.extend(get_allof_constraints(cls, allof_clause))
elif isinstance(clause, AllOf):
alloc = get_allof_constraints(cls, clause)
anyof_constraints.extend(alloc)
else:
raise ValueError(f"Unknown clause type: {spec!r}")
return anyof_constraints


class NpmVersionRange(VersionRange):
scheme = "npm"
version_class = versions.SemverVersion
Expand All @@ -245,35 +279,66 @@ def from_native(cls, string):

# an NpmSpec handles parsing of both the semver versions and node-semver
# ranges at once
spec = semantic_version.NpmSpec(string)

clause = spec.clause.simplify()
assert isinstance(clause, (AnyOf, AllOf))
anyof_constraints = []
if isinstance(clause, AnyOf):
for allof_clause in clause.clauses:
anyof_constraints.extend(get_allof_constraints(cls, allof_clause))
elif isinstance(clause, AllOf):
alloc = get_allof_constraints(cls, clause)
anyof_constraints.extend(alloc)
else:
raise ValueError(f"Unknown clause type: {spec!r}")

return cls(constraints=anyof_constraints)

if string == "*":
return cls(
constraints=[
VersionConstraint.from_string(string="*", version_class=cls.version_class)
]
)

def get_allof_constraints(cls, clause):
"""
Return a list of VersionConstraint given an AllOf ``clause``.
"""
assert isinstance(clause, AllOf)
allof_constraints = []
for constraint in clause.clauses:
comparator = cls.vers_by_native_comparators[constraint.operator]
version = cls.version_class(str(constraint.target))
constraint = VersionConstraint(comparator=comparator, version=version)
allof_constraints.append(constraint)
return allof_constraints
constraints = []
vrc = cls.version_class
# 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 range in string.split("||"):
if " - " in range:
constraints.extend(get_npm_version_constraints_from_semver_npm_spec(range, cls))
continue
comparator = ""
for constraint in range.split(" "):
if not constraint:
continue
if "".join([comparator, constraint]) in cls.vers_by_native_comparators:
comparator = "".join([comparator, constraint])
comparator = cls.vers_by_native_comparators[comparator]
continue
if comparator:
if constraint.endswith(".x"):
constraints.extend(
get_npm_version_constraints_from_semver_npm_spec(constraint, cls)
)
continue
Comment thread
TG1999 marked this conversation as resolved.
Outdated
constraint = constraint.lstrip("vV")
constraints.append(
VersionConstraint(comparator=comparator, version=vrc(constraint))
)
else:
if constraint.endswith(".x"):
Comment thread
TG1999 marked this conversation as resolved.
Outdated
constraints.extend(
get_npm_version_constraints_from_semver_npm_spec(constraint, cls)
)
continue
if constraint.startswith("~") or constraint.startswith("^"):
constraints.extend(
get_npm_version_constraints_from_semver_npm_spec(constraint, cls)
)
else:
comparator, version_constraint = split_req(
constraint, cls.vers_by_native_comparators, default="="
)
version_constraint = version_constraint.lstrip("vV")
constraints.append(
VersionConstraint(
comparator=comparator, version=vrc(version_constraint)
)
)
comparator = ""
Comment thread
TG1999 marked this conversation as resolved.
return cls(constraints=constraints)


class GemVersionRange(VersionRange):
Expand Down
Loading