Skip to content

Commit

Permalink
Merge pull request #627 from pierretr/pypi-req-star
Browse files Browse the repository at this point in the history
Handle star ending requirements in pypi add_constraint
  • Loading branch information
pierretr authored Sep 6, 2023
2 parents 69091db + b0f813e commit 83bfeaf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 19 deletions.
43 changes: 25 additions & 18 deletions src/e3/python/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,27 +332,34 @@ def add_constraint(self, requirement: Requirement) -> None:

# Apply version constraints
for spec in requirement.specs:
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] == "~=":
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 str(v).startswith(str(target_version) + ".")
v for v in self.versions if not str(v).startswith(spec[1][:-2])
]
else:
raise PyPIError(f"Unknown constraint operator {spec[0]}")
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]}")

if len(self.versions) != current_length:
logging.debug(
Expand Down
47 changes: 46 additions & 1 deletion tests/tests_e3/python/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from e3.os.process import Run
from e3.sys import python_script
from e3.fs import mkdir
from e3.python.pypi import PyPIClosure
from e3.python.pypi import PyPIClosure, PyPIError

import yaml
import os
import pytest


def generate_py_pkg_source(
Expand Down Expand Up @@ -103,3 +105,46 @@ def test_pypi_closure_tool():
"src1-1.0.0-py3-none-any.whl",
"src2-1.0.0-py3-none-any.whl",
}


def test_star_requirements():
"""Test package requirements ending with * with != operator."""
wheel1 = generate_py_pkg_source("src1", version="1.0.4")
assert os.path.isfile(wheel1.path)
assert not wheel1.requirements

wheel2 = generate_py_pkg_source("src2", requires=["src1!=1.0.*"])
assert os.path.isfile(wheel2.path)
assert len(wheel2.requirements) == 1

wheel3 = generate_py_pkg_source("src1", version="1.1.4")
assert os.path.isfile(wheel3.path)
assert not wheel3.requirements

mkdir(".cache")

with PyPIClosure(
python3_version=10,
platforms=[
"x86_64-linux",
],
cache_dir=".cache",
cache_file=".pypi.cache",
) as pypi:
pypi.add_wheel(wheel1.path)
pypi.add_wheel(wheel2.path)
with pytest.raises(PyPIError, match="Cannot satisfy constraint src1!=1.0.*"):
pypi.add_requirement("src2==1.0.0")

with PyPIClosure(
python3_version=10,
platforms=[
"x86_64-linux",
],
cache_dir=".cache",
cache_file=".pypi.cache",
) as pypi:
pypi.add_wheel(wheel2.path)
pypi.add_wheel(wheel3.path)
pypi.add_requirement("src2==1.0.0")
assert len(pypi.closure()) == 2

0 comments on commit 83bfeaf

Please sign in to comment.