Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/pip/commands/search.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
| author | bcclaywell |
|---|---|
| date | Mon, 12 Oct 2015 17:43:33 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d67268158946 |
|---|---|
| 1 from __future__ import absolute_import | |
| 2 | |
| 3 import logging | |
| 4 import sys | |
| 5 import textwrap | |
| 6 | |
| 7 from pip.basecommand import Command, SUCCESS | |
| 8 from pip.download import PipXmlrpcTransport | |
| 9 from pip.index import PyPI | |
| 10 from pip.utils import get_terminal_size | |
| 11 from pip.utils.logging import indent_log | |
| 12 from pip.exceptions import CommandError | |
| 13 from pip.status_codes import NO_MATCHES_FOUND | |
| 14 from pip._vendor import pkg_resources | |
| 15 from pip._vendor.six.moves import xmlrpc_client | |
| 16 | |
| 17 | |
| 18 logger = logging.getLogger(__name__) | |
| 19 | |
| 20 | |
| 21 class SearchCommand(Command): | |
| 22 """Search for PyPI packages whose name or summary contains <query>.""" | |
| 23 name = 'search' | |
| 24 usage = """ | |
| 25 %prog [options] <query>""" | |
| 26 summary = 'Search PyPI for packages.' | |
| 27 | |
| 28 def __init__(self, *args, **kw): | |
| 29 super(SearchCommand, self).__init__(*args, **kw) | |
| 30 self.cmd_opts.add_option( | |
| 31 '--index', | |
| 32 dest='index', | |
| 33 metavar='URL', | |
| 34 default=PyPI.pypi_url, | |
| 35 help='Base URL of Python Package Index (default %default)') | |
| 36 | |
| 37 self.parser.insert_option_group(0, self.cmd_opts) | |
| 38 | |
| 39 def run(self, options, args): | |
| 40 if not args: | |
| 41 raise CommandError('Missing required argument (search query).') | |
| 42 query = args | |
| 43 pypi_hits = self.search(query, options) | |
| 44 hits = transform_hits(pypi_hits) | |
| 45 | |
| 46 terminal_width = None | |
| 47 if sys.stdout.isatty(): | |
| 48 terminal_width = get_terminal_size()[0] | |
| 49 | |
| 50 print_results(hits, terminal_width=terminal_width) | |
| 51 if pypi_hits: | |
| 52 return SUCCESS | |
| 53 return NO_MATCHES_FOUND | |
| 54 | |
| 55 def search(self, query, options): | |
| 56 index_url = options.index | |
| 57 with self._build_session(options) as session: | |
| 58 transport = PipXmlrpcTransport(index_url, session) | |
| 59 pypi = xmlrpc_client.ServerProxy(index_url, transport) | |
| 60 hits = pypi.search({'name': query, 'summary': query}, 'or') | |
| 61 return hits | |
| 62 | |
| 63 | |
| 64 def transform_hits(hits): | |
| 65 """ | |
| 66 The list from pypi is really a list of versions. We want a list of | |
| 67 packages with the list of versions stored inline. This converts the | |
| 68 list from pypi into one we can use. | |
| 69 """ | |
| 70 packages = {} | |
| 71 for hit in hits: | |
| 72 name = hit['name'] | |
| 73 summary = hit['summary'] | |
| 74 version = hit['version'] | |
| 75 score = hit['_pypi_ordering'] | |
| 76 if score is None: | |
| 77 score = 0 | |
| 78 | |
| 79 if name not in packages.keys(): | |
| 80 packages[name] = { | |
| 81 'name': name, | |
| 82 'summary': summary, | |
| 83 'versions': [version], | |
| 84 'score': score, | |
| 85 } | |
| 86 else: | |
| 87 packages[name]['versions'].append(version) | |
| 88 | |
| 89 # if this is the highest version, replace summary and score | |
| 90 if version == highest_version(packages[name]['versions']): | |
| 91 packages[name]['summary'] = summary | |
| 92 packages[name]['score'] = score | |
| 93 | |
| 94 # each record has a unique name now, so we will convert the dict into a | |
| 95 # list sorted by score | |
| 96 package_list = sorted( | |
| 97 packages.values(), | |
| 98 key=lambda x: x['score'], | |
| 99 reverse=True, | |
| 100 ) | |
| 101 return package_list | |
| 102 | |
| 103 | |
| 104 def print_results(hits, name_column_width=None, terminal_width=None): | |
| 105 if not hits: | |
| 106 return | |
| 107 if name_column_width is None: | |
| 108 name_column_width = max((len(hit['name']) for hit in hits)) + 4 | |
| 109 installed_packages = [p.project_name for p in pkg_resources.working_set] | |
| 110 for hit in hits: | |
| 111 name = hit['name'] | |
| 112 summary = hit['summary'] or '' | |
| 113 if terminal_width is not None: | |
| 114 # wrap and indent summary to fit terminal | |
| 115 summary = textwrap.wrap( | |
| 116 summary, | |
| 117 terminal_width - name_column_width - 5, | |
| 118 ) | |
| 119 summary = ('\n' + ' ' * (name_column_width + 3)).join(summary) | |
| 120 line = '%s - %s' % (name.ljust(name_column_width), summary) | |
| 121 try: | |
| 122 logger.info(line) | |
| 123 if name in installed_packages: | |
| 124 dist = pkg_resources.get_distribution(name) | |
| 125 with indent_log(): | |
| 126 latest = highest_version(hit['versions']) | |
| 127 if dist.version == latest: | |
| 128 logger.info('INSTALLED: %s (latest)', dist.version) | |
| 129 else: | |
| 130 logger.info('INSTALLED: %s', dist.version) | |
| 131 logger.info('LATEST: %s', latest) | |
| 132 except UnicodeEncodeError: | |
| 133 pass | |
| 134 | |
| 135 | |
| 136 def highest_version(versions): | |
| 137 return next(iter( | |
| 138 sorted(versions, key=pkg_resources.parse_version, reverse=True) | |
| 139 )) |
