Skip to content

Commit 6cd0f06

Browse files
committed
Merge pull request #2644 from xavfernandez/sort_locations
Index code simplification
2 parents c5d3dd2 + 2aa9f53 commit 6cd0f06

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

pip/index.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import logging
55
import cgi
6+
import itertools
67
import sys
78
import os
89
import re
@@ -138,7 +139,8 @@ def add_dependency_links(self, links):
138139
)
139140
self.dependency_links.extend(links)
140141

141-
def _sort_locations(self, locations):
142+
@staticmethod
143+
def _sort_locations(locations, expand_dir=False):
142144
"""
143145
Sort locations into "files" (archives) and "urls", and return
144146
a pair of lists (files,urls)
@@ -158,19 +160,19 @@ def sort_path(path):
158160

159161
is_local_path = os.path.exists(url)
160162
is_file_url = url.startswith('file:')
161-
is_find_link = url in self.find_links
162163

163164
if is_local_path or is_file_url:
164165
if is_local_path:
165166
path = url
166167
else:
167168
path = url_to_path(url)
168-
if is_find_link and os.path.isdir(path):
169-
path = os.path.realpath(path)
170-
for item in os.listdir(path):
171-
sort_path(os.path.join(path, item))
172-
elif is_file_url and os.path.isdir(path):
173-
urls.append(url)
169+
if os.path.isdir(path):
170+
if expand_dir:
171+
path = os.path.realpath(path)
172+
for item in os.listdir(path):
173+
sort_path(os.path.join(path, item))
174+
elif is_file_url:
175+
urls.append(url)
174176
elif os.path.isfile(path):
175177
sort_path(path)
176178
else:
@@ -342,31 +344,33 @@ def _find_all_versions(self, project_name):
342344
See _link_package_versions for details on which files are accepted
343345
"""
344346
index_locations = self._get_index_urls_locations(project_name)
345-
file_locations, url_locations = self._sort_locations(index_locations)
346-
fl_file_loc, fl_url_loc = self._sort_locations(self.find_links)
347-
file_locations.extend(fl_file_loc)
348-
url_locations.extend(fl_url_loc)
349-
350-
_flocations, _ulocations = self._sort_locations(self.dependency_links)
351-
file_locations.extend(_flocations)
347+
index_file_loc, index_url_loc = self._sort_locations(index_locations)
348+
fl_file_loc, fl_url_loc = self._sort_locations(
349+
self.find_links, expand_dir=True)
350+
dep_file_loc, dep_url_loc = self._sort_locations(self.dependency_links)
351+
352+
file_locations = (
353+
Link(url) for url in itertools.chain(
354+
index_file_loc, fl_file_loc, dep_file_loc)
355+
)
352356

353357
# We trust every url that the user has given us whether it was given
354358
# via --index-url or --find-links
355-
locations = [Link(url, trusted=True) for url in url_locations]
356-
357359
# We explicitly do not trust links that came from dependency_links
358-
locations.extend([Link(url) for url in _ulocations])
359-
360360
# We want to filter out any thing which does not have a secure origin.
361-
locations = [
362-
l for l in locations
363-
if self._validate_secure_origin(logger, l)
361+
url_locations = [
362+
link for link in itertools.chain(
363+
(Link(url, trusted=True) for url in index_url_loc),
364+
(Link(url, trusted=True) for url in fl_url_loc),
365+
(Link(url) for url in dep_url_loc),
366+
)
367+
if self._validate_secure_origin(logger, link)
364368
]
365369

366370
logger.debug('%d location(s) to search for versions of %s:',
367-
len(locations), project_name)
371+
len(url_locations), project_name)
368372

369-
for location in locations:
373+
for location in url_locations:
370374
logger.debug('* %s', location)
371375

372376
find_links_versions = list(self._package_versions(
@@ -376,7 +380,7 @@ def _find_all_versions(self, project_name):
376380
))
377381

378382
page_versions = []
379-
for page in self._get_pages(locations, project_name):
383+
for page in self._get_pages(url_locations, project_name):
380384
logger.debug('Analyzing links from page %s', page.url)
381385
with indent_log():
382386
page_versions.extend(
@@ -396,7 +400,7 @@ def _find_all_versions(self, project_name):
396400

397401
file_versions = list(
398402
self._package_versions(
399-
(Link(url) for url in file_locations),
403+
file_locations,
400404
project_name.lower()
401405
)
402406
)

tests/unit/test_index.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from pip.index import PackageFinder, Link, INSTALLED_VERSION
66

77

8-
def test_sort_locations_file_find_link(data):
8+
def test_sort_locations_file_expand_dir(data):
99
"""
10-
Test that a file:// find-link dir gets listdir run
10+
Test that a file:// dir gets listdir run with expand_dir
1111
"""
1212
finder = PackageFinder([data.find_links], [], session=PipSession())
13-
files, urls = finder._sort_locations([data.find_links])
13+
files, urls = finder._sort_locations([data.find_links], expand_dir=True)
1414
assert files and not urls, (
1515
"files and not urls should have been found at find-links url: %s" %
1616
data.find_links

0 commit comments

Comments
 (0)