Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: pull out pip checks #735

Merged
merged 1 commit into from Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 34 additions & 21 deletions src/build/env.py
Expand Up @@ -58,6 +58,38 @@ def _should_use_virtualenv() -> bool:
)


def _minimum_pip_version() -> str:
if platform.system() == 'Darwin' and int(platform.mac_ver()[0].split('.')[0]) >= 11:
# macOS 11+ name scheme change requires 20.3. Intel macOS 11.0 can be
# told to report 10.16 for backwards compatibility; but that also fixes
# earlier versions of pip so this is only needed for 11+.
is_apple_silicon_python = platform.machine() != 'x86_64'
return '21.0.1' if is_apple_silicon_python else '20.3.0'

# PEP-517 and manylinux1 was first implemented in 19.1
return '19.1.0'


def _has_valid_pip(purelib: str) -> bool:
"""
Given a path, see if Pip is present and return True if the version is
sufficient for build, False if it is not.
"""

import packaging.version

if sys.version_info < (3, 8):
import importlib_metadata as metadata
else:
from importlib import metadata

pip_distribution = next(iter(metadata.distributions(name='pip', path=[purelib])))

current_pip_version = packaging.version.Version(pip_distribution.version)

return current_pip_version >= packaging.version.Version(_minimum_pip_version())


def _subprocess(cmd: list[str]) -> None:
"""Invoke subprocess and output stdout and stderr if it fails."""
try:
Expand Down Expand Up @@ -203,13 +235,6 @@ def _create_isolated_env_venv(path: str) -> tuple[str, str]:
"""
import venv

import packaging.version

if sys.version_info < (3, 8):
import importlib_metadata as metadata
else:
from importlib import metadata

symlinks = _fs_supports_symlink()
try:
with warnings.catch_warnings():
Expand All @@ -222,20 +247,8 @@ def _create_isolated_env_venv(path: str) -> tuple[str, str]:
executable, script_dir, purelib = _find_executable_and_scripts(path)

# Get the version of pip in the environment
pip_distribution = next(iter(metadata.distributions(name='pip', path=[purelib])))
current_pip_version = packaging.version.Version(pip_distribution.version)

if platform.system() == 'Darwin' and int(platform.mac_ver()[0].split('.')[0]) >= 11:
# macOS 11+ name scheme change requires 20.3. Intel macOS 11.0 can be told to report 10.16 for backwards
# compatibility; but that also fixes earlier versions of pip so this is only needed for 11+.
is_apple_silicon_python = platform.machine() != 'x86_64'
minimum_pip_version = '21.0.1' if is_apple_silicon_python else '20.3.0'
else:
# PEP-517 and manylinux1 was first implemented in 19.1
minimum_pip_version = '19.1.0'

if current_pip_version < packaging.version.Version(minimum_pip_version):
_subprocess([executable, '-m', 'pip', 'install', f'pip>={minimum_pip_version}'])
if not _has_valid_pip(purelib):
_subprocess([executable, '-m', 'pip', 'install', f'pip>={_minimum_pip_version()}'])

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