Mercurial > repos > guerler > hhblits
comparison lib/python3.8/site-packages/pip/_internal/commands/search.py @ 0:9e54283cc701 draft
"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author | guerler |
---|---|
date | Mon, 27 Jul 2020 03:47:31 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9e54283cc701 |
---|---|
1 # The following comment should be removed at some point in the future. | |
2 # mypy: disallow-untyped-defs=False | |
3 | |
4 from __future__ import absolute_import | |
5 | |
6 import logging | |
7 import sys | |
8 import textwrap | |
9 from collections import OrderedDict | |
10 | |
11 from pip._vendor import pkg_resources | |
12 from pip._vendor.packaging.version import parse as parse_version | |
13 # NOTE: XMLRPC Client is not annotated in typeshed as on 2017-07-17, which is | |
14 # why we ignore the type on this import | |
15 from pip._vendor.six.moves import xmlrpc_client # type: ignore | |
16 | |
17 from pip._internal.cli.base_command import Command | |
18 from pip._internal.cli.req_command import SessionCommandMixin | |
19 from pip._internal.cli.status_codes import NO_MATCHES_FOUND, SUCCESS | |
20 from pip._internal.exceptions import CommandError | |
21 from pip._internal.models.index import PyPI | |
22 from pip._internal.network.xmlrpc import PipXmlrpcTransport | |
23 from pip._internal.utils.compat import get_terminal_size | |
24 from pip._internal.utils.logging import indent_log | |
25 from pip._internal.utils.misc import write_output | |
26 | |
27 logger = logging.getLogger(__name__) | |
28 | |
29 | |
30 class SearchCommand(Command, SessionCommandMixin): | |
31 """Search for PyPI packages whose name or summary contains <query>.""" | |
32 | |
33 usage = """ | |
34 %prog [options] <query>""" | |
35 ignore_require_venv = True | |
36 | |
37 def __init__(self, *args, **kw): | |
38 super(SearchCommand, self).__init__(*args, **kw) | |
39 self.cmd_opts.add_option( | |
40 '-i', '--index', | |
41 dest='index', | |
42 metavar='URL', | |
43 default=PyPI.pypi_url, | |
44 help='Base URL of Python Package Index (default %default)') | |
45 | |
46 self.parser.insert_option_group(0, self.cmd_opts) | |
47 | |
48 def run(self, options, args): | |
49 if not args: | |
50 raise CommandError('Missing required argument (search query).') | |
51 query = args | |
52 pypi_hits = self.search(query, options) | |
53 hits = transform_hits(pypi_hits) | |
54 | |
55 terminal_width = None | |
56 if sys.stdout.isatty(): | |
57 terminal_width = get_terminal_size()[0] | |
58 | |
59 print_results(hits, terminal_width=terminal_width) | |
60 if pypi_hits: | |
61 return SUCCESS | |
62 return NO_MATCHES_FOUND | |
63 | |
64 def search(self, query, options): | |
65 index_url = options.index | |
66 | |
67 session = self.get_default_session(options) | |
68 | |
69 transport = PipXmlrpcTransport(index_url, session) | |
70 pypi = xmlrpc_client.ServerProxy(index_url, transport) | |
71 hits = pypi.search({'name': query, 'summary': query}, 'or') | |
72 return hits | |
73 | |
74 | |
75 def transform_hits(hits): | |
76 """ | |
77 The list from pypi is really a list of versions. We want a list of | |
78 packages with the list of versions stored inline. This converts the | |
79 list from pypi into one we can use. | |
80 """ | |
81 packages = OrderedDict() | |
82 for hit in hits: | |
83 name = hit['name'] | |
84 summary = hit['summary'] | |
85 version = hit['version'] | |
86 | |
87 if name not in packages.keys(): | |
88 packages[name] = { | |
89 'name': name, | |
90 'summary': summary, | |
91 'versions': [version], | |
92 } | |
93 else: | |
94 packages[name]['versions'].append(version) | |
95 | |
96 # if this is the highest version, replace summary and score | |
97 if version == highest_version(packages[name]['versions']): | |
98 packages[name]['summary'] = summary | |
99 | |
100 return list(packages.values()) | |
101 | |
102 | |
103 def print_results(hits, name_column_width=None, terminal_width=None): | |
104 if not hits: | |
105 return | |
106 if name_column_width is None: | |
107 name_column_width = max([ | |
108 len(hit['name']) + len(highest_version(hit.get('versions', ['-']))) | |
109 for hit in hits | |
110 ]) + 4 | |
111 | |
112 installed_packages = [p.project_name for p in pkg_resources.working_set] | |
113 for hit in hits: | |
114 name = hit['name'] | |
115 summary = hit['summary'] or '' | |
116 latest = highest_version(hit.get('versions', ['-'])) | |
117 if terminal_width is not None: | |
118 target_width = terminal_width - name_column_width - 5 | |
119 if target_width > 10: | |
120 # wrap and indent summary to fit terminal | |
121 summary = textwrap.wrap(summary, target_width) | |
122 summary = ('\n' + ' ' * (name_column_width + 3)).join(summary) | |
123 | |
124 line = '%-*s - %s' % (name_column_width, | |
125 '%s (%s)' % (name, latest), summary) | |
126 try: | |
127 write_output(line) | |
128 if name in installed_packages: | |
129 dist = pkg_resources.get_distribution(name) | |
130 with indent_log(): | |
131 if dist.version == latest: | |
132 write_output('INSTALLED: %s (latest)', dist.version) | |
133 else: | |
134 write_output('INSTALLED: %s', dist.version) | |
135 if parse_version(latest).pre: | |
136 write_output('LATEST: %s (pre-release; install' | |
137 ' with "pip install --pre")', latest) | |
138 else: | |
139 write_output('LATEST: %s', latest) | |
140 except UnicodeEncodeError: | |
141 pass | |
142 | |
143 | |
144 def highest_version(versions): | |
145 return max(versions, key=parse_version) |