debug.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import locale
  2. import logging
  3. import os
  4. import sys
  5. from optparse import Values
  6. from types import ModuleType
  7. from typing import Any, Dict, List, Optional
  8. import pip._vendor
  9. from pip._vendor.certifi import where
  10. from pip._vendor.packaging.version import parse as parse_version
  11. from pip import __file__ as pip_location
  12. from pip._internal.cli import cmdoptions
  13. from pip._internal.cli.base_command import Command
  14. from pip._internal.cli.cmdoptions import make_target_python
  15. from pip._internal.cli.status_codes import SUCCESS
  16. from pip._internal.configuration import Configuration
  17. from pip._internal.metadata import get_environment
  18. from pip._internal.utils.logging import indent_log
  19. from pip._internal.utils.misc import get_pip_version
  20. logger = logging.getLogger(__name__)
  21. def show_value(name: str, value: Any) -> None:
  22. logger.info("%s: %s", name, value)
  23. def show_sys_implementation() -> None:
  24. logger.info("sys.implementation:")
  25. implementation_name = sys.implementation.name
  26. with indent_log():
  27. show_value("name", implementation_name)
  28. def create_vendor_txt_map() -> Dict[str, str]:
  29. vendor_txt_path = os.path.join(
  30. os.path.dirname(pip_location), "_vendor", "vendor.txt"
  31. )
  32. with open(vendor_txt_path) as f:
  33. # Purge non version specifying lines.
  34. # Also, remove any space prefix or suffixes (including comments).
  35. lines = [
  36. line.strip().split(" ", 1)[0] for line in f.readlines() if "==" in line
  37. ]
  38. # Transform into "module" -> version dict.
  39. return dict(line.split("==", 1) for line in lines) # type: ignore
  40. def get_module_from_module_name(module_name: str) -> ModuleType:
  41. # Module name can be uppercase in vendor.txt for some reason...
  42. module_name = module_name.lower()
  43. # PATCH: setuptools is actually only pkg_resources.
  44. if module_name == "setuptools":
  45. module_name = "pkg_resources"
  46. __import__(f"pip._vendor.{module_name}", globals(), locals(), level=0)
  47. return getattr(pip._vendor, module_name)
  48. def get_vendor_version_from_module(module_name: str) -> Optional[str]:
  49. module = get_module_from_module_name(module_name)
  50. version = getattr(module, "__version__", None)
  51. if not version:
  52. # Try to find version in debundled module info.
  53. env = get_environment([os.path.dirname(module.__file__)])
  54. dist = env.get_distribution(module_name)
  55. if dist:
  56. version = str(dist.version)
  57. return version
  58. def show_actual_vendor_versions(vendor_txt_versions: Dict[str, str]) -> None:
  59. """Log the actual version and print extra info if there is
  60. a conflict or if the actual version could not be imported.
  61. """
  62. for module_name, expected_version in vendor_txt_versions.items():
  63. extra_message = ""
  64. actual_version = get_vendor_version_from_module(module_name)
  65. if not actual_version:
  66. extra_message = (
  67. " (Unable to locate actual module version, using"
  68. " vendor.txt specified version)"
  69. )
  70. actual_version = expected_version
  71. elif parse_version(actual_version) != parse_version(expected_version):
  72. extra_message = (
  73. " (CONFLICT: vendor.txt suggests version should"
  74. " be {})".format(expected_version)
  75. )
  76. logger.info("%s==%s%s", module_name, actual_version, extra_message)
  77. def show_vendor_versions() -> None:
  78. logger.info("vendored library versions:")
  79. vendor_txt_versions = create_vendor_txt_map()
  80. with indent_log():
  81. show_actual_vendor_versions(vendor_txt_versions)
  82. def show_tags(options: Values) -> None:
  83. tag_limit = 10
  84. target_python = make_target_python(options)
  85. tags = target_python.get_tags()
  86. # Display the target options that were explicitly provided.
  87. formatted_target = target_python.format_given()
  88. suffix = ""
  89. if formatted_target:
  90. suffix = f" (target: {formatted_target})"
  91. msg = "Compatible tags: {}{}".format(len(tags), suffix)
  92. logger.info(msg)
  93. if options.verbose < 1 and len(tags) > tag_limit:
  94. tags_limited = True
  95. tags = tags[:tag_limit]
  96. else:
  97. tags_limited = False
  98. with indent_log():
  99. for tag in tags:
  100. logger.info(str(tag))
  101. if tags_limited:
  102. msg = (
  103. "...\n[First {tag_limit} tags shown. Pass --verbose to show all.]"
  104. ).format(tag_limit=tag_limit)
  105. logger.info(msg)
  106. def ca_bundle_info(config: Configuration) -> str:
  107. levels = set()
  108. for key, _ in config.items():
  109. levels.add(key.split(".")[0])
  110. if not levels:
  111. return "Not specified"
  112. levels_that_override_global = ["install", "wheel", "download"]
  113. global_overriding_level = [
  114. level for level in levels if level in levels_that_override_global
  115. ]
  116. if not global_overriding_level:
  117. return "global"
  118. if "global" in levels:
  119. levels.remove("global")
  120. return ", ".join(levels)
  121. class DebugCommand(Command):
  122. """
  123. Display debug information.
  124. """
  125. usage = """
  126. %prog <options>"""
  127. ignore_require_venv = True
  128. def add_options(self) -> None:
  129. cmdoptions.add_target_python_options(self.cmd_opts)
  130. self.parser.insert_option_group(0, self.cmd_opts)
  131. self.parser.config.load()
  132. def run(self, options: Values, args: List[str]) -> int:
  133. logger.warning(
  134. "This command is only meant for debugging. "
  135. "Do not use this with automation for parsing and getting these "
  136. "details, since the output and options of this command may "
  137. "change without notice."
  138. )
  139. show_value("pip version", get_pip_version())
  140. show_value("sys.version", sys.version)
  141. show_value("sys.executable", sys.executable)
  142. show_value("sys.getdefaultencoding", sys.getdefaultencoding())
  143. show_value("sys.getfilesystemencoding", sys.getfilesystemencoding())
  144. show_value(
  145. "locale.getpreferredencoding",
  146. locale.getpreferredencoding(),
  147. )
  148. show_value("sys.platform", sys.platform)
  149. show_sys_implementation()
  150. show_value("'cert' config value", ca_bundle_info(self.parser.config))
  151. show_value("REQUESTS_CA_BUNDLE", os.environ.get("REQUESTS_CA_BUNDLE"))
  152. show_value("CURL_CA_BUNDLE", os.environ.get("CURL_CA_BUNDLE"))
  153. show_value("pip._vendor.certifi.where()", where())
  154. show_value("pip._vendor.DEBUNDLED", pip._vendor.DEBUNDLED)
  155. show_vendor_versions()
  156. show_tags(options)
  157. return SUCCESS