base_command.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. """Base Command class, and related routines"""
  2. import logging
  3. import logging.config
  4. import optparse
  5. import os
  6. import sys
  7. import traceback
  8. from optparse import Values
  9. from typing import List, Optional, Tuple
  10. from pip._vendor.rich import reconfigure
  11. from pip._vendor.rich import traceback as rich_traceback
  12. from pip._internal.cli import cmdoptions
  13. from pip._internal.cli.command_context import CommandContextMixIn
  14. from pip._internal.cli.parser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
  15. from pip._internal.cli.status_codes import (
  16. ERROR,
  17. PREVIOUS_BUILD_DIR_ERROR,
  18. UNKNOWN_ERROR,
  19. VIRTUALENV_NOT_FOUND,
  20. )
  21. from pip._internal.exceptions import (
  22. BadCommand,
  23. CommandError,
  24. DiagnosticPipError,
  25. InstallationError,
  26. NetworkConnectionError,
  27. PreviousBuildDirError,
  28. )
  29. from pip._internal.utils.filesystem import check_path_owner
  30. from pip._internal.utils.logging import BrokenStdoutLoggingError, setup_logging
  31. from pip._internal.utils.misc import get_prog, normalize_path
  32. from pip._internal.utils.temp_dir import TempDirectoryTypeRegistry as TempDirRegistry
  33. from pip._internal.utils.temp_dir import global_tempdir_manager, tempdir_registry
  34. from pip._internal.utils.virtualenv import running_under_virtualenv
  35. __all__ = ["Command"]
  36. logger = logging.getLogger(__name__)
  37. class Command(CommandContextMixIn):
  38. usage: str = ""
  39. ignore_require_venv: bool = False
  40. def __init__(self, name: str, summary: str, isolated: bool = False) -> None:
  41. super().__init__()
  42. self.name = name
  43. self.summary = summary
  44. self.parser = ConfigOptionParser(
  45. usage=self.usage,
  46. prog=f"{get_prog()} {name}",
  47. formatter=UpdatingDefaultsHelpFormatter(),
  48. add_help_option=False,
  49. name=name,
  50. description=self.__doc__,
  51. isolated=isolated,
  52. )
  53. self.tempdir_registry: Optional[TempDirRegistry] = None
  54. # Commands should add options to this option group
  55. optgroup_name = f"{self.name.capitalize()} Options"
  56. self.cmd_opts = optparse.OptionGroup(self.parser, optgroup_name)
  57. # Add the general options
  58. gen_opts = cmdoptions.make_option_group(
  59. cmdoptions.general_group,
  60. self.parser,
  61. )
  62. self.parser.add_option_group(gen_opts)
  63. self.add_options()
  64. def add_options(self) -> None:
  65. pass
  66. def handle_pip_version_check(self, options: Values) -> None:
  67. """
  68. This is a no-op so that commands by default do not do the pip version
  69. check.
  70. """
  71. # Make sure we do the pip version check if the index_group options
  72. # are present.
  73. assert not hasattr(options, "no_index")
  74. def run(self, options: Values, args: List[str]) -> int:
  75. raise NotImplementedError
  76. def _run_wrapper(self, level_number: int, options: Values, args: List[str]) -> int:
  77. def _inner_run() -> int:
  78. try:
  79. return self.run(options, args)
  80. finally:
  81. self.handle_pip_version_check(options)
  82. if options.debug_mode:
  83. rich_traceback.install(show_locals=True)
  84. return _inner_run()
  85. try:
  86. status = _inner_run()
  87. assert isinstance(status, int)
  88. return status
  89. except DiagnosticPipError as exc:
  90. logger.error("%s", exc, extra={"rich": True})
  91. logger.debug("Exception information:", exc_info=True)
  92. return ERROR
  93. except PreviousBuildDirError as exc:
  94. logger.critical(str(exc))
  95. logger.debug("Exception information:", exc_info=True)
  96. return PREVIOUS_BUILD_DIR_ERROR
  97. except (
  98. InstallationError,
  99. BadCommand,
  100. NetworkConnectionError,
  101. ) as exc:
  102. logger.critical(str(exc))
  103. logger.debug("Exception information:", exc_info=True)
  104. return ERROR
  105. except CommandError as exc:
  106. logger.critical("%s", exc)
  107. logger.debug("Exception information:", exc_info=True)
  108. return ERROR
  109. except BrokenStdoutLoggingError:
  110. # Bypass our logger and write any remaining messages to
  111. # stderr because stdout no longer works.
  112. print("ERROR: Pipe to stdout was broken", file=sys.stderr)
  113. if level_number <= logging.DEBUG:
  114. traceback.print_exc(file=sys.stderr)
  115. return ERROR
  116. except KeyboardInterrupt:
  117. logger.critical("Operation cancelled by user")
  118. logger.debug("Exception information:", exc_info=True)
  119. return ERROR
  120. except BaseException:
  121. logger.critical("Exception:", exc_info=True)
  122. return UNKNOWN_ERROR
  123. def parse_args(self, args: List[str]) -> Tuple[Values, List[str]]:
  124. # factored out for testability
  125. return self.parser.parse_args(args)
  126. def main(self, args: List[str]) -> int:
  127. try:
  128. with self.main_context():
  129. return self._main(args)
  130. finally:
  131. logging.shutdown()
  132. def _main(self, args: List[str]) -> int:
  133. # We must initialize this before the tempdir manager, otherwise the
  134. # configuration would not be accessible by the time we clean up the
  135. # tempdir manager.
  136. self.tempdir_registry = self.enter_context(tempdir_registry())
  137. # Intentionally set as early as possible so globally-managed temporary
  138. # directories are available to the rest of the code.
  139. self.enter_context(global_tempdir_manager())
  140. options, args = self.parse_args(args)
  141. # Set verbosity so that it can be used elsewhere.
  142. self.verbosity = options.verbose - options.quiet
  143. reconfigure(no_color=options.no_color)
  144. level_number = setup_logging(
  145. verbosity=self.verbosity,
  146. no_color=options.no_color,
  147. user_log_file=options.log,
  148. )
  149. always_enabled_features = set(options.features_enabled) & set(
  150. cmdoptions.ALWAYS_ENABLED_FEATURES
  151. )
  152. if always_enabled_features:
  153. logger.warning(
  154. "The following features are always enabled: %s. ",
  155. ", ".join(sorted(always_enabled_features)),
  156. )
  157. # Make sure that the --python argument isn't specified after the
  158. # subcommand. We can tell, because if --python was specified,
  159. # we should only reach this point if we're running in the created
  160. # subprocess, which has the _PIP_RUNNING_IN_SUBPROCESS environment
  161. # variable set.
  162. if options.python and "_PIP_RUNNING_IN_SUBPROCESS" not in os.environ:
  163. logger.critical(
  164. "The --python option must be placed before the pip subcommand name"
  165. )
  166. sys.exit(ERROR)
  167. # TODO: Try to get these passing down from the command?
  168. # without resorting to os.environ to hold these.
  169. # This also affects isolated builds and it should.
  170. if options.no_input:
  171. os.environ["PIP_NO_INPUT"] = "1"
  172. if options.exists_action:
  173. os.environ["PIP_EXISTS_ACTION"] = " ".join(options.exists_action)
  174. if options.require_venv and not self.ignore_require_venv:
  175. # If a venv is required check if it can really be found
  176. if not running_under_virtualenv():
  177. logger.critical("Could not find an activated virtualenv (required).")
  178. sys.exit(VIRTUALENV_NOT_FOUND)
  179. if options.cache_dir:
  180. options.cache_dir = normalize_path(options.cache_dir)
  181. if not check_path_owner(options.cache_dir):
  182. logger.warning(
  183. "The directory '%s' or its parent directory is not owned "
  184. "or is not writable by the current user. The cache "
  185. "has been disabled. Check the permissions and owner of "
  186. "that directory. If executing pip with sudo, you should "
  187. "use sudo's -H flag.",
  188. options.cache_dir,
  189. )
  190. options.cache_dir = None
  191. return self._run_wrapper(level_number, options, args)