Skip to content

Commit

Permalink
Use Specifier class to check version constraints
Browse files Browse the repository at this point in the history
Package.add_constraint implements its own way to check for
version constraints, while packaging.specifiers.Specifier can
be used
  • Loading branch information
adanaja committed Sep 22, 2023
1 parent 83bfeaf commit e41bfab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 28 deletions.
36 changes: 8 additions & 28 deletions src/e3/python/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time
import packaging.version
import packaging.tags
import packaging.specifiers
from typing import TYPE_CHECKING
from e3.error import E3Error
from e3.python.wheel import Wheel
Expand Down Expand Up @@ -332,34 +333,13 @@ def add_constraint(self, requirement: Requirement) -> None:

# Apply version constraints
for spec in requirement.specs:
if spec[1].endswith(".*") and spec[0] == "!=":
# Handle requirements ending with * apart as it is not covered by
# packaging.version
self.versions = [
v for v in self.versions if not str(v).startswith(spec[1][:-2])
]
else:
target_version = packaging.version.parse(spec[1])
if spec[0] == ">=":
self.versions = [v for v in self.versions if v >= target_version]
elif spec[0] == ">":
self.versions = [v for v in self.versions if v > target_version]
elif spec[0] == "!=":
self.versions = [v for v in self.versions if v != target_version]
elif spec[0] == "<":
self.versions = [v for v in self.versions if v < target_version]
elif spec[0] == "<=":
self.versions = [v for v in self.versions if v <= target_version]
elif spec[0] == "==":
self.versions = [v for v in self.versions if v == target_version]
elif spec[0] == "~=":
self.versions = [
v
for v in self.versions
if str(v).startswith(str(target_version) + ".")
]
else:
raise PyPIError(f"Unknown constraint operator {spec[0]}")
self.versions = [
v
for v in self.versions
if packaging.specifiers.Specifier(f"{spec[0]}{spec[1]}").contains(
str(v)
)
]

if len(self.versions) != current_length:
logging.debug(
Expand Down
1 change: 1 addition & 0 deletions tests/tests_e3/python/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_wheel():
pypi.add_requirement("src1>0.5.0")
pypi.add_requirement("src1>=0.6.0")
pypi.add_requirement("src1!=0.4.2")
pypi.add_requirement("src1~=1.0.0")
assert len(pypi.file_closure()) == 2
assert len(pypi.closure_as_requirements()) == 2
assert len(pypi.closure()) == 2
Expand Down

0 comments on commit e41bfab

Please sign in to comment.