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
19
from pip ._vendor .lockfile import LockError
19
20
from pip ._vendor .requests .adapters import BaseAdapter , HTTPAdapter
33
34
# Import ssl from compat so the initial import occurs in only one place.
34
35
from pip ._internal .utils .compat import HAS_TLS , ipaddress , ssl
35
36
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
+ )
37
43
from pip ._internal .utils .glibc import libc_ver
38
44
from pip ._internal .utils .misc import (
39
45
ask ,
44
50
build_url_from_netloc ,
45
51
consume ,
46
52
display_path ,
53
+ ensure_dir ,
47
54
format_size ,
48
55
get_installed_version ,
49
56
hide_url ,
@@ -536,7 +543,7 @@ def suppressed_cache_errors():
536
543
pass
537
544
538
545
539
- class SafeFileCache (FileCache ):
546
+ class SafeFileCache (BaseCache ):
540
547
"""
541
548
A file based cache which is safe to use even when the target directory may
542
549
not be accessible or writable.
@@ -545,7 +552,8 @@ class SafeFileCache(FileCache):
545
552
def __init__ (self , directory , use_dir_lock = False ):
546
553
# type: (str, bool) -> None
547
554
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
549
557
550
558
def _get_cache_path (self , name ):
551
559
# type: (str) -> str
@@ -558,18 +566,27 @@ def _get_cache_path(self, name):
558
566
559
567
def get (self , key ):
560
568
# type: (str) -> Optional[bytes]
569
+ path = self ._get_cache_path (key )
561
570
with suppressed_cache_errors ():
562
- return super (SafeFileCache , self ).get (key )
571
+ with open (path , 'rb' ) as f :
572
+ return f .read ()
563
573
564
574
def set (self , key , value ):
565
575
# type: (str, bytes) -> None
576
+ path = self ._get_cache_path (key )
566
577
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 )
568
584
569
585
def delete (self , key ):
570
586
# type: (str) -> None
587
+ path = self ._get_cache_path (key )
571
588
with suppressed_cache_errors ():
572
- return super ( SafeFileCache , self ). delete ( key )
589
+ os . remove ( path )
573
590
574
591
575
592
class InsecureHTTPAdapter (HTTPAdapter ):
0 commit comments