|
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: ships abi3 wheels; forcing sdist hits PyO3 "max supported Python" until upgraded |
| 27 | +_FORCE_INTERPRETER_BINARY_SKIP_EXACT = frozenset( |
| 28 | + { |
| 29 | + # cryptography: ships abi3 wheels; forcing sdist hits PyO3 "max supported Python" until upgraded |
| 30 | + # (use PYO3_USE_ABI3_FORWARD_COMPATIBILITY in CI if you must rebuild from source). |
| 31 | + # canonicalize_name("cryptography"), |
| 32 | + canonicalize_name("protobuf"), |
| 33 | + canonicalize_name("ruamel.yaml.clib"), |
| 34 | + } |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +def _force_interpreter_skip_package(canonical_dist_name: str) -> bool: |
| 39 | + if canonical_dist_name in _FORCE_INTERPRETER_BINARY_SKIP_EXACT: |
| 40 | + return True |
| 41 | + # PyObjC meta and framework bindings (pyobjc-framework-corebluetooth, etc.) |
| 42 | + return canonical_dist_name == "pyobjc" or canonical_dist_name.startswith("pyobjc-") |
| 43 | + |
| 44 | + |
| 45 | +def _force_interpreter_no_binary_args(requirement_line: str) -> list[str]: |
| 46 | + """Return pip --no-binary for this package so pip cannot reuse e.g. cp311-abi3 wheels on 3.13.""" |
| 47 | + line = requirement_line.strip() |
| 48 | + if not line: |
| 49 | + return [] |
| 50 | + try: |
| 51 | + req = Requirement(line) |
| 52 | + except InvalidRequirement: |
| 53 | + return [] |
| 54 | + if _force_interpreter_skip_package(canonicalize_name(req.name)): |
| 55 | + return [] |
| 56 | + return ["--no-binary", req.name] |
| 57 | + |
| 58 | + |
| 59 | +def _apply_force_interpreter_binary(cli_flag: bool) -> bool: |
| 60 | + """Linux/macOS only: forcing sdist builds for cryptography etc. is unreliable on Windows CI.""" |
| 61 | + return cli_flag and platform.system() != "Windows" |
| 62 | + |
| 63 | + |
16 | 64 | parser = argparse.ArgumentParser(description="Process build arguments.") |
17 | 65 | parser.add_argument( |
18 | 66 | "requirements_path", |
|
36 | 84 | action="store_true", |
37 | 85 | help="CI exclude-tests mode: fail if all wheels succeed (expect some to fail, e.g. excluded packages)", |
38 | 86 | ) |
| 87 | +parser.add_argument( |
| 88 | + "--force-interpreter-binary", |
| 89 | + action="store_true", |
| 90 | + help=( |
| 91 | + "For each requirement, pass --no-binary <pkg> so pip builds a wheel for the current " |
| 92 | + "interpreter instead of reusing a compatible abi3 / older cpXY wheel from --find-links. " |
| 93 | + "Ignored on Windows (source builds for e.g. cryptography are not used in CI there). " |
| 94 | + "Some packages are always skipped (e.g. cryptography, protobuf, PyObjC, ruamel.yaml.clib)." |
| 95 | + ), |
| 96 | +) |
39 | 97 |
|
40 | 98 | args = parser.parse_args() |
41 | 99 |
|
|
55 | 113 | raise SystemExit(f"Python version dependent requirements directory or file not found ({e})") |
56 | 114 |
|
57 | 115 | for requirement in requirements: |
| 116 | + requirement = requirement.strip() |
| 117 | + if not requirement or requirement.startswith("#"): |
| 118 | + continue |
58 | 119 | # Get no-binary args for packages that should be built from source |
59 | 120 | no_binary_args = get_no_binary_args(requirement) |
| 121 | + force_interpreter_args = ( |
| 122 | + _force_interpreter_no_binary_args(requirement) |
| 123 | + if _apply_force_interpreter_binary(args.force_interpreter_binary) |
| 124 | + else [] |
| 125 | + ) |
60 | 126 |
|
61 | 127 | out = subprocess.run( |
62 | 128 | [ |
63 | 129 | f"{sys.executable}", |
64 | 130 | "-m", |
65 | 131 | "pip", |
66 | 132 | "wheel", |
67 | | - f"{requirement}", |
| 133 | + requirement, |
68 | 134 | "--find-links", |
69 | 135 | "downloaded_wheels", |
70 | 136 | "--wheel-dir", |
71 | 137 | "downloaded_wheels", |
72 | 138 | ] |
73 | | - + no_binary_args, |
| 139 | + + no_binary_args |
| 140 | + + force_interpreter_args, |
74 | 141 | stdout=subprocess.PIPE, |
75 | 142 | stderr=subprocess.PIPE, |
76 | 143 | ) |
|
100 | 167 | for requirement in in_requirements: |
101 | 168 | # Get no-binary args for packages that should be built from source |
102 | 169 | no_binary_args = get_no_binary_args(requirement) |
| 170 | + force_interpreter_args = ( |
| 171 | + _force_interpreter_no_binary_args(requirement) |
| 172 | + if _apply_force_interpreter_binary(args.force_interpreter_binary) |
| 173 | + else [] |
| 174 | + ) |
103 | 175 |
|
104 | 176 | out = subprocess.run( |
105 | 177 | [ |
|
113 | 185 | "--wheel-dir", |
114 | 186 | "downloaded_wheels", |
115 | 187 | ] |
116 | | - + no_binary_args, |
| 188 | + + no_binary_args |
| 189 | + + force_interpreter_args, |
117 | 190 | stdout=subprocess.PIPE, |
118 | 191 | stderr=subprocess.PIPE, |
119 | 192 | ) |
|
0 commit comments