|
3 | 3 | # |
4 | 4 | # SPDX-License-Identifier: Apache-2.0 |
5 | 5 | # |
| 6 | +from __future__ import annotations |
| 7 | + |
6 | 8 | import argparse |
7 | 9 | import os |
| 10 | +import platform |
8 | 11 | import subprocess |
9 | 12 | import sys |
10 | 13 |
|
11 | 14 | from colorama import Fore |
| 15 | +from packaging.requirements import InvalidRequirement |
| 16 | +from packaging.requirements import Requirement |
| 17 | +from packaging.utils import canonicalize_name |
12 | 18 |
|
13 | 19 | from _helper_functions import get_no_binary_args |
14 | 20 | from _helper_functions import print_color |
15 | 21 |
|
| 22 | +# Do not pass --no-binary for these in --force-interpreter-binary mode: |
| 23 | +# - sdists whose legacy setup breaks under PEP 517 isolation (pkg_resources in isolated env). |
| 24 | +# - sdists that fail to compile on CI when a usable wheel exists (e.g. ruamel.yaml.clib + clang). |
| 25 | +# - PyObjC: all pyobjc / pyobjc-framework-* use pyobjc_setup.py + pkg_resources (macOS). |
| 26 | +# - cryptography: abi3 wheels; avoid PyO3 max-Python / heavy Rust rebuilds in dependent jobs. |
| 27 | +# - pydantic-core: maturin + jiter + PyO3 can fail from sdist on some CI combos (e.g. ARM64 3.9: |
| 28 | +# jiter vs pyo3-ffi PyUnicode_* / extract API). Prefer compatible wheels from find-links or PyPI. |
| 29 | +_FORCE_INTERPRETER_BINARY_SKIP_EXACT = frozenset( |
| 30 | + { |
| 31 | + canonicalize_name("cryptography"), |
| 32 | + canonicalize_name("pydantic-core"), |
| 33 | + canonicalize_name("protobuf"), |
| 34 | + canonicalize_name("ruamel.yaml.clib"), |
| 35 | + } |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +def _force_interpreter_skip_package(canonical_dist_name: str) -> bool: |
| 40 | + if canonical_dist_name in _FORCE_INTERPRETER_BINARY_SKIP_EXACT: |
| 41 | + return True |
| 42 | + # PyObjC meta and framework bindings (pyobjc-framework-corebluetooth, etc.) |
| 43 | + return canonical_dist_name == "pyobjc" or canonical_dist_name.startswith("pyobjc-") |
| 44 | + |
| 45 | + |
| 46 | +def _force_interpreter_no_binary_args(requirement_line: str) -> list[str]: |
| 47 | + """Return pip --no-binary for this package so pip cannot reuse e.g. cp311-abi3 wheels on 3.13.""" |
| 48 | + line = requirement_line.strip() |
| 49 | + if not line: |
| 50 | + return [] |
| 51 | + try: |
| 52 | + req = Requirement(line) |
| 53 | + except InvalidRequirement: |
| 54 | + return [] |
| 55 | + if _force_interpreter_skip_package(canonicalize_name(req.name)): |
| 56 | + return [] |
| 57 | + return ["--no-binary", req.name] |
| 58 | + |
| 59 | + |
| 60 | +def _apply_force_interpreter_binary(cli_flag: bool) -> bool: |
| 61 | + """Linux/macOS only: forcing sdist builds for cryptography etc. is unreliable on Windows CI.""" |
| 62 | + return cli_flag and platform.system() != "Windows" |
| 63 | + |
| 64 | + |
16 | 65 | parser = argparse.ArgumentParser(description="Process build arguments.") |
17 | 66 | parser.add_argument( |
18 | 67 | "requirements_path", |
|
36 | 85 | action="store_true", |
37 | 86 | help="CI exclude-tests mode: fail if all wheels succeed (expect some to fail, e.g. excluded packages)", |
38 | 87 | ) |
| 88 | +parser.add_argument( |
| 89 | + "--force-interpreter-binary", |
| 90 | + action="store_true", |
| 91 | + help=( |
| 92 | + "For each requirement, pass --no-binary <pkg> so pip builds a wheel for the current " |
| 93 | + "interpreter instead of reusing a compatible abi3 / older cpXY wheel from --find-links. " |
| 94 | + "Ignored on Windows (source builds for e.g. cryptography are not used in CI there). " |
| 95 | + "Some packages are always skipped (e.g. cryptography, pydantic-core, protobuf, PyObjC, ruamel.yaml.clib)." |
| 96 | + ), |
| 97 | +) |
39 | 98 |
|
40 | 99 | args = parser.parse_args() |
41 | 100 |
|
|
55 | 114 | raise SystemExit(f"Python version dependent requirements directory or file not found ({e})") |
56 | 115 |
|
57 | 116 | for requirement in requirements: |
| 117 | + requirement = requirement.strip() |
| 118 | + if not requirement or requirement.startswith("#"): |
| 119 | + continue |
58 | 120 | # Get no-binary args for packages that should be built from source |
59 | 121 | no_binary_args = get_no_binary_args(requirement) |
| 122 | + force_interpreter_args = ( |
| 123 | + _force_interpreter_no_binary_args(requirement) |
| 124 | + if _apply_force_interpreter_binary(args.force_interpreter_binary) |
| 125 | + else [] |
| 126 | + ) |
60 | 127 |
|
61 | 128 | out = subprocess.run( |
62 | 129 | [ |
63 | 130 | f"{sys.executable}", |
64 | 131 | "-m", |
65 | 132 | "pip", |
66 | 133 | "wheel", |
67 | | - f"{requirement}", |
| 134 | + requirement, |
68 | 135 | "--find-links", |
69 | 136 | "downloaded_wheels", |
70 | 137 | "--wheel-dir", |
71 | 138 | "downloaded_wheels", |
72 | 139 | ] |
73 | | - + no_binary_args, |
| 140 | + + no_binary_args |
| 141 | + + force_interpreter_args, |
74 | 142 | stdout=subprocess.PIPE, |
75 | 143 | stderr=subprocess.PIPE, |
76 | 144 | ) |
|
100 | 168 | for requirement in in_requirements: |
101 | 169 | # Get no-binary args for packages that should be built from source |
102 | 170 | no_binary_args = get_no_binary_args(requirement) |
| 171 | + force_interpreter_args = ( |
| 172 | + _force_interpreter_no_binary_args(requirement) |
| 173 | + if _apply_force_interpreter_binary(args.force_interpreter_binary) |
| 174 | + else [] |
| 175 | + ) |
103 | 176 |
|
104 | 177 | out = subprocess.run( |
105 | 178 | [ |
|
113 | 186 | "--wheel-dir", |
114 | 187 | "downloaded_wheels", |
115 | 188 | ] |
116 | | - + no_binary_args, |
| 189 | + + no_binary_args |
| 190 | + + force_interpreter_args, |
117 | 191 | stdout=subprocess.PIPE, |
118 | 192 | stderr=subprocess.PIPE, |
119 | 193 | ) |
|
0 commit comments