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