Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. from __future__ import absolute_import
  2. import json
  3. import logging
  4. from pip._vendor import six
  5. from pip._vendor.six.moves import zip_longest
  6. from pip._internal.cli import cmdoptions
  7. from pip._internal.cli.base_command import Command
  8. from pip._internal.exceptions import CommandError
  9. from pip._internal.index import PackageFinder
  10. from pip._internal.utils.misc import (
  11. dist_is_editable, get_installed_distributions,
  12. )
  13. from pip._internal.utils.packaging import get_installer
  14. logger = logging.getLogger(__name__)
  15. class ListCommand(Command):
  16. """
  17. List installed packages, including editables.
  18. Packages are listed in a case-insensitive sorted order.
  19. """
  20. name = 'list'
  21. usage = """
  22. %prog [options]"""
  23. summary = 'List installed packages.'
  24. def __init__(self, *args, **kw):
  25. super(ListCommand, self).__init__(*args, **kw)
  26. cmd_opts = self.cmd_opts
  27. cmd_opts.add_option(
  28. '-o', '--outdated',
  29. action='store_true',
  30. default=False,
  31. help='List outdated packages')
  32. cmd_opts.add_option(
  33. '-u', '--uptodate',
  34. action='store_true',
  35. default=False,
  36. help='List uptodate packages')
  37. cmd_opts.add_option(
  38. '-e', '--editable',
  39. action='store_true',
  40. default=False,
  41. help='List editable projects.')
  42. cmd_opts.add_option(
  43. '-l', '--local',
  44. action='store_true',
  45. default=False,
  46. help=('If in a virtualenv that has global access, do not list '
  47. 'globally-installed packages.'),
  48. )
  49. self.cmd_opts.add_option(
  50. '--user',
  51. dest='user',
  52. action='store_true',
  53. default=False,
  54. help='Only output packages installed in user-site.')
  55. cmd_opts.add_option(
  56. '--pre',
  57. action='store_true',
  58. default=False,
  59. help=("Include pre-release and development versions. By default, "
  60. "pip only finds stable versions."),
  61. )
  62. cmd_opts.add_option(
  63. '--format',
  64. action='store',
  65. dest='list_format',
  66. default="columns",
  67. choices=('columns', 'freeze', 'json'),
  68. help="Select the output format among: columns (default), freeze, "
  69. "or json",
  70. )
  71. cmd_opts.add_option(
  72. '--not-required',
  73. action='store_true',
  74. dest='not_required',
  75. help="List packages that are not dependencies of "
  76. "installed packages.",
  77. )
  78. cmd_opts.add_option(
  79. '--exclude-editable',
  80. action='store_false',
  81. dest='include_editable',
  82. help='Exclude editable package from output.',
  83. )
  84. cmd_opts.add_option(
  85. '--include-editable',
  86. action='store_true',
  87. dest='include_editable',
  88. help='Include editable package from output.',
  89. default=True,
  90. )
  91. index_opts = cmdoptions.make_option_group(
  92. cmdoptions.index_group, self.parser
  93. )
  94. self.parser.insert_option_group(0, index_opts)
  95. self.parser.insert_option_group(0, cmd_opts)
  96. def _build_package_finder(self, options, index_urls, session):
  97. """
  98. Create a package finder appropriate to this list command.
  99. """
  100. return PackageFinder(
  101. find_links=options.find_links,
  102. index_urls=index_urls,
  103. allow_all_prereleases=options.pre,
  104. trusted_hosts=options.trusted_hosts,
  105. process_dependency_links=options.process_dependency_links,
  106. session=session,
  107. )
  108. def run(self, options, args):
  109. if options.outdated and options.uptodate:
  110. raise CommandError(
  111. "Options --outdated and --uptodate cannot be combined.")
  112. packages = get_installed_distributions(
  113. local_only=options.local,
  114. user_only=options.user,
  115. editables_only=options.editable,
  116. include_editables=options.include_editable,
  117. )
  118. if options.outdated:
  119. packages = self.get_outdated(packages, options)
  120. elif options.uptodate:
  121. packages = self.get_uptodate(packages, options)
  122. if options.not_required:
  123. packages = self.get_not_required(packages, options)
  124. self.output_package_listing(packages, options)
  125. def get_outdated(self, packages, options):
  126. return [
  127. dist for dist in self.iter_packages_latest_infos(packages, options)
  128. if dist.latest_version > dist.parsed_version
  129. ]
  130. def get_uptodate(self, packages, options):
  131. return [
  132. dist for dist in self.iter_packages_latest_infos(packages, options)
  133. if dist.latest_version == dist.parsed_version
  134. ]
  135. def get_not_required(self, packages, options):
  136. dep_keys = set()
  137. for dist in packages:
  138. dep_keys.update(requirement.key for requirement in dist.requires())
  139. return {pkg for pkg in packages if pkg.key not in dep_keys}
  140. def iter_packages_latest_infos(self, packages, options):
  141. index_urls = [options.index_url] + options.extra_index_urls
  142. if options.no_index:
  143. logger.debug('Ignoring indexes: %s', ','.join(index_urls))
  144. index_urls = []
  145. dependency_links = []
  146. for dist in packages:
  147. if dist.has_metadata('dependency_links.txt'):
  148. dependency_links.extend(
  149. dist.get_metadata_lines('dependency_links.txt'),
  150. )
  151. with self._build_session(options) as session:
  152. finder = self._build_package_finder(options, index_urls, session)
  153. finder.add_dependency_links(dependency_links)
  154. for dist in packages:
  155. typ = 'unknown'
  156. all_candidates = finder.find_all_candidates(dist.key)
  157. if not options.pre:
  158. # Remove prereleases
  159. all_candidates = [candidate for candidate in all_candidates
  160. if not candidate.version.is_prerelease]
  161. if not all_candidates:
  162. continue
  163. best_candidate = max(all_candidates,
  164. key=finder._candidate_sort_key)
  165. remote_version = best_candidate.version
  166. if best_candidate.location.is_wheel:
  167. typ = 'wheel'
  168. else:
  169. typ = 'sdist'
  170. # This is dirty but makes the rest of the code much cleaner
  171. dist.latest_version = remote_version
  172. dist.latest_filetype = typ
  173. yield dist
  174. def output_package_listing(self, packages, options):
  175. packages = sorted(
  176. packages,
  177. key=lambda dist: dist.project_name.lower(),
  178. )
  179. if options.list_format == 'columns' and packages:
  180. data, header = format_for_columns(packages, options)
  181. self.output_package_listing_columns(data, header)
  182. elif options.list_format == 'freeze':
  183. for dist in packages:
  184. if options.verbose >= 1:
  185. logger.info("%s==%s (%s)", dist.project_name,
  186. dist.version, dist.location)
  187. else:
  188. logger.info("%s==%s", dist.project_name, dist.version)
  189. elif options.list_format == 'json':
  190. logger.info(format_for_json(packages, options))
  191. def output_package_listing_columns(self, data, header):
  192. # insert the header first: we need to know the size of column names
  193. if len(data) > 0:
  194. data.insert(0, header)
  195. pkg_strings, sizes = tabulate(data)
  196. # Create and add a separator.
  197. if len(data) > 0:
  198. pkg_strings.insert(1, " ".join(map(lambda x: '-' * x, sizes)))
  199. for val in pkg_strings:
  200. logger.info(val)
  201. def tabulate(vals):
  202. # From pfmoore on GitHub:
  203. # https://github.com/pypa/pip/issues/3651#issuecomment-216932564
  204. assert len(vals) > 0
  205. sizes = [0] * max(len(x) for x in vals)
  206. for row in vals:
  207. sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
  208. result = []
  209. for row in vals:
  210. display = " ".join([str(c).ljust(s) if c is not None else ''
  211. for s, c in zip_longest(sizes, row)])
  212. result.append(display)
  213. return result, sizes
  214. def format_for_columns(pkgs, options):
  215. """
  216. Convert the package data into something usable
  217. by output_package_listing_columns.
  218. """
  219. running_outdated = options.outdated
  220. # Adjust the header for the `pip list --outdated` case.
  221. if running_outdated:
  222. header = ["Package", "Version", "Latest", "Type"]
  223. else:
  224. header = ["Package", "Version"]
  225. data = []
  226. if options.verbose >= 1 or any(dist_is_editable(x) for x in pkgs):
  227. header.append("Location")
  228. if options.verbose >= 1:
  229. header.append("Installer")
  230. for proj in pkgs:
  231. # if we're working on the 'outdated' list, separate out the
  232. # latest_version and type
  233. row = [proj.project_name, proj.version]
  234. if running_outdated:
  235. row.append(proj.latest_version)
  236. row.append(proj.latest_filetype)
  237. if options.verbose >= 1 or dist_is_editable(proj):
  238. row.append(proj.location)
  239. if options.verbose >= 1:
  240. row.append(get_installer(proj))
  241. data.append(row)
  242. return data, header
  243. def format_for_json(packages, options):
  244. data = []
  245. for dist in packages:
  246. info = {
  247. 'name': dist.project_name,
  248. 'version': six.text_type(dist.version),
  249. }
  250. if options.verbose >= 1:
  251. info['location'] = dist.location
  252. info['installer'] = get_installer(dist)
  253. if options.outdated:
  254. info['latest_version'] = six.text_type(dist.latest_version)
  255. info['latest_filetype'] = dist.latest_filetype
  256. data.append(info)
  257. return json.dumps(data)