]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_internal/network/cache.py
1 """HTTP cache implementation.
5 from contextlib
import contextmanager
6 from typing
import Generator
, Optional
8 from pip
._vendor
.cachecontrol
.cache
import BaseCache
9 from pip
._vendor
.cachecontrol
.caches
import FileCache
10 from pip
._vendor
.requests
.models
import Response
12 from pip
._internal
.utils
.filesystem
import adjacent_tmp_file
, replace
13 from pip
._internal
.utils
.misc
import ensure_dir
16 def is_from_cache(response
: Response
) -> bool:
17 return getattr(response
, "from_cache", False)
21 def suppressed_cache_errors() -> Generator
[None, None, None]:
22 """If we can't access the cache then we can just skip caching and process
23 requests as if caching wasn't enabled.
31 class SafeFileCache(BaseCache
):
33 A file based cache which is safe to use even when the target directory may
34 not be accessible or writable.
37 def __init__(self
, directory
: str) -> None:
38 assert directory
is not None, "Cache directory must not be None."
40 self
.directory
= directory
42 def _get_cache_path(self
, name
: str) -> str:
43 # From cachecontrol.caches.file_cache.FileCache._fn, brought into our
44 # class for backwards-compatibility and to avoid using a non-public
46 hashed
= FileCache
.encode(name
)
47 parts
= list(hashed
[:5]) + [hashed
]
48 return os
.path
.join(self
.directory
, *parts
)
50 def get(self
, key
: str) -> Optional
[bytes]:
51 path
= self
._get
_cache
_path
(key
)
52 with suppressed_cache_errors():
53 with open(path
, "rb") as f
:
56 def set(self
, key
: str, value
: bytes, expires
: Optional
[int] = None) -> None:
57 path
= self
._get
_cache
_path
(key
)
58 with suppressed_cache_errors():
59 ensure_dir(os
.path
.dirname(path
))
61 with adjacent_tmp_file(path
) as f
:
66 def delete(self
, key
: str) -> None:
67 path
= self
._get
_cache
_path
(key
)
68 with suppressed_cache_errors():