base_command.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 Any, List, Optional, Tuple
  10. from pip._internal.cli import cmdoptions
  11. from pip._internal.cli.command_context import CommandContextMixIn
  12. from pip._internal.cli.parser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
  13. from pip._internal.cli.status_codes import (
  14. ERROR,
  15. PREVIOUS_BUILD_DIR_ERROR,
  16. UNKNOWN_ERROR,
  17. VIRTUALENV_NOT_FOUND,
  18. )
  19. from pip._internal.exceptions import (
  20. BadCommand,
  21. CommandError,
  22. InstallationError,
  23. NetworkConnectionError,
  24. PreviousBuildDirError,
  25. UninstallationError,
  26. )
  27. from pip._internal.utils.deprecation import deprecated
  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[Any]) -> int:
  74. raise NotImplementedError
  75. def parse_args(self, args: List[str]) -> Tuple[Any, Any]:
  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 getattr(options, "build_dir", None):
  125. deprecated(
  126. reason=(
  127. "The -b/--build/--build-dir/--build-directory "
  128. "option is deprecated and has no effect anymore."
  129. ),
  130. replacement=(
  131. "use the TMPDIR/TEMP/TMP environment variable, "
  132. "possibly combined with --no-clean"
  133. ),
  134. gone_in="21.3",
  135. issue=8333,
  136. )
  137. if "2020-resolver" in options.features_enabled:
  138. logger.warning(
  139. "--use-feature=2020-resolver no longer has any effect, "
  140. "since it is now the default dependency resolver in pip. "
  141. "This will become an error in pip 21.0."
  142. )
  143. try:
  144. status = self.run(options, args)
  145. assert isinstance(status, int)
  146. return status
  147. except PreviousBuildDirError as exc:
  148. logger.critical(str(exc))
  149. logger.debug("Exception information:", exc_info=True)
  150. return PREVIOUS_BUILD_DIR_ERROR
  151. except (
  152. InstallationError,
  153. UninstallationError,
  154. BadCommand,
  155. NetworkConnectionError,
  156. ) as exc:
  157. logger.critical(str(exc))
  158. logger.debug("Exception information:", exc_info=True)
  159. return ERROR
  160. except CommandError as exc:
  161. logger.critical("%s", exc)
  162. logger.debug("Exception information:", exc_info=True)
  163. return ERROR
  164. except BrokenStdoutLoggingError:
  165. # Bypass our logger and write any remaining messages to stderr
  166. # because stdout no longer works.
  167. print("ERROR: Pipe to stdout was broken", file=sys.stderr)
  168. if level_number <= logging.DEBUG:
  169. traceback.print_exc(file=sys.stderr)
  170. return ERROR
  171. except KeyboardInterrupt:
  172. logger.critical("Operation cancelled by user")
  173. logger.debug("Exception information:", exc_info=True)
  174. return ERROR
  175. except BaseException:
  176. logger.critical("Exception:", exc_info=True)
  177. return UNKNOWN_ERROR
  178. finally:
  179. self.handle_pip_version_check(options)