debug.py 7.1 KB

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