Skip to content

Commit 7d523f3

Browse files
authored
Use pep517's new subprocess_runner API (#7111)
2 parents b0ea605 + 1ad0495 commit 7d523f3

File tree

5 files changed

+69
-47
lines changed

5 files changed

+69
-47
lines changed

src/pip/_internal/distributions/source/legacy.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pip._internal.build_env import BuildEnvironment
77
from pip._internal.distributions.base import AbstractDistribution
88
from pip._internal.exceptions import InstallationError
9+
from pip._internal.utils.subprocess import runner_with_spinner_message
910

1011
logger = logging.getLogger(__name__)
1112

@@ -81,9 +82,13 @@ def _raise_conflicts(conflicting_with, conflicting_reqs):
8182
# This must be done in a second pass, as the pyproject.toml
8283
# dependencies must be installed before we can call the backend.
8384
with self.req.build_env:
84-
# We need to have the env active when calling the hook.
85-
self.req.spin_message = "Getting requirements to build wheel"
86-
reqs = self.req.pep517_backend.get_requires_for_build_wheel()
85+
runner = runner_with_spinner_message(
86+
"Getting requirements to build wheel"
87+
)
88+
backend = self.req.pep517_backend
89+
with backend.subprocess_runner(runner):
90+
reqs = backend.get_requires_for_build_wheel()
91+
8792
conflicting, missing = self.req.build_env.check_requirements(reqs)
8893
if conflicting:
8994
_raise_conflicts("the backend dependencies", conflicting)

src/pip/_internal/req/req_install.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,18 @@
4949
)
5050
from pip._internal.utils.packaging import get_metadata
5151
from pip._internal.utils.setuptools_build import make_setuptools_shim_args
52-
from pip._internal.utils.subprocess import call_subprocess
52+
from pip._internal.utils.subprocess import (
53+
call_subprocess,
54+
runner_with_spinner_message,
55+
)
5356
from pip._internal.utils.temp_dir import TempDirectory
5457
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
55-
from pip._internal.utils.ui import open_spinner
5658
from pip._internal.utils.virtualenv import running_under_virtualenv
5759
from pip._internal.vcs import vcs
5860

5961
if MYPY_CHECK_RUNNING:
6062
from typing import (
61-
Any, Dict, Iterable, List, Mapping, Optional, Sequence, Union,
63+
Any, Dict, Iterable, List, Optional, Sequence, Union,
6264
)
6365
from pip._internal.build_env import BuildEnvironment
6466
from pip._internal.cache import WheelCache
@@ -547,26 +549,6 @@ def load_pyproject_toml(self):
547549
self.unpacked_source_directory, backend
548550
)
549551

