show.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. from email.parser import FeedParser # type: ignore
  5. from pip._vendor import pkg_resources
  6. from pip._vendor.packaging.utils import canonicalize_name
  7. from pip._internal.basecommand import Command
  8. from pip._internal.status_codes import ERROR, SUCCESS
  9. logger = logging.getLogger(__name__)
  10. class ShowCommand(Command):
  11. """Show information about one or more installed packages."""
  12. name = 'show'
  13. usage = """
  14. %prog [options] <package> ..."""
  15. summary = 'Show information about installed packages.'
  16. ignore_require_venv = True
  17. def __init__(self, *args, **kw):
  18. super(ShowCommand, self).__init__(*args, **kw)
  19. self.cmd_opts.add_option(
  20. '-f', '--files',
  21. dest='files',
  22. action='store_true',
  23. default=False,
  24. help='Show the full list of installed files for each package.')
  25. self.parser.insert_option_group(0, self.cmd_opts)
  26. def run(self, options, args):
  27. if not args:
  28. logger.warning('ERROR: Please provide a package name or names.')
  29. return ERROR
  30. query = args
  31. results = search_packages_info(query)
  32. if not print_results(
  33. results, list_files=options.files, verbose=options.verbose):
  34. return ERROR
  35. return SUCCESS
  36. def search_packages_info(query):
  37. """
  38. Gather details from installed distributions. Print distribution name,
  39. version, location, and installed files. Installed files requires a
  40. pip generated 'installed-files.txt' in the distributions '.egg-info'
  41. directory.
  42. """
  43. installed = {}
  44. for p in pkg_resources.working_set:
  45. installed[canonicalize_name(p.project_name)] = p
  46. query_names = [canonicalize_name(name) for name in query]
  47. for dist in [installed[pkg] for pkg in query_names if pkg in installed]:
  48. package = {
  49. 'name': dist.project_name,
  50. 'version': dist.version,
  51. 'location': dist.location,
  52. 'requires': [dep.project_name for dep in dist.requires()],
  53. }
  54. file_list = None
  55. metadata = None
  56. if isinstance(dist, pkg_resources.DistInfoDistribution):
  57. # RECORDs should be part of .dist-info metadatas
  58. if dist.has_metadata('RECORD'):
  59. lines = dist.get_metadata_lines('RECORD')
  60. paths = [l.split(',')[0] for l in lines]
  61. paths = [os.path.join(dist.location, p) for p in paths]
  62. file_list = [os.path.relpath(p, dist.location) for p in paths]
  63. if dist.has_metadata('METADATA'):
  64. metadata = dist.get_metadata('METADATA')
  65. else:
  66. # Otherwise use pip's log for .egg-info's
  67. if dist.has_metadata('installed-files.txt'):
  68. paths = dist.get_metadata_lines('installed-files.txt')
  69. paths = [os.path.join(dist.egg_info, p) for p in paths]
  70. file_list = [os.path.relpath(p, dist.location) for p in paths]
  71. if dist.has_metadata('PKG-INFO'):
  72. metadata = dist.get_metadata('PKG-INFO')
  73. if dist.has_metadata('entry_points.txt'):
  74. entry_points = dist.get_metadata_lines('entry_points.txt')
  75. package['entry_points'] = entry_points
  76. if dist.has_metadata('INSTALLER'):
  77. for line in dist.get_metadata_lines('INSTALLER'):
  78. if line.strip():
  79. package['installer'] = line.strip()
  80. break
  81. # @todo: Should pkg_resources.Distribution have a
  82. # `get_pkg_info` method?
  83. feed_parser = FeedParser()
  84. feed_parser.feed(metadata)
  85. pkg_info_dict = feed_parser.close()
  86. for key in ('metadata-version', 'summary',
  87. 'home-page', 'author', 'author-email', 'license'):
  88. package[key] = pkg_info_dict.get(key)
  89. # It looks like FeedParser cannot deal with repeated headers
  90. classifiers = []
  91. for line in metadata.splitlines():
  92. if line.startswith('Classifier: '):
  93. classifiers.append(line[len('Classifier: '):])
  94. package['classifiers'] = classifiers
  95. if file_list:
  96. package['files'] = sorted(file_list)
  97. yield package
  98. def print_results(distributions, list_files=False, verbose=False):
  99. """
  100. Print the informations from installed distributions found.
  101. """
  102. results_printed = False
  103. for i, dist in enumerate(distributions):
  104. results_printed = True
  105. if i > 0:
  106. logger.info("---")
  107. name = dist.get('name', '')
  108. required_by = [
  109. pkg.project_name for pkg in pkg_resources.working_set
  110. if name in [required.name for required in pkg.requires()]
  111. ]
  112. logger.info("Name: %s", name)
  113. logger.info("Version: %s", dist.get('version', ''))
  114. logger.info("Summary: %s", dist.get('summary', ''))
  115. logger.info("Home-page: %s", dist.get('home-page', ''))
  116. logger.info("Author: %s", dist.get('author', ''))
  117. logger.info("Author-email: %s", dist.get('author-email', ''))
  118. logger.info("License: %s", dist.get('license', ''))
  119. logger.info("Location: %s", dist.get('location', ''))
  120. logger.info("Requires: %s", ', '.join(dist.get('requires', [])))
  121. logger.info("Required-by: %s", ', '.join(required_by))
  122. if verbose:
  123. logger.info("Metadata-Version: %s",
  124. dist.get('metadata-version', ''))
  125. logger.info("Installer: %s", dist.get('installer', ''))
  126. logger.info("Classifiers:")
  127. for classifier in dist.get('classifiers', []):
  128. logger.info(" %s", classifier)
  129. logger.info("Entry-points:")
  130. for entry in dist.get('entry_points', []):
  131. logger.info(" %s", entry.strip())
  132. if list_files:
  133. logger.info("Files:")
  134. for line in dist.get('files', []):
  135. logger.info(" %s", line.strip())
  136. if "files" not in dist:
  137. logger.info("Cannot locate installed-files.txt")
  138. return results_printed