From 894998a5fbb08ae169baf1f413f9fc5d1e1c3d3f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 26 Feb 2024 10:52:33 -0500 Subject: [PATCH] chore: address feedback Signed-off-by: Henry Schreiner --- src/build/env.py | 40 +++++++++++++++++++--------------------- tests/test_env.py | 3 +-- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/build/env.py b/src/build/env.py index cb6dbd70..95ba9d15 100644 --- a/src/build/env.py +++ b/src/build/env.py @@ -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) @@ -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 @@ -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') @@ -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', @@ -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) @@ -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 diff --git a/tests/test_env.py b/tests/test_env.py index de63c42b..feaf042a 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -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():