Skip to content

Commit 894998a

Browse files
henryiiilayday
authored andcommitted
chore: address feedback
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
1 parent aa0aa04 commit 894998a

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

src/build/env.py

+19-21
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,20 @@ def _minimum_pip_version() -> str:
7373
def _has_valid_pip(**distargs: object) -> bool:
7474
"""
7575
Given a path, see if Pip is present and return True if the version is
76-
sufficient for build, False if it is not.
76+
sufficient for build, False if it is not. ModuleNotFoundError is thrown if
77+
pip is not present.
7778
"""
7879

7980
import packaging.version
8081

81-
if sys.version_info < (3, 8):
82-
import importlib_metadata as metadata
83-
else:
84-
from importlib import metadata
82+
from ._importlib import metadata
83+
84+
name = 'pip'
8585

86-
pip_distribution = next(iter(metadata.distributions(name='pip', **distargs)))
86+
try:
87+
pip_distribution = next(iter(metadata.distributions(name=name, **distargs)))
88+
except StopIteration:
89+
raise ModuleNotFoundError(name) from None
8790

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

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

101103
try:
102104
return _has_valid_pip()
103-
except ModuleNotFoundError: # Python 3.7 only
104-
return None
105-
except StopIteration:
105+
except ModuleNotFoundError:
106106
return None
107107

108108

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

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

164164
def make_extra_environ(self) -> dict[str, str]:
165165
path = os.environ.get('PATH')
@@ -185,7 +185,7 @@ def install(self, requirements: Collection[str]) -> None:
185185
req_file.write(os.linesep.join(requirements))
186186
try:
187187
cmd = [
188-
*self._pip_args(isolate=True),
188+
*self._pip_args(),
189189
'install',
190190
'--use-pep517',
191191
'--no-warn-script-location',
@@ -222,9 +222,9 @@ def _create_isolated_env_virtualenv(path: str) -> tuple[str, str]:
222222
import virtualenv
223223

224224
if _valid_global_pip():
225-
cmd = [str(path), '--no-seed', '--activators', '']
225+
cmd = [path, '--no-seed', '--activators', '']
226226
else:
227-
cmd = [str(path), '--no-setuptools', '--no-wheel', '--activators', '']
227+
cmd = [path, '--no-setuptools', '--no-wheel', '--activators', '']
228228

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

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

283281
return executable, script_dir

tests/test_env.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ def test_pip_needs_upgrade_mac_os_11(mocker, pip_version, arch):
140140
mocker.patch('platform.system', return_value='Darwin')
141141
mocker.patch('platform.machine', return_value=arch)
142142
mocker.patch('platform.mac_ver', return_value=('11.0', ('', '', ''), ''))
143-
metadata_name = 'importlib_metadata' if sys.version_info < (3, 8) else 'importlib.metadata'
144-
mocker.patch(metadata_name + '.distributions', return_value=(SimpleNamespace(version=pip_version),))
143+
mocker.patch('build._importlib.metadata.distributions', return_value=(SimpleNamespace(version=pip_version),))
145144

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

0 commit comments

Comments
 (0)