Last active
August 29, 2015 14:00
-
-
Save dstufft/11237512 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/pip/cmdoptions.py b/pip/cmdoptions.py | |
| index 85b9abc..809d3a5 100644 | |
| --- a/pip/cmdoptions.py | |
| +++ b/pip/cmdoptions.py | |
| @@ -297,9 +297,8 @@ no_use_wheel = OptionMaker( | |
| download_cache = OptionMaker( | |
| '--download-cache', | |
| dest='download_cache', | |
| - metavar='dir', | |
| default=None, | |
| - help='Cache downloaded packages in <dir>.') | |
| + help=SUPPRESS_HELP) | |
| no_deps = OptionMaker( | |
| '--no-deps', '--no-dependencies', | |
| diff --git a/pip/commands/install.py b/pip/commands/install.py | |
| index 850a6ff..6d727c3 100644 | |
| --- a/pip/commands/install.py | |
| +++ b/pip/commands/install.py | |
| @@ -266,6 +266,14 @@ class InstallCommand(Command): | |
| ) | |
| index_urls += options.mirrors | |
| + if options.download_cache: | |
| + logger.deprecated( | |
| + "1.8", | |
| + "--download-cache has been deprecated and will be removed in " | |
| + " the future. Pip now has a generic HTTP Cache which will be " | |
| + "used automatically." | |
| + ) | |
| + | |
| session = self._build_session(options) | |
| finder = self._build_package_finder(options, index_urls, session) | |
| @@ -274,7 +282,6 @@ class InstallCommand(Command): | |
| build_dir=options.build_dir, | |
| src_dir=options.src_dir, | |
| download_dir=options.download_dir, | |
| - download_cache=options.download_cache, | |
| upgrade=options.upgrade, | |
| as_egg=options.as_egg, | |
| ignore_installed=options.ignore_installed, | |
| diff --git a/pip/commands/wheel.py b/pip/commands/wheel.py | |
| index 5536816..de418d5 100644 | |
| --- a/pip/commands/wheel.py | |
| +++ b/pip/commands/wheel.py | |
| @@ -142,6 +142,14 @@ class WheelCommand(Command): | |
| ) | |
| index_urls += options.mirrors | |
| + if options.download_cache: | |
| + logger.deprecated( | |
| + "1.8", | |
| + "--download-cache has been deprecated and will be removed in " | |
| + " the future. Pip now has a generic HTTP Cache which will be " | |
| + "used automatically." | |
| + ) | |
| + | |
| session = self._build_session(options) | |
| finder = PackageFinder( | |
| @@ -160,7 +168,6 @@ class WheelCommand(Command): | |
| build_dir=options.build_dir, | |
| src_dir=None, | |
| download_dir=None, | |
| - download_cache=options.download_cache, | |
| ignore_dependencies=options.ignore_dependencies, | |
| ignore_installed=True, | |
| session=session, | |
| diff --git a/pip/download.py b/pip/download.py | |
| index 4c23f5e..77a0403 100644 | |
| --- a/pip/download.py | |
| +++ b/pip/download.py | |
| @@ -15,9 +15,9 @@ import pip | |
| from pip.backwardcompat import urllib, urlparse, raw_input | |
| from pip.exceptions import InstallationError, HashMismatch | |
| from pip.util import (splitext, rmtree, format_size, display_path, | |
| - backup_dir, ask_path_exists, unpack_file, | |
| - create_download_cache_folder, cache_download) | |
| + backup_dir, ask_path_exists, unpack_file) | |
| from pip.vcs import vcs | |
| +from pip.locations import HTTP_CACHE_DIR | |
| from pip.log import logger | |
| from pip._vendor import requests, six | |
| from pip._vendor.requests.adapters import BaseAdapter | |
| @@ -27,6 +27,9 @@ from pip._vendor.requests.exceptions import InvalidURL, ChunkedEncodingError | |
| from pip._vendor.requests.models import Response | |
| from pip._vendor.requests.structures import CaseInsensitiveDict | |
| +from cachecontrol import CacheControlAdapter | |
| +from cachecontrol.caches import FileCache | |
| + | |
| __all__ = ['get_file_content', | |
| 'is_url', 'url_to_path', 'path_to_url', | |
| 'is_archive_file', 'unpack_vcs_link', | |
| @@ -225,11 +228,17 @@ class PipSession(requests.Session): | |
| # Attach our Authentication handler to the session | |
| self.auth = MultiDomainBasicAuth() | |
| + # Configure our Cache | |
| + adapter_kw = {"cache": FileCache(HTTP_CACHE_DIR)} | |
| + | |
| # Configure retries | |
| if retries: | |
| - http_adapter = requests.adapters.HTTPAdapter(max_retries=retries) | |
| - self.mount("http://", http_adapter) | |
| - self.mount("https://", http_adapter) | |
| + adapter_kw.update({"max_retries": retries}) | |
| + | |
| + http_adapter = CacheControlAdapter(**adapter_kw) | |
| + | |
| + self.mount("http://", http_adapter) | |
| + self.mount("https://", http_adapter) | |
| # Enable file:// urls | |
| self.mount("file://", LocalFSAdapter()) | |
| @@ -419,6 +428,7 @@ def _download_url(resp, link, temp_location): | |
| "Unsupported hash name %s for package %s" % | |
| (link.hash_name, link) | |
| ) | |
| + | |
| try: | |
| total_length = int(resp.headers['content-length']) | |
| except (ValueError, KeyError, TypeError): | |
| @@ -529,32 +539,15 @@ def _copy_file(filename, location, content_type, link): | |
| logger.notify('Saved %s' % display_path(download_location)) | |
| -def unpack_http_url(link, location, download_cache, download_dir=None, | |
| - session=None): | |
| +def unpack_http_url(link, location, download_dir=None, session=None): | |
| if session is None: | |
| session = PipSession() | |
| temp_dir = tempfile.mkdtemp('-unpack', 'pip-') | |
| temp_location = None | |
| target_url = link.url.split('#', 1)[0] | |
| - already_cached = False | |
| - cache_file = None | |
| - cache_content_type_file = None | |
| - download_hash = None | |
| - # If a download cache is specified, is the file cached there? | |
| - if download_cache: | |
| - cache_file = os.path.join( | |
| - download_cache, | |
| - urllib.quote(target_url, '') | |
| - ) | |
| - cache_content_type_file = cache_file + '.content-type' | |
| - already_cached = ( | |
| - os.path.exists(cache_file) and | |
| - os.path.exists(cache_content_type_file) | |
| - ) | |
| - if not os.path.isdir(download_cache): | |
| - create_download_cache_folder(download_cache) | |
| + download_hash = None | |
| # If a download dir is specified, is the file already downloaded there? | |
| already_downloaded = None | |
| @@ -581,27 +574,6 @@ def unpack_http_url(link, location, download_cache, download_dir=None, | |
| os.unlink(already_downloaded) | |
| already_downloaded = None | |
| - # If not a valid download, let's confirm the cached file is valid | |
| - if already_cached and not temp_location: | |
| - with open(cache_content_type_file) as fp: | |
| - content_type = fp.read().strip() | |
| - temp_location = cache_file | |
| - logger.notify('Using download cache from %s' % cache_file) | |
| - if link.hash and link.hash_name: | |
| - download_hash = _get_hash_from_file(cache_file, link) | |
| - try: | |
| - _check_hash(download_hash, link) | |
| - except HashMismatch: | |
| - logger.warn( | |
| - 'Cached file %s has bad hash, ' | |
| - 're-downloading.' % temp_location | |
| - ) | |
| - temp_location = None | |
| - os.unlink(cache_file) | |
| - os.unlink(cache_content_type_file) | |
| - already_cached = False | |
| - | |
| - # We don't have either a cached or a downloaded copy | |
| # let's download to a tmp dir | |
| if not temp_location: | |
| try: | |
| @@ -666,11 +638,7 @@ def unpack_http_url(link, location, download_cache, download_dir=None, | |
| # archives, they have to be unpacked to parse dependencies | |
| unpack_file(temp_location, location, content_type, link) | |
| - # if using a download cache, cache it, if needed | |
| - if cache_file and not already_cached: | |
| - cache_download(cache_file, temp_location, content_type) | |
| - | |
| - if not (already_cached or already_downloaded): | |
| + if not already_downloaded: | |
| os.unlink(temp_location) | |
| os.rmdir(temp_dir) | |
| diff --git a/pip/index.py b/pip/index.py | |
| index 278b26f..2a5c343 100644 | |
| --- a/pip/index.py | |
| +++ b/pip/index.py | |
| @@ -41,7 +41,7 @@ class PackageFinder(object): | |
| session=None): | |
| self.find_links = find_links | |
| self.index_urls = index_urls | |
| - self.cache = PageCache() | |
| + | |
| # These are boring links that have already been logged somehow: | |
| self.logged_links = set() | |
| @@ -181,8 +181,7 @@ class PackageFinder(object): | |
| mkurl_pypi_url(self.index_urls[0]), | |
| trusted=True, | |
| ) | |
| - # This will also cache the page, so it's okay that we get it again | |
| - # later: | |
| + | |
| page = self._get_page(main_index_url, req) | |
| if page is None: | |
| url_name = self._find_url_name( | |
| @@ -655,41 +654,7 @@ class PackageFinder(object): | |
| return None | |
| def _get_page(self, link, req): | |
| - return HTMLPage.get_page( | |
| - link, req, | |
| - cache=self.cache, | |
| - session=self.session, | |
| - ) | |
| - | |
| - | |
| -class PageCache(object): | |
| - """Cache of HTML pages""" | |
| - | |
| - failure_limit = 3 | |
| - | |
| - def __init__(self): | |
| - self._failures = {} | |
| - self._pages = {} | |
| - self._archives = {} | |
| - | |
| - def too_many_failures(self, url): | |
| - return self._failures.get(url, 0) >= self.failure_limit | |
| - | |
| - def get_page(self, url): | |
| - return self._pages.get(url) | |
| - | |
| - def is_archive(self, url): | |
| - return self._archives.get(url, False) | |
| - | |
| - def set_is_archive(self, url, value=True): | |
| - self._archives[url] = value | |
| - | |
| - def add_page_failure(self, url, level): | |
| - self._failures[url] = self._failures.get(url, 0) + level | |
| - | |
| - def add_page(self, urls, page): | |
| - for url in urls: | |
| - self._pages[url] = page | |
| + return HTMLPage.get_page(link, req, session=self.session) | |
| class HTMLPage(object): | |
| @@ -714,14 +679,12 @@ class HTMLPage(object): | |
| return self.url | |
| @classmethod | |
| - def get_page(cls, link, req, cache=None, skip_archives=True, session=None): | |
| + def get_page(cls, link, req, skip_archives=True, session=None): | |
| if session is None: | |
| session = PipSession() | |
| url = link.url | |
| url = url.split('#', 1)[0] | |
| - if cache.too_many_failures(url): | |
| - return None | |
| # Check for VCS schemes that do not support lookup as web pages. | |
| from pip.vcs import VcsSupport | |
| @@ -732,15 +695,8 @@ class HTMLPage(object): | |
| ) | |
| return None | |
| - if cache is not None: | |
| - inst = cache.get_page(url) | |
| - if inst is not None: | |
| - return inst | |
| try: | |
| if skip_archives: | |
| - if cache is not None: | |
| - if cache.is_archive(url): | |
| - return None | |
| filename = link.filename | |
| for bad_ext in ['.tar', '.tar.gz', '.tar.bz2', '.tgz', '.zip']: | |
| if filename.endswith(bad_ext): | |
| @@ -754,9 +710,8 @@ class HTMLPage(object): | |
| 'Skipping page %s because of Content-Type: ' | |
| '%s' % (link, content_type) | |
| ) | |
| - if cache is not None: | |
| - cache.set_is_archive(url) | |
| - return None | |
| + return | |
| + | |
| logger.debug('Getting page %s' % url) | |
| # Tack index.html onto file:// URLs that point to directories | |
| @@ -784,37 +739,31 @@ class HTMLPage(object): | |
| 'Skipping page %s because of Content-Type: %s' % | |
| (link, content_type) | |
| ) | |
| - if cache is not None: | |
| - cache.set_is_archive(url) | |
| - return None | |
| + return | |
| inst = cls(resp.text, resp.url, resp.headers, trusted=link.trusted) | |
| except requests.HTTPError as exc: | |
| level = 2 if exc.response.status_code == 404 else 1 | |
| - cls._handle_fail(req, link, exc, url, cache=cache, level=level) | |
| + cls._handle_fail(req, link, exc, url, level=level) | |
| except requests.ConnectionError as exc: | |
| cls._handle_fail( | |
| req, link, "connection error: %s" % exc, url, | |
| - cache=cache, | |
| ) | |
| except requests.Timeout: | |
| - cls._handle_fail(req, link, "timed out", url, cache=cache) | |
| + cls._handle_fail(req, link, "timed out", url) | |
| except SSLError as exc: | |
| reason = ("There was a problem confirming the ssl certificate: " | |
| "%s" % exc) | |
| cls._handle_fail( | |
| req, link, reason, url, | |
| - cache=cache, | |
| level=2, | |
| meth=logger.notify, | |
| ) | |
| else: | |
| - if cache is not None: | |
| - cache.add_page([url, resp.url], inst) | |
| return inst | |
| @staticmethod | |
| - def _handle_fail(req, link, reason, url, cache=None, level=1, meth=None): | |
| + def _handle_fail(req, link, reason, url, level=1, meth=None): | |
| if meth is None: | |
| meth = logger.info | |
| @@ -822,9 +771,6 @@ class HTMLPage(object): | |
| meth("Will skip URL %s when looking for download links for %s" % | |
| (link.url, req)) | |
| - if cache is not None: | |
| - cache.add_page_failure(url, level) | |
| - | |
| @staticmethod | |
| def _get_content_type(url, session=None): | |
| """Get the Content-Type of the given url, using a HEAD request""" | |
| diff --git a/pip/locations.py b/pip/locations.py | |
| index 7a848cf..17df4d7 100644 | |
| --- a/pip/locations.py | |
| +++ b/pip/locations.py | |
| @@ -17,6 +17,9 @@ import pip.exceptions | |
| install | |
| +HTTP_CACHE_DIR = os.path.expanduser("~/Library/Caches/pip/http") | |
| + | |
| + | |
| DELETE_MARKER_MESSAGE = '''\ | |
| This file is placed here by pip to indicate the source was put | |
| here by pip. | |
| diff --git a/pip/req/req_set.py b/pip/req/req_set.py | |
| index 0490538..092e1f7 100644 | |
| --- a/pip/req/req_set.py | |
| +++ b/pip/req/req_set.py | |
| @@ -49,17 +49,14 @@ class Requirements(object): | |
| class RequirementSet(object): | |
| - def __init__(self, build_dir, src_dir, download_dir, download_cache=None, | |
| - upgrade=False, ignore_installed=False, as_egg=False, | |
| - target_dir=None, ignore_dependencies=False, | |
| - force_reinstall=False, use_user_site=False, session=None, | |
| - pycompile=True, wheel_download_dir=None): | |
| + def __init__(self, build_dir, src_dir, download_dir, upgrade=False, | |
| + ignore_installed=False, as_egg=False, target_dir=None, | |
| + ignore_dependencies=False, force_reinstall=False, | |
| + use_user_site=False, session=None, pycompile=True, | |
| + wheel_download_dir=None): | |
| self.build_dir = build_dir | |
| self.src_dir = src_dir | |
| self.download_dir = download_dir | |
| - if download_cache: | |
| - download_cache = os.path.expanduser(download_cache) | |
| - self.download_cache = download_cache | |
| self.upgrade = upgrade | |
| self.ignore_installed = ignore_installed | |
| self.force_reinstall = force_reinstall | |
| @@ -558,7 +555,6 @@ class RequirementSet(object): | |
| unpack_http_url( | |
| link, | |
| location, | |
| - self.download_cache, | |
| download_dir, | |
| self.session, | |
| ) | |
| diff --git a/pip/util.py b/pip/util.py | |
| index 79f7924..f8b26e2 100644 | |
| --- a/pip/util.py | |
| +++ b/pip/util.py | |
| @@ -30,8 +30,7 @@ __all__ = ['rmtree', 'display_path', 'backup_dir', | |
| 'split_leading_dir', 'has_leading_dir', | |
| 'make_path_relative', 'normalize_path', | |
| 'renames', 'get_terminal_size', 'get_prog', | |
| - 'unzip_file', 'untar_file', 'create_download_cache_folder', | |
| - 'cache_download', 'unpack_file', 'call_subprocess'] | |
| + 'unzip_file', 'untar_file', 'unpack_file', 'call_subprocess'] | |
| def get_prog(): | |
| @@ -615,23 +614,6 @@ def untar_file(filename, location): | |
| tar.close() | |
| -def create_download_cache_folder(folder): | |
| - logger.indent -= 2 | |
| - logger.notify('Creating supposed download cache at %s' % folder) | |
| - logger.indent += 2 | |
| - os.makedirs(folder) | |
| - | |
| - | |
| -def cache_download(target_file, temp_location, content_type): | |
| - logger.notify( | |
| - 'Storing download in cache at %s' % display_path(target_file) | |
| - ) | |
| - shutil.copyfile(temp_location, target_file) | |
| - fp = open(target_file + '.content-type', 'w') | |
| - fp.write(content_type) | |
| - fp.close() | |
| - | |
| - | |
| def unpack_file(filename, location, content_type, link): | |
| filename = os.path.realpath(filename) | |
| if (content_type == 'application/zip' | |
| diff --git a/tests/unit/test_download.py b/tests/unit/test_download.py | |
| index 353a51e..1c977c2 100644 | |
| --- a/tests/unit/test_download.py | |
| +++ b/tests/unit/test_download.py | |
| @@ -35,7 +35,6 @@ def test_unpack_http_url_with_urllib_response_without_content_type(data): | |
| unpack_http_url( | |
| link, | |
| temp_dir, | |
| - download_cache=None, | |
| download_dir=None, | |
| session=session, | |
| ) | |
| @@ -77,50 +76,6 @@ class MockResponse(object): | |
| @patch('pip.download.unpack_file') | |
| -def test_unpack_http_url_bad_cache_checksum(mock_unpack_file): | |
| - """ | |
| - If cached download has bad checksum, re-download. | |
| - """ | |
| - base_url = 'http://www.example.com/somepackage.tgz' | |
| - contents = b('downloaded') | |
| - download_hash = hashlib.new('sha1', contents) | |
| - link = Link(base_url + '#sha1=' + download_hash.hexdigest()) | |
| - | |
| - session = Mock() | |
| - session.get = Mock() | |
| - response = session.get.return_value = MockResponse(contents) | |
| - response.headers = {'content-type': 'application/x-tar'} | |
| - response.url = base_url | |
| - | |
| - cache_dir = mkdtemp() | |
| - try: | |
| - cache_file = os.path.join(cache_dir, urllib.quote(base_url, '')) | |
| - cache_ct_file = cache_file + '.content-type' | |
| - _write_file(cache_file, 'some contents') | |
| - _write_file(cache_ct_file, 'application/x-tar') | |
| - | |
| - unpack_http_url( | |
| - link, | |
| - 'location', | |
| - download_cache=cache_dir, | |
| - session=session, | |
| - ) | |
| - | |
| - # despite existence of cached file with bad hash, downloaded again | |
| - session.get.assert_called_once_with( | |
| - "http://www.example.com/somepackage.tgz", | |
| - headers={"Accept-Encoding": "identity"}, | |
| - stream=True, | |
| - ) | |
| - # cached file is replaced with newly downloaded file | |
| - with open(cache_file) as fh: | |
| - assert fh.read() == 'downloaded' | |
| - | |
| - finally: | |
| - rmtree(cache_dir) | |
| - | |
| - | |
| -@patch('pip.download.unpack_file') | |
| def test_unpack_http_url_bad_downloaded_checksum(mock_unpack_file): | |
| """ | |
| If already-downloaded file has bad checksum, re-download. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment