show.py 6.8 KB

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