search.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from __future__ import absolute_import
  2. import logging
  3. import sys
  4. import textwrap
  5. from collections import OrderedDict
  6. from pip._vendor import pkg_resources
  7. from pip._vendor.packaging.version import parse as parse_version
  8. # NOTE: XMLRPC Client is not annotated in typeshed as on 2017-07-17, which is
  9. # why we ignore the type on this import
  10. from pip._vendor.six.moves import xmlrpc_client # type: ignore
  11. from pip._internal.basecommand import SUCCESS, Command
  12. from pip._internal.compat import get_terminal_size
  13. from pip._internal.download import PipXmlrpcTransport
  14. from pip._internal.exceptions import CommandError
  15. from pip._internal.models import PyPI
  16. from pip._internal.status_codes import NO_MATCHES_FOUND
  17. from pip._internal.utils.logging import indent_log
  18. logger = logging.getLogger(__name__)
  19. class SearchCommand(Command):
  20. """Search for PyPI packages whose name or summary contains <query>."""
  21. name = 'search'
  22. usage = """
  23. %prog [options] <query>"""
  24. summary = 'Search PyPI for packages.'
  25. ignore_require_venv = True
  26. def __init__(self, *args, **kw):
  27. super(SearchCommand, self).__init__(*args, **kw)
  28. self.cmd_opts.add_option(
  29. '-i', '--index',
  30. dest='index',
  31. metavar='URL',
  32. default=PyPI.pypi_url,
  33. help='Base URL of Python Package Index (default %default)')
  34. self.parser.insert_option_group(0, self.cmd_opts)
  35. def run(self, options, args):
  36. if not args:
  37. raise CommandError('Missing required argument (search query).')
  38. query = args
  39. pypi_hits = self.search(query, options)
  40. hits = transform_hits(pypi_hits)
  41. terminal_width = None
  42. if sys.stdout.isatty():
  43. terminal_width = get_terminal_size()[0]
  44. print_results(hits, terminal_width=terminal_width)
  45. if pypi_hits:
  46. return SUCCESS
  47. return NO_MATCHES_FOUND
  48. def search(self, query, options):
  49. index_url = options.index
  50. with self._build_session(options) as session:
  51. transport = PipXmlrpcTransport(index_url, session)
  52. pypi = xmlrpc_client.ServerProxy(index_url, transport)
  53. hits = pypi.search({'name': query, 'summary': query}, 'or')
  54. return hits
  55. def transform_hits(hits):
  56. """
  57. The list from pypi is really a list of versions. We want a list of
  58. packages with the list of versions stored inline. This converts the
  59. list from pypi into one we can use.
  60. """
  61. packages = OrderedDict()
  62. for hit in hits:
  63. name = hit['name']
  64. summary = hit['summary']
  65. version = hit['version']
  66. if name not in packages.keys():
  67. packages[name] = {
  68. 'name': name,
  69. 'summary': summary,
  70. 'versions': [version],
  71. }
  72. else:
  73. packages[name]['versions'].append(version)
  74. # if this is the highest version, replace summary and score
  75. if version == highest_version(packages[name]['versions']):
  76. packages[name]['summary'] = summary
  77. return list(packages.values())
  78. def print_results(hits, name_column_width=None, terminal_width=None):
  79. if not hits:
  80. return
  81. if name_column_width is None:
  82. name_column_width = max([
  83. len(hit['name']) + len(highest_version(hit.get('versions', ['-'])))
  84. for hit in hits
  85. ]) + 4
  86. installed_packages = [p.project_name for p in pkg_resources.working_set]
  87. for hit in hits:
  88. name = hit['name']
  89. summary = hit['summary'] or ''
  90. latest = highest_version(hit.get('versions', ['-']))
  91. if terminal_width is not None:
  92. target_width = terminal_width - name_column_width - 5
  93. if target_width > 10:
  94. # wrap and indent summary to fit terminal
  95. summary = textwrap.wrap(summary, target_width)
  96. summary = ('\n' + ' ' * (name_column_width + 3)).join(summary)
  97. line = '%-*s - %s' % (name_column_width,
  98. '%s (%s)' % (name, latest), summary)
  99. try:
  100. logger.info(line)
  101. if name in installed_packages:
  102. dist = pkg_resources.get_distribution(name)
  103. with indent_log():
  104. if dist.version == latest:
  105. logger.info('INSTALLED: %s (latest)', dist.version)
  106. else:
  107. logger.info('INSTALLED: %s', dist.version)
  108. logger.info('LATEST: %s', latest)
  109. except UnicodeEncodeError:
  110. pass
  111. def highest_version(versions):
  112. return max(versions, key=parse_version)