From 0ae9562d88d7c67420f8a7b423ec63860c8cda50 Mon Sep 17 00:00:00 2001 From: Buck Golemon Date: Mon, 3 Nov 2014 18:11:02 -0800 Subject: [PATCH 1/2] attempt an optimistic localhost-only search for "==" requirements --- pip/index.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/pip/index.py b/pip/index.py index 65c985bbff9..ef33e8b43e2 100644 --- a/pip/index.py +++ b/pip/index.py @@ -225,6 +225,23 @@ def _warn_about_insecure_transport_scheme(self, logger, location): ctx) def find_requirement(self, req, upgrade): + if any(op == '==' for op, ver in req.req.specs): + # if the version is pinned-down by a ==, do an optimistic search + # for a satisfactory package on the local filesystem. + try: + result = self._find_requirement( + req, upgrade, network_allowed=False, + ) + except DistributionNotFound: + result = None + + if result is not None: + return result + + # otherwise, do the full network search + return self._find_requirement(req, upgrade, network_allowed=True) + + def _find_requirement(self, req, upgrade, network_allowed): def mkurl_pypi_url(url): loc = posixpath.join(url, url_name) @@ -248,7 +265,7 @@ def mkurl_pypi_url(url): trusted=True, ) - page = self._get_page(main_index_url, req) + page = self._get_page(main_index_url, req, network_allowed) if page is None: warnings.warn( "One or more of your dependencies required using a " @@ -295,7 +312,7 @@ def mkurl_pypi_url(url): ) ) page_versions = [] - for page in self._get_pages(locations, req): + for page in self._get_pages(locations, req, network_allowed): logger.debug('Analyzing links from page %s', page.url) with indent_log(): page_versions.extend( @@ -482,7 +499,7 @@ def _find_url_name(self, index_url, url_name, req): # Vaguely part of the PyPI API... weird but true. # FIXME: bad to modify this? index_url.url += '/' - page = self._get_page(index_url, req) + page = self._get_page(index_url, req, network_allowed=True) if page is None: logger.critical('Cannot fetch index base URL %s', index_url) return @@ -496,7 +513,7 @@ def _find_url_name(self, index_url, url_name, req): return base return None - def _get_pages(self, locations, req): + def _get_pages(self, locations, req, network_allowed): """ Yields (page, page_url) from the given locations, skipping locations that have errors, and adding download/homepage links @@ -510,7 +527,7 @@ def _get_pages(self, locations, req): continue seen.add(location) - page = self._get_page(location, req) + page = self._get_page(location, req, network_allowed) if page is None: continue @@ -727,8 +744,11 @@ def _egg_info_matches(self, egg_info, search_name, link): else: return None - def _get_page(self, link, req): - return HTMLPage.get_page(link, req, session=self.session) + def _get_page(self, link, req, network_allowed): + if network_allowed or link.url.startswith('file:'): + return HTMLPage.get_page(link, req, session=self.session) + else: + return HTMLPage('', 'fake://' + link.url) class HTMLPage(object): From 2cdf43a92a1b709a94167b97a4f8cf65a5fa8d11 Mon Sep 17 00:00:00 2001 From: Buck Golemon Date: Mon, 3 Nov 2014 18:24:18 -0800 Subject: [PATCH 2/2] _get_pages grew a third argument --- tests/unit/test_finder.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/unit/test_finder.py b/tests/unit/test_finder.py index 454f1320bb1..cabef26be54 100644 --- a/tests/unit/test_finder.py +++ b/tests/unit/test_finder.py @@ -283,7 +283,7 @@ def test_finder_priority_nonegg_over_eggfragments(): finder = PackageFinder(links, [], session=PipSession()) - with patch.object(finder, "_get_pages", lambda x, y: []): + with patch.object(finder, "_get_pages", lambda x, y, z: []): link = finder.find_requirement(req, False) assert link.url.endswith('tar.gz') @@ -291,7 +291,7 @@ def test_finder_priority_nonegg_over_eggfragments(): links.reverse() finder = PackageFinder(links, [], session=PipSession()) - with patch.object(finder, "_get_pages", lambda x, y: []): + with patch.object(finder, "_get_pages", lambda x, y, z: []): link = finder.find_requirement(req, False) assert link.url.endswith('tar.gz') @@ -313,14 +313,14 @@ def test_finder_only_installs_stable_releases(data): links = ["https://foo/bar-1.0.tar.gz", "https://foo/bar-2.0b1.tar.gz"] finder = PackageFinder(links, [], session=PipSession()) - with patch.object(finder, "_get_pages", lambda x, y: []): + with patch.object(finder, "_get_pages", lambda x, y, z: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-1.0.tar.gz" links.reverse() finder = PackageFinder(links, [], session=PipSession()) - with patch.object(finder, "_get_pages", lambda x, y: []): + with patch.object(finder, "_get_pages", lambda x, y, z: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-1.0.tar.gz" @@ -341,14 +341,14 @@ def test_finder_installs_pre_releases(data): links = ["https://foo/bar-1.0.tar.gz", "https://foo/bar-2.0b1.tar.gz"] finder = PackageFinder(links, [], session=PipSession()) - with patch.object(finder, "_get_pages", lambda x, y: []): + with patch.object(finder, "_get_pages", lambda x, y, z: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-2.0b1.tar.gz" links.reverse() finder = PackageFinder(links, [], session=PipSession()) - with patch.object(finder, "_get_pages", lambda x, y: []): + with patch.object(finder, "_get_pages", lambda x, y, z: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-2.0b1.tar.gz" @@ -375,14 +375,14 @@ def test_finder_installs_pre_releases_with_version_spec(): finder = PackageFinder(links, [], session=PipSession()) - with patch.object(finder, "_get_pages", lambda x, y: []): + with patch.object(finder, "_get_pages", lambda x, y, z: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-2.0b1.tar.gz" links.reverse() finder = PackageFinder(links, [], session=PipSession()) - with patch.object(finder, "_get_pages", lambda x, y: []): + with patch.object(finder, "_get_pages", lambda x, y, z: []): link = finder.find_requirement(req, False) assert link.url == "https://foo/bar-2.0b1.tar.gz"