Skip to content

Commit 803ccbd

Browse files
committed
refactor: pull out dependency_constraint
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 8e2cc78 commit 803ccbd

File tree

5 files changed

+44
-40
lines changed

5 files changed

+44
-40
lines changed

cibuildwheel/platforms/ios.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from ..logger import log
2525
from ..options import Options
2626
from ..selector import BuildSelector
27-
from ..typing import PathOrStr
2827
from ..util import resources
2928
from ..util.cmd import call, shell
3029
from ..util.file import (
@@ -39,7 +38,7 @@
3938
find_compatible_wheel,
4039
get_pip_version,
4140
)
42-
from ..venv import virtualenv
41+
from ..venv import constraint_flags, virtualenv
4342
from .macos import install_cpython as install_build_cpython
4443

4544

@@ -151,7 +150,7 @@ def cross_virtualenv(
151150
multiarch: str,
152151
build_python: Path,
153152
venv_path: Path,
154-
dependency_constraint_flags: Sequence[PathOrStr],
153+
dependency_constraint: Path | None,
155154
xbuild_tools: Sequence[str] | None,
156155
) -> dict[str, str]:
157156
"""Create a cross-compilation virtual environment.
@@ -188,7 +187,7 @@ def cross_virtualenv(
188187
py_version,
189188
build_python,
190189
venv_path,
191-
dependency_constraint_flags,
190+
dependency_constraint,
192191
use_uv=False,
193192
)
194193

@@ -279,7 +278,7 @@ def setup_python(
279278
tmp: Path,
280279
*,
281280
python_configuration: PythonConfiguration,
282-
dependency_constraint_flags: Sequence[PathOrStr],
281+
dependency_constraint: Path | None,
283282
environment: ParsedEnvironment,
284283
build_frontend: BuildFrontendName,
285284
xbuild_tools: Sequence[str] | None,
@@ -327,14 +326,16 @@ def setup_python(
327326

328327
log.step("Creating cross build environment...")
329328

329+
dependency_constraint_flags = constraint_flags(dependency_constraint)
330+
330331
venv_path = tmp / "venv"
331332
env = cross_virtualenv(
332333
py_version=python_configuration.version,
333334
target_python=target_python,
334335
multiarch=python_configuration.multiarch,
335336
build_python=build_python,
336337
venv_path=venv_path,
337-
dependency_constraint_flags=dependency_constraint_flags,
338+
dependency_constraint=dependency_constraint,
338339
xbuild_tools=xbuild_tools,
339340
)
340341
venv_bin_path = venv_path / "bin"
@@ -453,14 +454,11 @@ def build(options: Options, tmp_path: Path) -> None:
453454
constraints_path = build_options.dependency_constraints.get_for_python_version(
454455
version=config.version, tmp_dir=identifier_tmp_dir
455456
)
456-
dependency_constraint_flags: Sequence[PathOrStr] = (
457-
["-c", constraints_path] if constraints_path else []
458-
)
459457

460458
target_install_path, env = setup_python(
461459
identifier_tmp_dir / "build",
462460
python_configuration=config,
463-
dependency_constraint_flags=dependency_constraint_flags,
461+
dependency_constraint=constraints_path,
464462
environment=build_options.environment,
465463
build_frontend=build_frontend.name,
466464
xbuild_tools=build_options.xbuild_tools,

cibuildwheel/platforms/macos.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import subprocess
88
import sys
99
import typing
10-
from collections.abc import Sequence, Set
10+
from collections.abc import Set
1111
from dataclasses import dataclass
1212
from pathlib import Path
1313
from typing import Literal, assert_never
@@ -23,7 +23,6 @@
2323
from ..logger import log
2424
from ..options import Options
2525
from ..selector import BuildSelector
26-
from ..typing import PathOrStr
2726
from ..util import resources
2827
from ..util.cmd import call, shell
2928
from ..util.file import (
@@ -34,7 +33,7 @@
3433
)
3534
from ..util.helpers import prepare_command, unwrap
3635
from ..util.packaging import combine_constraints, find_compatible_wheel, get_pip_version
37-
from ..venv import find_uv, virtualenv
36+
from ..venv import constraint_flags, find_uv, virtualenv
3837

3938

4039
@functools.cache
@@ -195,7 +194,7 @@ def install_pypy(tmp: Path, url: str) -> Path:
195194
def setup_python(
196195
tmp: Path,
197196
python_configuration: PythonConfiguration,
198-
dependency_constraint_flags: Sequence[PathOrStr],
197+
dependency_constraint: Path | None,
199198
environment: ParsedEnvironment,
200199
build_frontend: BuildFrontendName,
201200
) -> tuple[Path, dict[str, str]]:
@@ -220,13 +219,15 @@ def setup_python(
220219
f"{base_python.name} not found, has {list(base_python.parent.iterdir())}"
221220
)
222221

222+
dependency_constraint_flags = constraint_flags(dependency_constraint)
223+
223224
log.step("Setting up build environment...")
224225
venv_path = tmp / "venv"
225226
env = virtualenv(
226227
python_configuration.version,
227228
base_python,
228229
venv_path,
229-
dependency_constraint_flags,
230+
dependency_constraint,
230231
use_uv=use_uv,
231232
)
232233
venv_bin_path = venv_path / "bin"
@@ -427,14 +428,12 @@ def build(options: Options, tmp_path: Path) -> None:
427428
constraints_path = build_options.dependency_constraints.get_for_python_version(
428429
version=config.version, tmp_dir=identifier_tmp_dir
429430
)
430-
dependency_constraint_flags: Sequence[PathOrStr] = (
431-
["-c", constraints_path] if constraints_path else []
432-
)
431+
dependency_constraint_flags = constraint_flags(constraints_path)
433432

434433
base_python, env = setup_python(
435434
identifier_tmp_dir / "build",
436435
config,
437-
dependency_constraint_flags,
436+
constraints_path,
438437
build_options.environment,
439438
build_frontend.name,
440439
)

cibuildwheel/platforms/pyodide.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def setup_python(
152152

153153
log.step("Setting up build environment...")
154154
venv_path = tmp / "venv"
155-
env = virtualenv(python_configuration.version, base_python, venv_path, [], use_uv=False)
155+
env = virtualenv(python_configuration.version, base_python, venv_path, None, use_uv=False)
156156
venv_bin_path = venv_path / "bin"
157157
assert venv_bin_path.exists()
158158
env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
@@ -269,8 +269,8 @@ def build(options: Options, tmp_path: Path) -> None:
269269
constraints_path = build_options.dependency_constraints.get_for_python_version(
270270
version=config.version, variant="pyodide", tmp_dir=identifier_tmp_dir
271271
)
272-
dependency_constraint_flags: Sequence[PathOrStr] = (
273-
["-c", constraints_path] if constraints_path else []
272+
dependency_constraint_flags = (
273+
["-c", constraints_path.as_uri()] if constraints_path else []
274274
)
275275

276276
env = setup_python(

cibuildwheel/platforms/windows.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shutil
44
import subprocess
55
import textwrap
6-
from collections.abc import MutableMapping, Sequence, Set
6+
from collections.abc import MutableMapping, Set
77
from dataclasses import dataclass
88
from functools import cache
99
from pathlib import Path
@@ -18,13 +18,12 @@
1818
from ..logger import log
1919
from ..options import Options
2020
from ..selector import BuildSelector
21-
from ..typing import PathOrStr
2221
from ..util import resources
2322
from ..util.cmd import call, shell
2423
from ..util.file import CIBW_CACHE_PATH, copy_test_sources, download, extract_zip, move_file
2524
from ..util.helpers import prepare_command, unwrap
2625
from ..util.packaging import combine_constraints, find_compatible_wheel, get_pip_version
27-
from ..venv import find_uv, virtualenv
26+
from ..venv import constraint_flags, find_uv, virtualenv
2827

2928

3029
def get_nuget_args(
@@ -219,7 +218,7 @@ def can_use_uv(python_configuration: PythonConfiguration) -> bool:
219218
def setup_python(
220219
tmp: Path,
221220
python_configuration: PythonConfiguration,
222-
dependency_constraint_flags: Sequence[PathOrStr],
221+
dependency_constraint: Path | None,
223222
environment: ParsedEnvironment,
224223
build_frontend: BuildFrontendName,
225224
) -> tuple[Path, dict[str, str]]:
@@ -248,7 +247,7 @@ def setup_python(
248247
if build_frontend == "build[uv]" and not can_use_uv(python_configuration):
249248
build_frontend = "build"
250249

251-
use_uv = build_frontend == "build[uv]"
250+
use_uv = build_frontend in {"build[uv]", "uv"}
252251
uv_path = find_uv()
253252

254253
log.step("Setting up build environment...")
@@ -257,10 +256,12 @@ def setup_python(
257256
python_configuration.version,
258257
base_python,
259258
venv_path,
260-
dependency_constraint_flags,
259+
dependency_constraint,
261260
use_uv=use_uv,
262261
)
263262

263+
dependency_constraint_flags = constraint_flags(dependency_constraint)
264+
264265
# set up environment variables for run_with_env
265266
env["PYTHON_VERSION"] = python_configuration.version
266267
env["PYTHON_ARCH"] = python_configuration.arch
@@ -276,7 +277,7 @@ def setup_python(
276277
"install",
277278
"--upgrade",
278279
"pip",
279-
*dependency_constraint_flags,
280+
*constraint_flags(dependency_constraint),
280281
env=env,
281282
cwd=venv_path,
282283
)
@@ -370,15 +371,15 @@ def build(options: Options, tmp_path: Path) -> None:
370371
version=config.version,
371372
tmp_dir=identifier_tmp_dir,
372373
)
373-
dependency_constraint_flags: Sequence[PathOrStr] = (
374-
["-c", constraints_path] if constraints_path else []
374+
dependency_constraint_flags = (
375+
["-c", constraints_path.as_uri()] if constraints_path else []
375376
)
376377

377378
# install Python
378379
base_python, env = setup_python(
379380
identifier_tmp_dir / "build",
380381
config,
381-
dependency_constraint_flags,
382+
constraints_path,
382383
build_options.environment,
383384
build_frontend.name,
384385
)

cibuildwheel/venv.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from packaging.requirements import InvalidRequirement, Requirement
1313
from packaging.version import Version
1414

15-
from .typing import PathOrStr
1615
from .util import resources
1716
from .util.cmd import call
1817
from .util.file import CIBW_CACHE_PATH, download
@@ -36,8 +35,18 @@ def _ensure_virtualenv(version: str) -> Path:
3635
return path
3736

3837

38+
def constraint_flags(
39+
dependency_constraint: Path | None,
40+
) -> Sequence[str]:
41+
"""
42+
Returns the flags to pass to pip for the given dependency constraint.
43+
"""
44+
45+
return ["-c", dependency_constraint.as_uri()] if dependency_constraint else []
46+
47+
3948
def _parse_pip_constraint_for_virtualenv(
40-
dependency_constraint_flags: Sequence[PathOrStr],
49+
constraint_path: Path | None,
4150
) -> str:
4251
"""
4352
Parses the constraints file referenced by `dependency_constraint_flags` and returns a dict where
@@ -48,10 +57,7 @@ def _parse_pip_constraint_for_virtualenv(
4857
If it can't get an exact version, the real constraint will be handled by the
4958
{macos|windows}.setup_python function.
5059
"""
51-
assert len(dependency_constraint_flags) in {0, 2}
52-
if len(dependency_constraint_flags) == 2:
53-
assert dependency_constraint_flags[0] == "-c"
54-
constraint_path = Path(dependency_constraint_flags[1])
60+
if constraint_path:
5561
assert constraint_path.exists()
5662
with constraint_path.open(encoding="utf-8") as constraint_file:
5763
for line_ in constraint_file:
@@ -84,7 +90,7 @@ def virtualenv(
8490
version: str,
8591
python: Path,
8692
venv_path: Path,
87-
dependency_constraint_flags: Sequence[PathOrStr],
93+
dependency_constraint: Path | None,
8894
*,
8995
use_uv: bool,
9096
) -> dict[str, str]:
@@ -103,7 +109,7 @@ def virtualenv(
103109
call("uv", "venv", venv_path, "--python", python)
104110
else:
105111
virtualenv_app = _ensure_virtualenv(version)
106-
pip_constraint = _parse_pip_constraint_for_virtualenv(dependency_constraint_flags)
112+
pip_constraint = _parse_pip_constraint_for_virtualenv(dependency_constraint)
107113
additional_flags = [f"--pip={pip_constraint}", "--no-setuptools", "--no-wheel"]
108114

109115
# Using symlinks to pre-installed seed packages is really the fastest way to get a virtual

0 commit comments

Comments
 (0)