|
14 | 14 |
|
15 | 15 | from pip._vendor import requests, six, urllib3
|
16 | 16 | from pip._vendor.cachecontrol import CacheControlAdapter
|
| 17 | +from pip._vendor.cachecontrol.cache import BaseCache |
17 | 18 | from pip._vendor.cachecontrol.caches import FileCache
|
18 |
| -from pip._vendor.lockfile import LockError |
19 | 19 | from pip._vendor.requests.adapters import BaseAdapter, HTTPAdapter
|
20 | 20 | from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
|
21 | 21 | from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response
|
|
33 | 33 | # Import ssl from compat so the initial import occurs in only one place.
|
34 | 34 | from pip._internal.utils.compat import HAS_TLS, ipaddress, ssl
|
35 | 35 | from pip._internal.utils.encoding import auto_decode
|
36 |
| -from pip._internal.utils.filesystem import check_path_owner, copy2_fixed |
| 36 | +from pip._internal.utils.filesystem import ( |
| 37 | + adjacent_tmp_file, |
| 38 | + check_path_owner, |
| 39 | + copy2_fixed, |
| 40 | + replace, |
| 41 | +) |
37 | 42 | from pip._internal.utils.glibc import libc_ver
|
38 | 43 | from pip._internal.utils.misc import (
|
39 | 44 | ask,
|
|
44 | 49 | build_url_from_netloc,
|
45 | 50 | consume,
|
46 | 51 | display_path,
|
| 52 | + ensure_dir, |
47 | 53 | format_size,
|
48 | 54 | get_installed_version,
|
49 | 55 | hide_url,
|
@@ -532,31 +538,54 @@ def suppressed_cache_errors():
|
532 | 538 | """
|
533 | 539 | try:
|
534 | 540 | yield
|
535 |
| - except (LockError, OSError, IOError): |
| 541 | + except (OSError, IOError): |
536 | 542 | pass
|
537 | 543 |
|
538 | 544 |
|
539 |
| -class SafeFileCache(FileCache): |
| 545 | +class SafeFileCache(BaseCache): |
540 | 546 | """
|
541 | 547 | A file based cache which is safe to use even when the target directory may
|
542 | 548 | not be accessible or writable.
|
543 | 549 | """
|
544 | 550 |
|
545 |
| - def __init__(self, directory, *args, **kwargs): |
| 551 | + def __init__(self, directory): |
| 552 | + # type: (str) -> None |
546 | 553 | assert directory is not None, "Cache directory must not be None."
|
547 |
| - super(SafeFileCache, self).__init__(directory, *args, **kwargs) |
548 |
| - |
549 |
| - def get(self, *args, **kwargs): |
| 554 | + super(SafeFileCache, self).__init__() |
| 555 | + self.directory = directory |
| 556 | + |
| 557 | + def _get_cache_path(self, name): |
| 558 | + # type: (str) -> str |
| 559 | + # From cachecontrol.caches.file_cache.FileCache._fn, brought into our |
| 560 | + # class for backwards-compatibility and to avoid using a non-public |
| 561 | + # method. |
| 562 | + hashed = FileCache.encode(name) |
| 563 | + parts = list(hashed[:5]) + [hashed] |
| 564 | + return os.path.join(self.directory, *parts) |
| 565 | + |
| 566 | + def get(self, key): |
| 567 | + # type: (str) -> Optional[bytes] |
| 568 | + path = self._get_cache_path(key) |
550 | 569 | with suppressed_cache_errors():
|
551 |
| - return super(SafeFileCache, self).get(*args, **kwargs) |
| 570 | + with open(path, 'rb') as f: |
| 571 | + return f.read() |
552 | 572 |
|
553 |
| - def set(self, *args, **kwargs): |
| 573 | + def set(self, key, value): |
| 574 | + # type: (str, bytes) -> None |
| 575 | + path = self._get_cache_path(key) |
554 | 576 | with suppressed_cache_errors():
|
555 |
| - return super(SafeFileCache, self).set(*args, **kwargs) |
| 577 | + ensure_dir(os.path.dirname(path)) |
| 578 | + |
| 579 | + with adjacent_tmp_file(path) as f: |
| 580 | + f.write(value) |
| 581 | + |
| 582 | + replace(f.name, path) |
556 | 583 |
|
557 |
| - def delete(self, *args, **kwargs): |
| 584 | + def delete(self, key): |
| 585 | + # type: (str) -> None |
| 586 | + path = self._get_cache_path(key) |
558 | 587 | with suppressed_cache_errors():
|
559 |
| - return super(SafeFileCache, self).delete(*args, **kwargs) |
| 588 | + os.remove(path) |
560 | 589 |
|
561 | 590 |
|
562 | 591 | class InsecureHTTPAdapter(HTTPAdapter):
|
@@ -631,7 +660,7 @@ def __init__(self, *args, **kwargs):
|
631 | 660 | # require manual eviction from the cache to fix it.
|
632 | 661 | if cache:
|
633 | 662 | secure_adapter = CacheControlAdapter(
|
634 |
| - cache=SafeFileCache(cache, use_dir_lock=True), |
| 663 | + cache=SafeFileCache(cache), |
635 | 664 | max_retries=retries,
|
636 | 665 | )
|
637 | 666 | else:
|
|
0 commit comments