from __future__ import absolute_import import logging import os import subprocess from pip._vendor.six.moves import shlex_quote from pip._internal.cli.spinners import SpinnerInterface, open_spinner from pip._internal.exceptions import InstallationError from pip._internal.utils.compat import console_to_str, str_to_display from pip._internal.utils.logging import subprocess_logger from pip._internal.utils.misc import HiddenText, path_to_display from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: from typing import ( Any, Callable, Iterable, List, Mapping, Optional, Text, Union, ) CommandArgs = List[Union[str, HiddenText]] LOG_DIVIDER = '----------------------------------------' def make_command(*args): # type: (Union[str, HiddenText, CommandArgs]) -> CommandArgs """ Create a CommandArgs object. """ command_args = [] # type: CommandArgs for arg in args: # Check for list instead of CommandArgs since CommandArgs is # only known during type-checking. if isinstance(arg, list): command_args.extend(arg) else: # Otherwise, arg is str or HiddenText. command_args.append(arg) return command_args def format_command_args(args): # type: (Union[List[str], CommandArgs]) -> str """ Format command arguments for display. """ # For HiddenText arguments, display the redacted form by calling str(). # Also, we don't apply str() to arguments that aren't HiddenText since # this can trigger a UnicodeDecodeError in Python 2 if the argument # has type unicode and includes a non-ascii character. (The type # checker doesn't ensure the annotations are correct in all cases.) return ' '.join( shlex_quote(str(arg)) if isinstance(arg, HiddenText) else shlex_quote(arg) for arg in args ) def reveal_command_args(args): # type: (Union[List[str], CommandArgs]) -> List[str] """ Return the arguments in their raw, unredacted form. """ return [ arg.secret if isinstance(arg, HiddenText) else arg for arg in args ] def make_subprocess_output_error( cmd_args, # type: Union[List[str], CommandArgs] cwd, # type: Optional[str] lines, # type: List[Text] exit_status, # type: int ): # type: (...) -> Text """ Create and return the error message to use to log a subprocess error with command output. :param lines: A list of lines, each ending with a newline. """ command = format_command_args(cmd_args) # Convert `command` and `cwd` to text (unicode in Python 2) so we can use # them as arguments in the unicode format string below. This avoids # "UnicodeDecodeError: 'ascii' codec can't decode byte ..." in Python 2 # if either contains a non-ascii character. command_display = str_to_display(command, desc='command bytes') cwd_display = path_to_display(cwd) # We know the joined output value ends in a newline. output = ''.join(lines) msg = ( # Use a unicode string to avoid "UnicodeEncodeError: 'ascii' # codec can't encode character ..." in Python 2 when a format # argument (e.g. `output`) has a non-ascii character. u'Command errored out with exit status {exit_status}:\n' ' command: {command_display}\n' ' cwd: {cwd_display}\n' 'Complete output ({line_count} lines):\n{output}{divider}' ).format( exit_status=exit_status, command_display=command_display, cwd_display=cwd_display, line_count=len(lines), output=output, divider=LOG_DIVIDER, ) return msg def call_subprocess( cmd, # type: Union[List[str], CommandArgs] show_stdout=False, # type: bool cwd=None, # type: Optional[str] on_returncode='raise', # type: str extra_ok_returncodes=None, # type: Optional[Iterable[int]] command_desc=None, # type: Optional[str] extra_environ=None, # type: Optional[Mapping[str, Any]] unset_environ=None, # type: Optional[Iterable[str]] spinner=None, # type: Optional[SpinnerInterface] log_failed_cmd=True # type: Optional[bool] ): # type: (...) -> Text """ Args: show_stdout: if true, use INFO to log the subprocess's stderr and stdout streams. Otherwise, use DEBUG. Defaults to False. extra_ok_returncodes: an iterable of integer return codes that are acceptable, in addition to 0. Defaults to None, which means []. unset_environ: an iterable of environment variable names to unset prior to calling subprocess.Popen(). log_failed_cmd: if false, failed commands are not logged, only raised. """ if extra_ok_returncodes is None: extra_ok_returncodes = [] if unset_environ is None: unset_environ = [] # Most places in pip use show_stdout=False. What this means is-- # # - We connect the child's output (combined stderr and stdout) to a # single pipe, which we read. # - We log this output to stderr at DEBUG level as it is received. # - If DEBUG logging isn't enabled (e.g. if --verbose logging wasn't # requested), then we show a spinner so the user can still see the # subprocess is in progress. # - If the subprocess exits with an error, we log the output to stderr # at ERROR level if it hasn't already been displayed to the console # (e.g. if --verbose logging wasn't enabled). This way we don't log # the output to the console twice. # # If show_stdout=True, then the above is still done, but with DEBUG # replaced by INFO. if show_stdout: # Then log the subprocess output at INFO level. log_subprocess = subprocess_logger.info used_level = logging.INFO else: # Then log the subprocess output using DEBUG. This also ensures # it will be logged to the log file (aka user_log), if enabled. log_subprocess = subprocess_logger.debug used_level = logging.DEBUG # Whether the subprocess will be visible in the console. showing_subprocess = subprocess_logger.getEffectiveLevel() <= used_level # Only use the spinner if we're not showing the subprocess output # and we have a spinner. use_spinner = not showing_subprocess and spinner is not None if command_desc is None: command_desc = format_command_args(cmd) log_subprocess("Running command %s", command_desc) env = os.environ.copy() if extra_environ: env.update(extra_environ) for name in unset_environ: env.pop(name, None) try: proc = subprocess.Popen( # Convert HiddenText objects to the underlying str. reveal_command_args(cmd), stderr=subprocess.STDOUT, stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=cwd, env=env, ) assert proc.stdin assert proc.stdout proc.stdin.close() except Exception as exc: if log_failed_cmd: subprocess_logger.critical( "Error %s while executing command %s", exc, command_desc, ) raise all_output = [] while True: # The "line" value is a unicode string in Python 2. line = console_to_str(proc.stdout.readline()) if not line: break line = line.rstrip() all_output.append(line + '\n') # Show the line immediately. log_subprocess(line) # Update the spinner. if use_spinner: assert spinner spinner.spin() try: proc.wait() finally: if proc.stdout: proc.stdout.close() proc_had_error = ( proc.returncode and proc.returncode not in extra_ok_returncodes ) if use_spinner: assert spinner if proc_had_error: spinner.finish("error") else: spinner.finish("done") if proc_had_error: if on_returncode == 'raise': if not showing_subprocess and log_failed_cmd: # Then the subprocess streams haven't been logged to the # console yet. msg = make_subprocess_output_error( cmd_args=cmd, cwd=cwd, lines=all_output, exit_status=proc.returncode, ) subprocess_logger.error(msg) exc_msg = ( 'Command errored out with exit status {}: {} ' 'Check the logs for full command output.' ).format(proc.returncode, command_desc) raise InstallationError(exc_msg) elif on_returncode == 'warn': subprocess_logger.warning( 'Command "%s" had error code %s in %s', command_desc, proc.returncode, cwd, ) elif on_returncode == 'ignore': pass else: raise ValueError('Invalid value: on_returncode={!r}'.format( on_returncode)) return ''.join(all_output) def runner_with_spinner_message(message): # type: (str) -> Callable[..., None] """Provide a subprocess_runner that shows a spinner message. Intended for use with for pep517's Pep517HookCaller. Thus, the runner has an API that matches what's expected by Pep517HookCaller.subprocess_runner. """ def runner( cmd, # type: List[str] cwd=None, # type: Optional[str] extra_environ=None # type: Optional[Mapping[str, Any]] ): # type: (...) -> None with open_spinner(message) as spinner: call_subprocess( cmd, cwd=cwd, extra_environ=extra_environ, spinner=spinner, ) return runner
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
.__init__.pyo.40009 | File | 164 B | 0644 |
|
.appdirs.pyo.40009 | File | 1.78 KB | 0644 |
|
.compat.pyo.40009 | File | 8.62 KB | 0644 |
|
.compatibility_tags.pyo.40009 | File | 4.45 KB | 0644 |
|
.datetime.pyo.40009 | File | 625 B | 0644 |
|
.deprecation.pyo.40009 | File | 3.44 KB | 0644 |
|
.distutils_args.pyo.40009 | File | 1.6 KB | 0644 |
|
.entrypoints.pyo.40009 | File | 1.47 KB | 0644 |
|
.filetypes.pyo.40009 | File | 792 B | 0644 |
|
.glibc.pyo.40009 | File | 2.19 KB | 0644 |
|
.hashes.pyo.40009 | File | 5.68 KB | 0644 |
|
.inject_securetransport.pyo.40009 | File | 1.07 KB | 0644 |
|
.logging.pyo.40009 | File | 11.6 KB | 0644 |
|
.misc.pyo.40009 | File | 32.2 KB | 0644 |
|
.models.pyo.40009 | File | 2.55 KB | 0644 |
|
.packaging.pyo.40009 | File | 3.27 KB | 0644 |
|
.parallel.pyo.40009 | File | 3.62 KB | 0644 |
|
.pkg_resources.pyo.40009 | File | 2.39 KB | 0644 |
|
.typing.pyo.40009 | File | 1.52 KB | 0644 |
|
.virtualenv.pyo.40009 | File | 4.11 KB | 0644 |
|
.wheel.pyo.40009 | File | 7.27 KB | 0644 |
|
__init__.py | File | 0 B | 0644 |
|
__init__.pyc | File | 164 B | 0644 |
|
__init__.pyo | File | 164 B | 0644 |
|
appdirs.py | File | 1.32 KB | 0644 |
|
appdirs.pyc | File | 1.78 KB | 0644 |
|
appdirs.pyo | File | 1.78 KB | 0644 |
|
compat.py | File | 9.27 KB | 0644 |
|
compat.pyc | File | 8.62 KB | 0644 |
|
compat.pyo | File | 8.62 KB | 0644 |
|
compatibility_tags.py | File | 5.31 KB | 0644 |
|
compatibility_tags.pyc | File | 4.45 KB | 0644 |
|
compatibility_tags.pyo | File | 4.45 KB | 0644 |
|
datetime.py | File | 295 B | 0644 |
|
datetime.pyc | File | 625 B | 0644 |
|
datetime.pyo | File | 625 B | 0644 |
|
deprecation.py | File | 3.24 KB | 0644 |
|
deprecation.pyc | File | 3.44 KB | 0644 |
|
deprecation.pyo | File | 3.44 KB | 0644 |
|
direct_url_helpers.py | File | 4.26 KB | 0644 |
|
direct_url_helpers.pyc | File | 3.37 KB | 0644 |
|
direct_url_helpers.pyo | File | 3.24 KB | 0644 |
|
distutils_args.py | File | 1.32 KB | 0644 |
|
distutils_args.pyc | File | 1.6 KB | 0644 |
|
distutils_args.pyo | File | 1.6 KB | 0644 |
|
encoding.py | File | 1.25 KB | 0644 |
|
encoding.pyc | File | 1.61 KB | 0644 |
|
encoding.pyo | File | 1.56 KB | 0644 |
|
entrypoints.py | File | 1.19 KB | 0644 |
|
entrypoints.pyc | File | 1.47 KB | 0644 |
|
entrypoints.pyo | File | 1.47 KB | 0644 |
|
filesystem.py | File | 6.78 KB | 0644 |
|
filesystem.pyc | File | 7.4 KB | 0644 |
|
filesystem.pyo | File | 7.35 KB | 0644 |
|
filetypes.py | File | 571 B | 0644 |
|
filetypes.pyc | File | 792 B | 0644 |
|
filetypes.pyo | File | 792 B | 0644 |
|
glibc.py | File | 3.22 KB | 0644 |
|
glibc.pyc | File | 2.19 KB | 0644 |
|
glibc.pyo | File | 2.19 KB | 0644 |
|
hashes.py | File | 4.57 KB | 0644 |
|
hashes.pyc | File | 5.68 KB | 0644 |
|
hashes.pyo | File | 5.68 KB | 0644 |
|
inject_securetransport.py | File | 810 B | 0644 |
|
inject_securetransport.pyc | File | 1.07 KB | 0644 |
|
inject_securetransport.pyo | File | 1.07 KB | 0644 |
|
logging.py | File | 12.79 KB | 0644 |
|
logging.pyc | File | 11.6 KB | 0644 |
|
logging.pyo | File | 11.6 KB | 0644 |
|
misc.py | File | 27.67 KB | 0644 |
|
misc.pyc | File | 32.2 KB | 0644 |
|
misc.pyo | File | 32.2 KB | 0644 |
|
models.py | File | 1.17 KB | 0644 |
|
models.pyc | File | 2.55 KB | 0644 |
|
models.pyo | File | 2.55 KB | 0644 |
|
packaging.py | File | 2.96 KB | 0644 |
|
packaging.pyc | File | 3.27 KB | 0644 |
|
packaging.pyo | File | 3.27 KB | 0644 |
|
parallel.py | File | 3.32 KB | 0644 |
|
parallel.pyc | File | 3.62 KB | 0644 |
|
parallel.pyo | File | 3.62 KB | 0644 |
|
pkg_resources.py | File | 1.22 KB | 0644 |
|
pkg_resources.pyc | File | 2.39 KB | 0644 |
|
pkg_resources.pyo | File | 2.39 KB | 0644 |
|
setuptools_build.py | File | 4.94 KB | 0644 |
|
setuptools_build.pyc | File | 3.83 KB | 0644 |
|
setuptools_build.pyo | File | 3.75 KB | 0644 |
|
subprocess.py | File | 9.69 KB | 0644 |
|
subprocess.pyc | File | 6.77 KB | 0644 |
|
subprocess.pyo | File | 6.69 KB | 0644 |
|
temp_dir.py | File | 8.18 KB | 0644 |
|
temp_dir.pyc | File | 8.75 KB | 0644 |
|
temp_dir.pyo | File | 8.62 KB | 0644 |
|
typing.py | File | 1.37 KB | 0644 |
|
typing.pyc | File | 1.52 KB | 0644 |
|
typing.pyo | File | 1.52 KB | 0644 |
|
unpacking.py | File | 9.27 KB | 0644 |
|
unpacking.pyc | File | 7.83 KB | 0644 |
|
unpacking.pyo | File | 7.79 KB | 0644 |
|
urls.py | File | 1.49 KB | 0644 |
|
urls.pyc | File | 1.94 KB | 0644 |
|
urls.pyo | File | 1.8 KB | 0644 |
|
virtualenv.py | File | 3.62 KB | 0644 |
|
virtualenv.pyc | File | 4.11 KB | 0644 |
|
virtualenv.pyo | File | 4.11 KB | 0644 |
|
wheel.py | File | 7.13 KB | 0644 |
|
wheel.pyc | File | 7.27 KB | 0644 |
|
wheel.pyo | File | 7.27 KB | 0644 |
|