550-
# Use a custom function to call subprocesses
551-
self.spin_message = ""
552-
553-
def runner(
554-
cmd, # type: List[str]
555-
cwd=None, # type: Optional[str]
556-
extra_environ=None # type: Optional[Mapping[str, Any]]
557-
):
558-
# type: (...) -> None
559-
with open_spinner(self.spin_message) as spinner:
560-
call_subprocess(
561-
cmd,
562-
cwd=cwd,
563-
extra_environ=extra_environ,
564-
spinner=spinner
565-
)
566-
self.spin_message = ""
567-
568-
self.pep517_backend._subprocess_runner = runner
569-
570552
def prepare_metadata(self):
571553
# type: () -> None
572554
"""Ensure that project metadata is available.
@@ -622,11 +604,12 @@ def prepare_pep517_metadata(self):
622604
# Note that Pep517HookCaller implements a fallback for
623605
# prepare_metadata_for_build_wheel, so we don't have to
624606
# consider the possibility that this hook doesn't exist.
607+
runner = runner_with_spinner_message("Preparing wheel metadata")
625608
backend = self.pep517_backend
626-
self.spin_message = "Preparing wheel metadata"
627-
distinfo_dir = backend.prepare_metadata_for_build_wheel(
628-
metadata_dir
629-
)
609+
with backend.subprocess_runner(runner):
610+
distinfo_dir = backend.prepare_metadata_for_build_wheel(
611+
metadata_dir
612+
)
630613

631614
return os.path.join(metadata_dir, distinfo_dir)
632615

@@ -951,15 +934,15 @@ def install(
951934
install_args = self.get_install_args(
952935
global_options, record_filename, root, prefix, pycompile,
953936
)
954-
msg = 'Running setup.py install for %s' % (self.name,)
955-
with open_spinner(msg) as spinner:
956-
with indent_log():
957-
with self.build_env:
958-
call_subprocess(
959-
install_args + install_options,
960-
cwd=self.unpacked_source_directory,
961-
spinner=spinner,
962-
)
937+
938+
runner = runner_with_spinner_message(
939+
"Running setup.py install for {}".format(self.name)
940+
)
941+
with indent_log(), self.build_env:
942+
runner(
943+
cmd=install_args + install_options,
944+
cwd=self.unpacked_source_directory,
945+
)
963946

964947
if not os.path.exists(record_filename):
965948
logger.debug('Record file %s not found', record_filename)

src/pip/_internal/utils/logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
import logging.handlers
1010
import os
1111
import sys
12-
from logging import Filter
12+
from logging import Filter, getLogger
1313

1414
from pip._vendor.six import PY2
1515

1616
from pip._internal.utils.compat import WINDOWS
1717
from pip._internal.utils.deprecation import DEPRECATION_MSG_PREFIX
1818
from pip._internal.utils.misc import ensure_dir
19-
from pip._internal.utils.subprocess import subprocess_logger
2019

2120
try:
2221
import threading
@@ -54,6 +53,7 @@
5453

5554
_log_state = threading.local()
5655
_log_state.indentation = 0
56+
subprocess_logger = getLogger('pip.subprocessor')
5757

5858

5959
class BrokenStdoutLoggingError(Exception):

src/pip/_internal/utils/subprocess.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111

1212
from pip._internal.exceptions import InstallationError
1313
from pip._internal.utils.compat import console_to_str, str_to_display
14+
from pip._internal.utils.logging import subprocess_logger
1415
from pip._internal.utils.misc import HiddenText, path_to_display
1516
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
17+
from pip._internal.utils.ui import open_spinner
1618

1719
if MYPY_CHECK_RUNNING:
18-
from typing import Any, Iterable, List, Mapping, Optional, Text, Union
20+
from typing import (
21+
Any, Callable, Iterable, List, Mapping, Optional, Text, Union,
22+
)
1923
from pip._internal.utils.ui import SpinnerInterface
2024

2125
CommandArgs = List[Union[str, HiddenText]]
2226

2327

24-
subprocess_logger = logging.getLogger('pip.subprocessor')
2528
LOG_DIVIDER = '----------------------------------------'
2629

2730

@@ -245,3 +248,28 @@ def call_subprocess(
245248
raise ValueError('Invalid value: on_returncode=%s' %
246249
repr(on_returncode))
247250
return ''.join(all_output)
251+
252+
253+
def runner_with_spinner_message(message):
254+
# type: (str) -> Callable
255+
"""Provide a subprocess_runner that shows a spinner message.
256+
257+
Intended for use with for pep517's Pep517HookCaller. Thus, the runner has
258+
an API that matches what's expected by Pep517HookCaller.subprocess_runner.
259+
"""
260+
261+
def runner(
262+
cmd, # type: List[str]
263+
cwd=None, # type: Optional[str]
264+
extra_environ=None # type: Optional[Mapping[str, Any]]
265+
):
266+
# type: (...) -> None
267+
with open_spinner(message) as spinner:
268+
call_subprocess(
269+
cmd,
270+
cwd=cwd,
271+
extra_environ=extra_environ,
272+
spinner=spinner,
273+
)
274+
275+
return runner

src/pip/_internal/wheel.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
LOG_DIVIDER,
4545
call_subprocess,
4646
format_command_args,
47+
runner_with_spinner_message,
4748
)
4849
from pip._internal.utils.temp_dir import TempDirectory
4950
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -979,12 +980,17 @@ def _build_one_pep517(self, req, tempd, python_tag=None):
979980
'--build-options is present' % (req.name,))
980981
return None
981982
try:
982-
req.spin_message = 'Building wheel for %s (PEP 517)' % (req.name,)
983983
logger.debug('Destination directory: %s', tempd)
984-
wheel_name = req.pep517_backend.build_wheel(
985-
tempd,
986-
metadata_directory=req.metadata_directory
984+
985+
runner = runner_with_spinner_message(
986+
'Building wheel for {} (PEP 517)'.format(req.name)
987987
)
988+
backend = req.pep517_backend
989+
with backend.subprocess_runner(runner):
990+
wheel_name = backend.build_wheel(
991+
tempd,
992+
metadata_directory=req.metadata_directory,
993+
)
988994
if python_tag:
989995
# General PEP 517 backends don't necessarily support
990996
# a "--python-tag" option, so we rename the wheel

0 commit comments

Comments
 (0)