base_command.py 7.6 KB

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