5
5
import textwrap
6
6
7
7
from pip .basecommand import Command , SUCCESS
8
- from pip .download import PipXmlrpcTransport
9
8
from pip .index import PyPI
10
9
from pip .utils import get_terminal_size
11
10
from pip .utils .logging import indent_log
12
11
from pip .exceptions import CommandError
13
12
from pip .status_codes import NO_MATCHES_FOUND
14
13
from pip ._vendor import pkg_resources
15
- from pip ._vendor .six .moves import xmlrpc_client
14
+
15
+ from pip .operations .search import highest_version , search , transform_hits
16
16
17
17
18
18
logger = logging .getLogger (__name__ )
@@ -40,7 +40,7 @@ def run(self, options, args):
40
40
if not args :
41
41
raise CommandError ('Missing required argument (search query).' )
42
42
query = args
43
- pypi_hits = self . search (query , options )
43
+ pypi_hits = search (query , options , self . _build_session )
44
44
hits = transform_hits (pypi_hits )
45
45
46
46
terminal_width = None
@@ -52,54 +52,6 @@ def run(self, options, args):
52
52
return SUCCESS
53
53
return NO_MATCHES_FOUND
54
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
55
104
56
def print_results (hits , name_column_width = None , terminal_width = None ):
105
57
if not hits :
@@ -131,9 +83,3 @@ def print_results(hits, name_column_width=None, terminal_width=None):
131
83
logger .info ('LATEST: %s' , latest )
132
84
except UnicodeEncodeError :
133
85
pass
134
-
135
-
136
- def highest_version (versions ):
137
- return next (iter (
138
- sorted (versions , key = pkg_resources .parse_version , reverse = True )
139
- ))
0 commit comments