Repository navigation
Expand file tree
/
Copy path_cache.py
More file actions
274 lines (222 loc) · 8.74 KB
/
Copy path_cache.py
File metadata and controls
274 lines (222 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""Cache Protocol and built-in implementations for Python interpreter discovery."""
from __future__ import annotations
__lazy_modules__ = [
"hashlib",
"json",
"pathlib",
"tempfile",
]
import json
import logging
import os
import stat
from contextlib import contextmanager, suppress
from hashlib import sha256
from os import fsencode
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, Final, Protocol, runtime_checkable
if TYPE_CHECKING:
from collections.abc import Generator
_LOGGER: Final[logging.Logger] = logging.getLogger(__name__)
@runtime_checkable
class ContentStore(Protocol):
"""A store for reading and writing cached content."""
def exists(self) -> bool:
"""
Return whether the cached content exists.
:returns: ``True`` if the content is cached, ``False`` otherwise.
"""
...
def read(self) -> dict | None:
"""
Read the cached content, or ``None`` if unavailable or corrupt.
:returns: the cached content, or ``None``.
"""
...
def write(self, content: dict) -> None:
"""
Persist *content* to the store.
:param content: interpreter metadata to cache.
"""
...
def remove(self) -> None:
"""Delete the cached content."""
...
@contextmanager
def locked(self) -> Generator[None]:
"""
Context manager that acquires an exclusive lock on this store.
:returns: a context manager; the lock is held inside its ``with`` block.
"""
...
@runtime_checkable
class PyInfoCache(Protocol):
"""Cache interface for Python interpreter information."""
def py_info(self, path: Path) -> ContentStore:
"""
Return the content store for the interpreter at *path*.
:param path: absolute path to a Python executable.
:returns: the content store for *path*.
"""
...
def py_info_clear(self) -> None:
"""Remove all cached interpreter information."""
...
class DiskCache:
"""
File-system based Python interpreter info cache (``<root>/py_info/4/<sha256>.json``).
:param root: root directory for the on-disk cache.
"""
def __init__(self, root: Path) -> None:
self._root = Path(os.path.abspath(root)) # ruff:ignore[os-path-abspath] # resolve() follows untrusted links
def py_info(self, path: Path) -> ContentStore:
"""
Return the content store for the interpreter at *path*.
:param path: absolute path to a Python executable.
:returns: the content store for *path*.
"""
if not self._trusted_folder():
return NoOpContentStore()
return DiskContentStore(self._py_info_dir, sha256(fsencode(path)).hexdigest())
def _trusted_folder(self) -> bool:
if not hasattr(os, "geteuid"):
return True
user_id = os.geteuid()
folder = self._py_info_dir
private = {self._root, folder.parent, folder}
for candidate in (*reversed(folder.parents), folder):
try:
try:
status = candidate.lstat()
except FileNotFoundError:
candidate.mkdir(exist_ok=True)
status = candidate.lstat()
except OSError:
_LOGGER.warning("not caching in %s", candidate, exc_info=True)
return False
if (
not stat.S_ISDIR(status.st_mode)
or status.st_uid not in {user_id, 0}
or (status.st_mode & 0o022 and (candidate in private or not status.st_mode & stat.S_ISVTX))
):
_LOGGER.warning("not caching in untrusted directory %s", candidate)
return False
return True
@property
def _py_info_dir(self) -> Path:
return self._root / "py_info" / "4"
def py_info_clear(self) -> None:
"""Remove all cached interpreter information."""
folder = self._py_info_dir
if folder.exists() and self._trusted_folder():
for entry in folder.iterdir():
if entry.suffix == ".json":
with suppress(OSError):
entry.unlink()
class NoOpContentStore(ContentStore):
# Content store that does nothing -- implements ContentStore protocol.
def exists(self) -> bool: # ruff:ignore[no-self-use] cache protocol
return False
def read(self) -> dict | None: # ruff:ignore[no-self-use] cache protocol
return None
def write(self, content: dict) -> None:
pass
def remove(self) -> None:
pass
@contextmanager
def locked(self) -> Generator[None]: # ruff:ignore[no-self-use] cache protocol
yield
class DiskContentStore:
# JSON file-based content store with file locking.
def __init__(self, folder: Path, key: str) -> None:
self._folder = folder
self._key = key
def exists(self) -> bool:
return self._file.exists()
@property
def _file(self) -> Path:
return self._folder / f"{self._key}.json"
def read(self) -> dict | None:
data, bad_format = None, False
try:
data, bad_format = self._read_entry()
except (ValueError, RecursionError):
bad_format = True
except OSError:
_LOGGER.debug("failed to read %s", self._file, exc_info=True)
bad_format = self._file.is_symlink()
else:
if not bad_format:
_LOGGER.debug("got python info from %s", self._file)
return data
if bad_format:
with suppress(OSError):
self.remove()
return None
def _read_entry(self) -> tuple[dict | None, bool]:
with open(self._file, encoding="utf-8", opener=self._open_entry) as file:
status = os.fstat(file.fileno())
if not stat.S_ISREG(status.st_mode) or (
hasattr(os, "geteuid") and (status.st_uid not in {os.geteuid(), 0} or status.st_mode & 0o022)
):
_LOGGER.warning("ignoring untrusted cache entry at %s", self._file)
return None, True
# A 1 MiB query can expand to 12 MiB when supplementary Unicode is escaped.
if status.st_size > 16 << 20:
_LOGGER.warning("ignoring oversized cache entry at %s", self._file)
return None, True
return json.load(file), False
@staticmethod
def _open_entry(path: str, flags: int) -> int:
return os.open(path, flags | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0))
def remove(self) -> None:
with suppress(OSError):
self._file.unlink()
_LOGGER.debug("removed python info at %s", self._file)
def write(self, content: dict) -> None:
self._folder.mkdir(parents=True, exist_ok=True)
temporary: Path | None = None
try:
with NamedTemporaryFile(
mode="w", encoding="utf-8", dir=self._folder, prefix=f".{self._key}.", delete=False
) as file:
temporary = Path(file.name)
# Compact encoding avoids indentation growth and the streaming encoder's recursion limit.
file.write(json.dumps(content, sort_keys=True))
temporary.replace(self._file)
except OSError:
_LOGGER.debug("failed to write %s", self._file, exc_info=True)
else:
_LOGGER.debug("wrote python info at %s", self._file)
finally:
if temporary is not None:
with suppress(OSError):
temporary.unlink(missing_ok=True)
@contextmanager
def locked(self) -> Generator[None]:
from filelock import FileLock # ruff:ignore[import-outside-top-level] filelock is needed only when locking
self._folder.mkdir(parents=True, exist_ok=True)
if os.name == "posix":
group_shared = bool(self._folder.stat().st_mode & stat.S_IWGRP)
lock_folder = self._folder / "locks-v2"
lock_folder.mkdir(mode=0o770 if group_shared else 0o700, exist_ok=True)
lock = FileLock(str(lock_folder / f"{self._key}.lock"), mode=0o660 if group_shared else 0o600)
else:
lock = FileLock(str(self._folder / f"{self._key}.lock"))
with lock:
yield
class NoOpCache(PyInfoCache):
# Cache that does nothing -- implements PyInfoCache protocol.
def py_info(self, path: Path) -> NoOpContentStore: # ruff:ignore[unused-method-argument, no-self-use] protocol
return NoOpContentStore()
def py_info_clear(self) -> None:
pass
__all__ = [
"ContentStore",
"DiskCache",
"DiskContentStore",
"NoOpCache",
"NoOpContentStore",
"PyInfoCache",
]