Skip to content

Commit

Permalink
fix: respect constraints when building with pip
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeut committed May 10, 2024
1 parent 744d37e commit adec6c3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 53 deletions.
27 changes: 13 additions & 14 deletions cibuildwheel/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,18 @@ def build(options: Options, tmp_path: Path) -> None:
)
extra_flags += build_frontend.args

build_env = env.copy()
build_env["VIRTUALENV_PIP"] = get_pip_version(env)
if build_options.dependency_constraints:
constraint_path = build_options.dependency_constraints.get_for_python_version(
config.version
)
user_constraints = build_env.get("PIP_CONSTRAINT")
our_constraints = constraint_path.as_uri()
build_env["PIP_CONSTRAINT"] = " ".join(
c for c in [user_constraints, our_constraints] if c
)

if build_frontend.name == "pip":
extra_flags += get_build_verbosity_extra_flags(build_options.build_verbosity)
# Path.resolve() is needed. Without it pip wheel may try to fetch package from pypi.org
Expand All @@ -394,25 +406,12 @@ def build(options: Options, tmp_path: Path) -> None:
f"--wheel-dir={built_wheel_dir}",
"--no-deps",
*extra_flags,
env=env,
env=build_env,
)
elif build_frontend.name == "build":
if not 0 <= build_options.build_verbosity < 2:
msg = f"build_verbosity {build_options.build_verbosity} is not supported for build frontend. Ignoring."
log.warning(msg)
build_env = env.copy()
if build_options.dependency_constraints:
constraint_path = (
build_options.dependency_constraints.get_for_python_version(
config.version
)
)
user_constraints = build_env.get("PIP_CONSTRAINT")
our_constraints = constraint_path.as_uri()
build_env["PIP_CONSTRAINT"] = " ".join(
c for c in [user_constraints, our_constraints] if c
)
build_env["VIRTUALENV_PIP"] = get_pip_version(env)
call(
"python",
"-m",
Expand Down
68 changes: 33 additions & 35 deletions cibuildwheel/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,28 @@ def build(options: Options, tmp_path: Path) -> None:
)
extra_flags += build_frontend.args

build_env = env.copy()
build_env["VIRTUALENV_PIP"] = get_pip_version(env)
if build_options.dependency_constraints:
constraints_path = build_options.dependency_constraints.get_for_python_version(
config.version
)
# Bug in pip <= 21.1.3 - we can't have a space in the
# constraints file, and pip doesn't support drive letters
# in uhi. After probably pip 21.2, we can use uri. For
# now, use a temporary file.
if " " in str(constraints_path):
assert " " not in str(identifier_tmp_dir)
tmp_file = identifier_tmp_dir / "constraints.txt"
tmp_file.write_bytes(constraints_path.read_bytes())
constraints_path = tmp_file

our_constraints = str(constraints_path)
user_constraints = build_env.get("PIP_CONSTRAINT")
build_env["PIP_CONSTRAINT"] = " ".join(
c for c in [our_constraints, user_constraints] if c
)

if build_frontend.name == "pip":
extra_flags += get_build_verbosity_extra_flags(build_options.build_verbosity)
# Path.resolve() is needed. Without it pip wheel may try to fetch package from pypi.org
Expand All @@ -420,46 +442,22 @@ def build(options: Options, tmp_path: Path) -> None:
f"--wheel-dir={built_wheel_dir}",
"--no-deps",
*extra_flags,
env=env,
env=build_env,
)
elif build_frontend.name == "build":
if not 0 <= build_options.build_verbosity < 2:
msg = f"build_verbosity {build_options.build_verbosity} is not supported for build frontend. Ignoring."
log.warning(msg)
build_env = env.copy()
if build_options.dependency_constraints:
constraints_path = (
build_options.dependency_constraints.get_for_python_version(
config.version
)
)
# Bug in pip <= 21.1.3 - we can't have a space in the
# constraints file, and pip doesn't support drive letters
# in uhi. After probably pip 21.2, we can use uri. For
# now, use a temporary file.
if " " in str(constraints_path):
assert " " not in str(identifier_tmp_dir)
tmp_file = identifier_tmp_dir / "constraints.txt"
tmp_file.write_bytes(constraints_path.read_bytes())
constraints_path = tmp_file

our_constraints = str(constraints_path)
user_constraints = build_env.get("PIP_CONSTRAINT")
build_env["PIP_CONSTRAINT"] = " ".join(
c for c in [our_constraints, user_constraints] if c
)

build_env["VIRTUALENV_PIP"] = get_pip_version(env)
call(
"python",
"-m",
"build",
build_options.package_dir,
"--wheel",
f"--outdir={built_wheel_dir}",
*extra_flags,
env=build_env,
)
call(
"python",
"-m",
"build",
build_options.package_dir,
"--wheel",
f"--outdir={built_wheel_dir}",
*extra_flags,
env=build_env,
)
else:
assert_never(build_frontend)

Expand Down
11 changes: 7 additions & 4 deletions test/test_dependency_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
r"""
import subprocess
import os
import sys
versions_output_text = subprocess.check_output(
['pip', 'freeze', '--all', '-qq'],
[sys.executable, '-m', 'pip', 'freeze', '--all', '-qq'],
universal_newlines=True,
)
versions = versions_output_text.strip().splitlines()
Expand All @@ -36,6 +37,11 @@
)
)

project_with_expected_version_checks.files["pyproject.toml"] = r"""
[build-system]
requires = ["setuptools", "pip"]
build-backend = "setuptools.build_meta"
"""

VERSION_REGEX = r"([\w-]+)==([^\s]+)"

Expand All @@ -61,9 +67,6 @@ def test_pinned_versions(tmp_path, python_version, build_frontend_env):
pytest.skip("macOS arm64 does not support Python 3.6")
constraint_filename = "constraints-python36.txt"
build_pattern = "[cp]p36-*"
elif python_version == "3.7":
constraint_filename = "constraints-python37.txt"
build_pattern = "[cp]p37-*"
elif python_version == "3.8":
constraint_filename = "constraints-python38.txt"
build_pattern = "[cp]p38-*"
Expand Down

0 comments on commit adec6c3

Please sign in to comment.