Skip to content

Commit ac57b94

Browse files
authored
refactor: pull out pip checks (#735)
Signed-off-by: Henry Schreiner <[email protected]>
1 parent e6969d1 commit ac57b94

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

Diff for: src/build/env.py

+34-21
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,38 @@ def _should_use_virtualenv() -> bool:
5858
)
5959

6060

61+
def _minimum_pip_version() -> str:
62+
if platform.system() == 'Darwin' and int(platform.mac_ver()[0].split('.')[0]) >= 11:
63+
# macOS 11+ name scheme change requires 20.3. Intel macOS 11.0 can be
64+
# told to report 10.16 for backwards compatibility; but that also fixes
65+
# earlier versions of pip so this is only needed for 11+.
66+
is_apple_silicon_python = platform.machine() != 'x86_64'
67+
return '21.0.1' if is_apple_silicon_python else '20.3.0'
68+
69+
# PEP-517 and manylinux1 was first implemented in 19.1
70+
return '19.1.0'
71+
72+
73+
def _has_valid_pip(purelib: str) -> bool:
74+
"""
75+
Given a path, see if Pip is present and return True if the version is
76+
sufficient for build, False if it is not.
77+
"""
78+
79+
import packaging.version
80+
81+
if sys.version_info < (3, 8):
82+
import importlib_metadata as metadata
83+
else:
84+
from importlib import metadata
85+
86+
pip_distribution = next(iter(metadata.distributions(name='pip', path=[purelib])))
87+
88+
current_pip_version = packaging.version.Version(pip_distribution.version)
89+
90+
return current_pip_version >= packaging.version.Version(_minimum_pip_version())
91+
92+
6193
def _subprocess(cmd: list[str]) -> None:
6294
"""Invoke subprocess and output stdout and stderr if it fails."""
6395
try:
@@ -203,13 +235,6 @@ def _create_isolated_env_venv(path: str) -> tuple[str, str]:
203235
"""
204236
import venv
205237

206-
import packaging.version
207-
208-
if sys.version_info < (3, 8):
209-
import importlib_metadata as metadata
210-
else:
211-
from importlib import metadata
212-
213238
symlinks = _fs_supports_symlink()
214239
try:
215240
with warnings.catch_warnings():
@@ -222,20 +247,8 @@ def _create_isolated_env_venv(path: str) -> tuple[str, str]:
222247
executable, script_dir, purelib = _find_executable_and_scripts(path)
223248

224249
# Get the version of pip in the environment
225-
pip_distribution = next(iter(metadata.distributions(name='pip', path=[purelib])))
226-
current_pip_version = packaging.version.Version(pip_distribution.version)
227-
228-
if platform.system() == 'Darwin' and int(platform.mac_ver()[0].split('.')[0]) >= 11:
229-
# macOS 11+ name scheme change requires 20.3. Intel macOS 11.0 can be told to report 10.16 for backwards
230-
# compatibility; but that also fixes earlier versions of pip so this is only needed for 11+.
231-
is_apple_silicon_python = platform.machine() != 'x86_64'
232-
minimum_pip_version = '21.0.1' if is_apple_silicon_python else '20.3.0'
233-
else:
234-
# PEP-517 and manylinux1 was first implemented in 19.1
235-
minimum_pip_version = '19.1.0'
236-
237-
if current_pip_version < packaging.version.Version(minimum_pip_version):
238-
_subprocess([executable, '-m', 'pip', 'install', f'pip>={minimum_pip_version}'])
250+
if not _has_valid_pip(purelib):
251+
_subprocess([executable, '-m', 'pip', 'install', f'pip>={_minimum_pip_version()}'])
239252

240253
# Avoid the setuptools from ensurepip to break the isolation
241254
_subprocess([executable, '-m', 'pip', 'uninstall', 'setuptools', '-y'])

0 commit comments

Comments
 (0)