Skip to content

Commit

Permalink
chore: address feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Feb 26, 2024
1 parent e83bfa6 commit 04dd0d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
40 changes: 19 additions & 21 deletions src/build/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,20 @@ def _minimum_pip_version() -> str:
def _has_valid_pip(**distargs: object) -> bool:
"""
Given a path, see if Pip is present and return True if the version is
sufficient for build, False if it is not.
sufficient for build, False if it is not. ModuleNotFoundError is thrown if
pip is not present.
"""

import packaging.version

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

name = 'pip'

pip_distribution = next(iter(metadata.distributions(name='pip', **distargs)))
try:
pip_distribution = next(iter(metadata.distributions(name=name, **distargs)))
except StopIteration:
raise ModuleNotFoundError(name) from None

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

Expand All @@ -93,16 +96,13 @@ def _has_valid_pip(**distargs: object) -> bool:
@functools.lru_cache(maxsize=None)
def _valid_global_pip() -> bool | None:
"""
This checks for a valid global pip. Returns None if the prerequisites are
not available (Python 3.7 only) or pip is missing, False if Pip is too old,
and True if it can be used.
This checks for a valid global pip. Returns None if pip is missing, False
if Pip is too old, and True if it can be used.
"""

try:
return _has_valid_pip()
except ModuleNotFoundError: # Python 3.7 only
return None
except StopIteration:
except ModuleNotFoundError:
return None


Expand Down Expand Up @@ -155,11 +155,11 @@ def python_executable(self) -> str:
"""The python executable of the isolated build environment."""
return self._python_executable

def _pip_args(self, *, isolate: bool = False) -> list[str]:
def _pip_args(self) -> list[str]:
if _valid_global_pip():
return [sys.executable, '-Im' if isolate else '-m', 'pip', '--python', self.python_executable]
return [sys.executable, '-Im', 'pip', '--python', self.python_executable]
else:
return [self.python_executable, '-Im' if isolate else '-m', 'pip']
return [self.python_executable, '-Im', 'pip']

def make_extra_environ(self) -> dict[str, str]:
path = os.environ.get('PATH')
Expand All @@ -185,7 +185,7 @@ def install(self, requirements: Collection[str]) -> None:
req_file.write(os.linesep.join(requirements))
try:
cmd = [
*self._pip_args(isolate=True),
*self._pip_args(),
'install',
'--use-pep517',
'--no-warn-script-location',
Expand Down Expand Up @@ -222,9 +222,9 @@ def _create_isolated_env_virtualenv(path: str) -> tuple[str, str]:
import virtualenv

if _valid_global_pip():
cmd = [str(path), '--no-seed', '--activators', '']
cmd = [path, '--no-seed', '--activators', '']
else:
cmd = [str(path), '--no-setuptools', '--no-wheel', '--activators', '']
cmd = [path, '--no-setuptools', '--no-wheel', '--activators', '']

result = virtualenv.cli_run(cmd, setup_logging=False)
executable = str(result.creator.exe)
Expand Down Expand Up @@ -275,9 +275,7 @@ def _create_isolated_env_venv(path: str) -> tuple[str, str]:
_subprocess([executable, '-m', 'pip', 'install', f'pip>={_minimum_pip_version()}'])

# Avoid the setuptools from ensurepip to break the isolation
if _valid_global_pip():
_subprocess([sys.executable, '-m', 'pip', '--python', executable, 'uninstall', 'setuptools', '-y'])
else:
if not _valid_global_pip():
_subprocess([executable, '-m', 'pip', 'uninstall', 'setuptools', '-y'])

return executable, script_dir
Expand Down
3 changes: 1 addition & 2 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ def test_pip_needs_upgrade_mac_os_11(mocker, pip_version, arch):
mocker.patch('platform.system', return_value='Darwin')
mocker.patch('platform.machine', return_value=arch)
mocker.patch('platform.mac_ver', return_value=('11.0', ('', '', ''), ''))
metadata_name = 'importlib_metadata' if sys.version_info < (3, 8) else 'importlib.metadata'
mocker.patch(metadata_name + '.distributions', return_value=(SimpleNamespace(version=pip_version),))
mocker.patch('build._importlib.metadata.distributions', return_value=(SimpleNamespace(version=pip_version),))

min_version = Version('20.3' if arch == 'x86_64' else '21.0.1')
with build.env.DefaultIsolatedEnv():
Expand Down

0 comments on commit 04dd0d1

Please sign in to comment.