Skip to content

Commit 48be7eb

Browse files
committed
Implement cache methods using plain filesystem functions.
1 parent 80f092d commit 48be7eb

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/pip/_internal/download.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from pip._vendor import requests, six, urllib3
1616
from pip._vendor.cachecontrol import CacheControlAdapter
17+
from pip._vendor.cachecontrol.cache import BaseCache
1718
from pip._vendor.cachecontrol.caches import FileCache
1819
from pip._vendor.lockfile import LockError
1920
from pip._vendor.requests.adapters import BaseAdapter, HTTPAdapter
@@ -33,7 +34,12 @@
3334
# Import ssl from compat so the initial import occurs in only one place.
3435
from pip._internal.utils.compat import HAS_TLS, ipaddress, ssl
3536
from pip._internal.utils.encoding import auto_decode
36-
from pip._internal.utils.filesystem import check_path_owner, copy2_fixed
37+
from pip._internal.utils.filesystem import (
38+
adjacent_tmp_file,
39+
check_path_owner,
40+
copy2_fixed,
41+
replace,
42+
)
3743
from pip._internal.utils.glibc import libc_ver
3844
from pip._internal.utils.misc import (
3945
ask,
@@ -44,6 +50,7 @@
4450
build_url_from_netloc,
4551
consume,
4652
display_path,
53+
ensure_dir,
4754
format_size,
4855
get_installed_version,
4956
hide_url,
@@ -536,7 +543,7 @@ def suppressed_cache_errors():
536543
pass
537544

538545

539-
class SafeFileCache(FileCache):
546+
class SafeFileCache(BaseCache):
540547
"""
541548
A file based cache which is safe to use even when the target directory may
542549
not be accessible or writable.
@@ -545,7 +552,8 @@ class SafeFileCache(FileCache):
545552
def __init__(self, directory, use_dir_lock=False):
546553
# type: (str, bool) -> None
547554
assert directory is not None, "Cache directory must not be None."
548-
super(SafeFileCache, self).__init__(directory, use_dir_lock)
555+
super(SafeFileCache, self).__init__()
556+
self.directory = directory
549557

550558
def _get_cache_path(self, name):
551559
# type: (str) -> str
@@ -558,18 +566,27 @@ def _get_cache_path(self, name):
558566

559567
def get(self, key):
560568
# type: (str) -> Optional[bytes]
569+
path = self._get_cache_path(key)
561570
with suppressed_cache_errors():
562-
return super(SafeFileCache, self).get(key)
571+
with open(path, 'rb') as f:
572+
return f.read()
563573

564574
def set(self, key, value):
565575
# type: (str, bytes) -> None
576+
path = self._get_cache_path(key)
566577
with suppressed_cache_errors():
567-
return super(SafeFileCache, self).set(key, value)
578+
ensure_dir(os.path.dirname(path))
579+
580+
with adjacent_tmp_file(path) as f:
581+
f.write(value)
582+
583+
replace(f.name, path)
568584

569585
def delete(self, key):
570586
# type: (str) -> None
587+
path = self._get_cache_path(key)
571588
with suppressed_cache_errors():
572-
return super(SafeFileCache, self).delete(key)
589+
os.remove(path)
573590

574591

575592
class InsecureHTTPAdapter(HTTPAdapter):

tests/unit/test_download.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,10 @@ def test_safe_delete_no_perms(self, cache_tmpdir):
553553
def test_cache_hashes_are_same(self, cache_tmpdir):
554554
cache = SafeFileCache(cache_tmpdir)
555555
key = "test key"
556-
assert cache._get_cache_path(key) == FileCache._fn(cache, key)
556+
fake_cache = Mock(
557+
FileCache, directory=cache.directory, encode=FileCache.encode
558+
)
559+
assert cache._get_cache_path(key) == FileCache._fn(fake_cache, key)
557560

558561

559562
class TestPipSession:

0 commit comments

Comments
 (0)