2010-02-22 23:03:05 +01:00
|
|
|
import sys
|
|
|
|
import textwrap
|
|
|
|
import pkg_resources
|
2010-07-02 14:27:22 +02:00
|
|
|
import pip.download
|
2011-08-02 19:54:18 +02:00
|
|
|
from pip.basecommand import Command, SUCCESS
|
2010-02-22 23:03:05 +01:00
|
|
|
from pip.util import get_terminal_size
|
2010-02-22 23:43:49 +01:00
|
|
|
from pip.log import logger
|
2011-03-15 20:49:48 +01:00
|
|
|
from pip.backwardcompat import xmlrpclib, reduce, cmp
|
2011-07-20 21:04:11 +02:00
|
|
|
from pip.exceptions import CommandError
|
2011-12-04 21:50:34 +01:00
|
|
|
from pip.status_codes import NO_MATCHES_FOUND
|
2010-02-22 23:03:05 +01:00
|
|
|
from distutils.version import StrictVersion, LooseVersion
|
|
|
|
|
2011-08-02 19:16:18 +02:00
|
|
|
|
2010-02-22 23:03:05 +01:00
|
|
|
class SearchCommand(Command):
|
|
|
|
name = 'search'
|
|
|
|
usage = '%prog QUERY'
|
|
|
|
summary = 'Search PyPI'
|
|
|
|
|
2012-11-10 05:15:45 +01:00
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
super(SearchCommand, self).__init__(*args, **kw)
|
2012-12-10 02:28:50 +01:00
|
|
|
self.cmd_opts.add_option(
|
2010-02-22 23:03:05 +01:00
|
|
|
'--index',
|
|
|
|
dest='index',
|
|
|
|
metavar='URL',
|
|
|
|
default='http://pypi.python.org/pypi',
|
|
|
|
help='Base URL of Python Package Index (default %default)')
|
|
|
|
|
2012-12-10 02:28:50 +01:00
|
|
|
self.parser.add_option_group(self.cmd_opts)
|
|
|
|
|
2010-02-22 23:03:05 +01:00
|
|
|
def run(self, options, args):
|
|
|
|
if not args:
|
2011-07-20 21:04:11 +02:00
|
|
|
raise CommandError('Missing required argument (search query).')
|
2011-04-16 18:00:10 +02:00
|
|
|
query = args
|
2010-02-22 23:03:05 +01:00
|
|
|
index_url = options.index
|
|
|
|
|
|
|
|
pypi_hits = self.search(query, index_url)
|
2010-02-23 22:01:43 +01:00
|
|
|
hits = transform_hits(pypi_hits)
|
2010-02-22 23:03:05 +01:00
|
|
|
|
2010-02-22 23:37:19 +01:00
|
|
|
terminal_width = None
|
2010-02-22 23:03:05 +01:00
|
|
|
if sys.stdout.isatty():
|
2010-04-15 17:59:55 +02:00
|
|
|
terminal_width = get_terminal_size()[0]
|
2010-02-22 23:03:05 +01:00
|
|
|
|
|
|
|
print_results(hits, terminal_width=terminal_width)
|
2011-08-02 19:16:18 +02:00
|
|
|
if pypi_hits:
|
|
|
|
return SUCCESS
|
|
|
|
return NO_MATCHES_FOUND
|
2010-02-22 23:03:05 +01:00
|
|
|
|
|
|
|
def search(self, query, index_url):
|
2010-07-02 14:27:22 +02:00
|
|
|
pypi = xmlrpclib.ServerProxy(index_url, pip.download.xmlrpclib_transport)
|
2010-02-22 23:03:05 +01:00
|
|
|
hits = pypi.search({'name': query, 'summary': query}, 'or')
|
|
|
|
return hits
|
|
|
|
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2010-02-23 22:01:43 +01:00
|
|
|
def transform_hits(hits):
|
2010-02-22 23:03:05 +01:00
|
|
|
"""
|
|
|
|
The list from pypi is really a list of versions. We want a list of
|
|
|
|
packages with the list of versions stored inline. This converts the
|
|
|
|
list from pypi into one we can use.
|
|
|
|
"""
|
|
|
|
packages = {}
|
|
|
|
for hit in hits:
|
|
|
|
name = hit['name']
|
|
|
|
summary = hit['summary']
|
|
|
|
version = hit['version']
|
|
|
|
score = hit['_pypi_ordering']
|
2012-03-18 11:33:13 +01:00
|
|
|
if score is None:
|
|
|
|
score = 0
|
2010-02-22 23:03:05 +01:00
|
|
|
|
|
|
|
if name not in packages.keys():
|
|
|
|
packages[name] = {'name': name, 'summary': summary, 'versions': [version], 'score': score}
|
|
|
|
else:
|
|
|
|
packages[name]['versions'].append(version)
|
|
|
|
|
|
|
|
# if this is the highest version, replace summary and score
|
|
|
|
if version == highest_version(packages[name]['versions']):
|
|
|
|
packages[name]['summary'] = summary
|
|
|
|
packages[name]['score'] = score
|
|
|
|
|
|
|
|
# each record has a unique name now, so we will convert the dict into a list sorted by score
|
2011-03-15 20:49:48 +01:00
|
|
|
package_list = sorted(packages.values(), key=lambda x: x['score'], reverse=True)
|
2010-02-22 23:03:05 +01:00
|
|
|
return package_list
|
|
|
|
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2010-02-22 23:03:05 +01:00
|
|
|
def print_results(hits, name_column_width=25, terminal_width=None):
|
|
|
|
installed_packages = [p.project_name for p in pkg_resources.working_set]
|
|
|
|
for hit in hits:
|
|
|
|
name = hit['name']
|
|
|
|
summary = hit['summary'] or ''
|
|
|
|
if terminal_width is not None:
|
|
|
|
# wrap and indent summary to fit terminal
|
|
|
|
summary = textwrap.wrap(summary, terminal_width - name_column_width - 5)
|
|
|
|
summary = ('\n' + ' ' * (name_column_width + 3)).join(summary)
|
|
|
|
line = '%s - %s' % (name.ljust(name_column_width), summary)
|
|
|
|
try:
|
2010-02-22 23:43:49 +01:00
|
|
|
logger.notify(line)
|
2010-02-22 23:03:05 +01:00
|
|
|
if name in installed_packages:
|
|
|
|
dist = pkg_resources.get_distribution(name)
|
2010-02-23 00:10:56 +01:00
|
|
|
logger.indent += 2
|
|
|
|
try:
|
2010-02-23 00:20:19 +01:00
|
|
|
latest = highest_version(hit['versions'])
|
|
|
|
if dist.version == latest:
|
|
|
|
logger.notify('INSTALLED: %s (latest)' % dist.version)
|
|
|
|
else:
|
|
|
|
logger.notify('INSTALLED: %s' % dist.version)
|
|
|
|
logger.notify('LATEST: %s' % latest)
|
2010-02-23 00:10:56 +01:00
|
|
|
finally:
|
|
|
|
logger.indent -= 2
|
2010-02-22 23:03:05 +01:00
|
|
|
except UnicodeEncodeError:
|
|
|
|
pass
|
|
|
|
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2010-02-22 23:03:05 +01:00
|
|
|
def compare_versions(version1, version2):
|
|
|
|
try:
|
|
|
|
return cmp(StrictVersion(version1), StrictVersion(version2))
|
|
|
|
# in case of abnormal version number, fall back to LooseVersion
|
|
|
|
except ValueError:
|
Work around failing test due to distutils.version issue
In py3k the following fails, handle this and use string comparison in worst
case.
Tested on 2.6 and 3.2
>> from distutils.version import LooseVersion as LV
>>> v1, v2, v3 = (LV('1.1'), LV('1.1.2'), LV('1.1a1'))
>>> v1 > v2
False
>>> v1 > v3
False
>>> v2 > v3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "distutils/version.py", line 70, in __gt__
c = self._cmp(other)
File "distutils/version.py", line 343, in _cmp
if self.version < other.version:
TypeError: unorderable types: int() < str()
2011-04-16 22:57:22 +02:00
|
|
|
pass
|
|
|
|
try:
|
2010-02-22 23:03:05 +01:00
|
|
|
return cmp(LooseVersion(version1), LooseVersion(version2))
|
Work around failing test due to distutils.version issue
In py3k the following fails, handle this and use string comparison in worst
case.
Tested on 2.6 and 3.2
>> from distutils.version import LooseVersion as LV
>>> v1, v2, v3 = (LV('1.1'), LV('1.1.2'), LV('1.1a1'))
>>> v1 > v2
False
>>> v1 > v3
False
>>> v2 > v3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "distutils/version.py", line 70, in __gt__
c = self._cmp(other)
File "distutils/version.py", line 343, in _cmp
if self.version < other.version:
TypeError: unorderable types: int() < str()
2011-04-16 22:57:22 +02:00
|
|
|
except TypeError:
|
|
|
|
# certain LooseVersion comparions raise due to unorderable types,
|
|
|
|
# fallback to string comparison
|
|
|
|
return cmp([str(v) for v in LooseVersion(version1).version],
|
|
|
|
[str(v) for v in LooseVersion(version2).version])
|
2010-02-22 23:03:05 +01:00
|
|
|
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2010-02-22 23:03:05 +01:00
|
|
|
def highest_version(versions):
|
|
|
|
return reduce((lambda v1, v2: compare_versions(v1, v2) == 1 and v1 or v2), versions)
|