debkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- debkit/__init__.py +7 -0
- debkit/__main__.py +8 -0
- debkit/_env.py +67 -0
- debkit/_escape.py +25 -0
- debkit/_logging.py +98 -0
- debkit/_options.py +177 -0
- debkit/action/__init__.py +325 -0
- debkit/action/action_history.py +191 -0
- debkit/action/action_pkg.py +468 -0
- debkit/action/action_sync.py +495 -0
- debkit/argparser.py +246 -0
- debkit/argtypes.py +140 -0
- debkit/cli.py +130 -0
- debkit/repo/__init__.py +610 -0
- debkit/repo/changes.py +172 -0
- debkit/repo/component.py +335 -0
- debkit/repo/compress.py +266 -0
- debkit/repo/debfile.py +207 -0
- debkit/repo/gnupg.py +215 -0
- debkit/repo/hashes.py +139 -0
- debkit/repo/index_selection.py +224 -0
- debkit/repo/package.py +177 -0
- debkit/repo/packages.py +525 -0
- debkit/repo/params.py +305 -0
- debkit/repo/release.py +881 -0
- debkit/repo/repo_diff.py +114 -0
- debkit/repo/repo_rw.py +444 -0
- debkit/repo/repo_sync.py +433 -0
- debkit/repo/shared_root.py +348 -0
- debkit/repo/sources.py +165 -0
- debkit/repo/stanza.py +47 -0
- debkit-0.1.0.dist-info/METADATA +138 -0
- debkit-0.1.0.dist-info/RECORD +36 -0
- debkit-0.1.0.dist-info/WHEEL +4 -0
- debkit-0.1.0.dist-info/entry_points.txt +2 -0
- debkit-0.1.0.dist-info/licenses/LICENSE +21 -0
debkit/repo/packages.py
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
"""Class containing packages (Package objects) from 'Packages' indices file."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import re
|
|
7
|
+
from collections.abc import Callable, Iterable, Mapping
|
|
8
|
+
from typing import (
|
|
9
|
+
TYPE_CHECKING,
|
|
10
|
+
Any,
|
|
11
|
+
ClassVar,
|
|
12
|
+
NamedTuple,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from txstore import calc_digests, compare_digests, is_unsafe_path
|
|
16
|
+
|
|
17
|
+
from debkit.repo.compress import CODECS, CorruptStreamError, SizeLimitError, codec_for, read_capped
|
|
18
|
+
from debkit.repo.hashes import ChecksumException
|
|
19
|
+
from debkit.repo.package import Package
|
|
20
|
+
from debkit.repo.params import is_safe_name
|
|
21
|
+
from debkit.repo.release import Release, RootRelease
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from txstore import Storage
|
|
25
|
+
|
|
26
|
+
from debkit.repo.params import RepoParams
|
|
27
|
+
|
|
28
|
+
logger = logging.getLogger(__name__)
|
|
29
|
+
logger.addHandler(logging.NullHandler())
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# Soft cap on decompressed Packages-indices size (bytes); decoding stops as soon as it is
|
|
33
|
+
# passed, so a zip-bomb-style payload is never inflated in full
|
|
34
|
+
# -> [VALIDATION.md](VALIDATION.md#index-decoding)
|
|
35
|
+
MAX_INDICES_BYTES = 256 * 1024 * 1024
|
|
36
|
+
|
|
37
|
+
# Debian Policy 5.1: a stanza ends at a blank line, which may hold spaces or tabs
|
|
38
|
+
# -> [VALIDATION.md](VALIDATION.md#index-decoding)
|
|
39
|
+
_STANZA_SEPARATOR = re.compile(r"\n[ \t]*\n\s*")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class PackagesDiff(NamedTuple):
|
|
43
|
+
"""How a newer set of packages differs from an older one, each keyed by `Filename`.
|
|
44
|
+
|
|
45
|
+
Attributes:
|
|
46
|
+
added: Packages only the newer set holds.
|
|
47
|
+
removed: Packages only the older set holds.
|
|
48
|
+
updated: Packages both hold that the newer set changed, as the newer set has them.
|
|
49
|
+
old_versions: The older set's versions of the updated packages.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
added: dict[str, Package]
|
|
53
|
+
removed: dict[str, Package]
|
|
54
|
+
updated: dict[str, Package]
|
|
55
|
+
old_versions: dict[str, Package]
|
|
56
|
+
|
|
57
|
+
def is_empty(self) -> bool:
|
|
58
|
+
"""Report whether the sets are equal: nothing added, removed, or updated."""
|
|
59
|
+
return not (self.added or self.removed or self.updated)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class PackagesException(Exception):
|
|
63
|
+
"""Exception raised for errors in the Packages class."""
|
|
64
|
+
|
|
65
|
+
def __init__(self, message: str) -> None:
|
|
66
|
+
"""Initialize with error message."""
|
|
67
|
+
super().__init__(f"Packages: {message}")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class PackagesNotFoundException(PackagesException):
|
|
71
|
+
"""No index file the Release lists for the arch is in storage; nothing was read."""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def diff_packages(newer: Mapping[str, Package], older: Mapping[str, Package]) -> PackagesDiff:
|
|
75
|
+
"""Compare a newer package map with an older one, both keyed by `Filename`.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
newer: The newer package map.
|
|
79
|
+
older: The older package map.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
Packages by `Filename`: added, removed, and updated in `newer`, and the old versions
|
|
83
|
+
the updated ones replace.
|
|
84
|
+
"""
|
|
85
|
+
updated = {
|
|
86
|
+
filename: package
|
|
87
|
+
for filename, package in newer.items()
|
|
88
|
+
if filename in older and package != older[filename]
|
|
89
|
+
}
|
|
90
|
+
return PackagesDiff(
|
|
91
|
+
added={filename: pkg for filename, pkg in newer.items() if filename not in older},
|
|
92
|
+
removed={filename: pkg for filename, pkg in older.items() if filename not in newer},
|
|
93
|
+
updated=updated,
|
|
94
|
+
old_versions={filename: pkg for filename, pkg in older.items() if filename in updated},
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class Packages:
|
|
99
|
+
"""Container to store packages (Package objects).
|
|
100
|
+
|
|
101
|
+
Attributes:
|
|
102
|
+
storage: The storage object.
|
|
103
|
+
params: The parameters for the Packages object.
|
|
104
|
+
release: The release object.
|
|
105
|
+
packages: The packages objects from Packages indices file.
|
|
106
|
+
used_indices_path: The Packages file used for self.packages.
|
|
107
|
+
found_indices_paths: Every index form the Release lists that this container reads or
|
|
108
|
+
writes: found by `load`, planned by `setup`.
|
|
109
|
+
size: The number of packages.
|
|
110
|
+
modified: Whether the Packages object has been modified.
|
|
111
|
+
WRITABLE: Whether a save may regenerate this kind of index.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
WRITABLE: ClassVar[bool] = True
|
|
115
|
+
|
|
116
|
+
def __init__(self, storage: Storage, params: RepoParams) -> None:
|
|
117
|
+
"""Initialize a Packages object.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
storage: The storage object of repo.
|
|
121
|
+
params: The parameters of related repo.
|
|
122
|
+
"""
|
|
123
|
+
self.storage = storage
|
|
124
|
+
self.params = params
|
|
125
|
+
self.release = Release(storage, params)
|
|
126
|
+
self.packages: dict[str, Package] = {}
|
|
127
|
+
self.used_indices_path = ""
|
|
128
|
+
self.found_indices_paths: list[str] = []
|
|
129
|
+
self.modified = False
|
|
130
|
+
|
|
131
|
+
def _index_inner(self, arch: str, suffix: str = "") -> str:
|
|
132
|
+
"""Compose the Release-relative inner path of a package index.
|
|
133
|
+
|
|
134
|
+
One composition for the write and the read alike, so a written index lands
|
|
135
|
+
where the loader looks -> [VALIDATION.md](VALIDATION.md#repository-index-paths).
|
|
136
|
+
A flat repo hosts the index at the component or dist root; a componented one
|
|
137
|
+
nests it under the architecture directory.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
arch: The architecture name, or the source architecture.
|
|
141
|
+
suffix: The compression suffix; empty for the uncompressed index.
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
The path under `<comp>/`, or the bare filename for a flat repo.
|
|
145
|
+
"""
|
|
146
|
+
name = f"{self.params.index_name(arch)}{suffix}"
|
|
147
|
+
return name if self.params.is_flat else f"{self.params.arch_dir(arch)}/{name}"
|
|
148
|
+
|
|
149
|
+
def setup(self, comp: str, arch: str) -> None:
|
|
150
|
+
"""Initialize the new empty Packages object.
|
|
151
|
+
|
|
152
|
+
A flat repo carries no per-component Release, as its loader reads none.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
comp: The component name.
|
|
156
|
+
arch: The architecture name.
|
|
157
|
+
"""
|
|
158
|
+
self.used_indices_path = self.params.indices_path(self._index_inner(arch, ".gz"), comp)
|
|
159
|
+
plain_path = self.params.indices_path(self._index_inner(arch), comp)
|
|
160
|
+
self.found_indices_paths = [plain_path, self.used_indices_path]
|
|
161
|
+
if not self.params.is_flat:
|
|
162
|
+
self.release.setup_component(comp, arch)
|
|
163
|
+
|
|
164
|
+
@staticmethod
|
|
165
|
+
def _stanzas(data: str) -> list[str]:
|
|
166
|
+
"""Split a decoded index into its stanzas, CRLF line ends read as LF.
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
data: The decoded index.
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
Every stanza holding more than whitespace, in index order.
|
|
173
|
+
"""
|
|
174
|
+
blocks = _STANZA_SEPARATOR.split(data.replace("\r\n", "\n"))
|
|
175
|
+
return [block for block in blocks if block.strip()]
|
|
176
|
+
|
|
177
|
+
def _parse_indices(self, data: str) -> int:
|
|
178
|
+
"""Read list of packages from data from Packages file.
|
|
179
|
+
|
|
180
|
+
Use decompress function for unpack, parse data from headers format.
|
|
181
|
+
|
|
182
|
+
Args:
|
|
183
|
+
data: The data from the Packages file.
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
The number of packages parsed.
|
|
187
|
+
"""
|
|
188
|
+
for block in self._stanzas(data):
|
|
189
|
+
package = Package()
|
|
190
|
+
pkg_name = package.parse(block)
|
|
191
|
+
|
|
192
|
+
if pkg_name is None:
|
|
193
|
+
continue
|
|
194
|
+
|
|
195
|
+
# a Filename is remote metadata, so a hostile index can name a key outside the
|
|
196
|
+
# storage root, or the root itself once an empty value is normalized to `.`; a
|
|
197
|
+
# rejected stanza is dropped rather than failing the load, so one bad record
|
|
198
|
+
# cannot stop a mirror -> [VALIDATION.md](VALIDATION.md#filename-safety)
|
|
199
|
+
if not is_safe_name(pkg_name):
|
|
200
|
+
logger.warning("%s named in an index is an unsafe package path. Skipped.", pkg_name)
|
|
201
|
+
continue
|
|
202
|
+
# a payload under `dists/` would overwrite the metadata of any dist sharing the
|
|
203
|
+
# root -> [VALIDATION.md](VALIDATION.md#filename-safety)
|
|
204
|
+
if self.params.is_index_tree_key(pkg_name):
|
|
205
|
+
logger.warning("%s named in an index lies in the dists/ tree. Skipped.", pkg_name)
|
|
206
|
+
continue
|
|
207
|
+
|
|
208
|
+
self.packages[pkg_name] = package
|
|
209
|
+
return len(self)
|
|
210
|
+
|
|
211
|
+
def _serialize_indices(self) -> str:
|
|
212
|
+
"""Serialize the content of the Packages file to headers format.
|
|
213
|
+
|
|
214
|
+
Returns:
|
|
215
|
+
The serialized packages indices.
|
|
216
|
+
"""
|
|
217
|
+
return "".join(package.serialize() for package in self.packages.values())
|
|
218
|
+
|
|
219
|
+
def _find_packages_file(
|
|
220
|
+
self, root_release: RootRelease, comp: str, arch: str
|
|
221
|
+
) -> tuple[list[str], str, Callable[[bytes, int], bytes]]:
|
|
222
|
+
"""Find the index files the Release lists and storage holds, and pick one to read.
|
|
223
|
+
|
|
224
|
+
Every listed form present is found, the uncompressed one included, so a save
|
|
225
|
+
rewrites each; the first in `CODECS` order is read. Nothing is assigned, so a
|
|
226
|
+
repeated `load` finds the same files.
|
|
227
|
+
|
|
228
|
+
Args:
|
|
229
|
+
root_release: The root release object.
|
|
230
|
+
comp: The component name.
|
|
231
|
+
arch: The architecture name.
|
|
232
|
+
|
|
233
|
+
Raises:
|
|
234
|
+
PackagesNotFoundException: There is no Packages* file.
|
|
235
|
+
|
|
236
|
+
Returns:
|
|
237
|
+
Every found index path, the path to read, and its decompression function.
|
|
238
|
+
"""
|
|
239
|
+
found: list[str] = []
|
|
240
|
+
chosen: tuple[str, Callable[[bytes, int], bytes]] | None = None
|
|
241
|
+
|
|
242
|
+
for codec in CODECS:
|
|
243
|
+
indices_path = self.params.indices_path(self._index_inner(arch, codec.suffix), comp)
|
|
244
|
+
|
|
245
|
+
if self.params.release_key(indices_path) not in root_release.digests:
|
|
246
|
+
logger.debug("%s not in Release...", indices_path)
|
|
247
|
+
continue
|
|
248
|
+
if not self.storage.exists(indices_path):
|
|
249
|
+
logger.debug("%s not exist...", indices_path)
|
|
250
|
+
continue
|
|
251
|
+
|
|
252
|
+
found.append(indices_path)
|
|
253
|
+
if chosen is None:
|
|
254
|
+
logger.debug("%s found... (use this)", indices_path)
|
|
255
|
+
chosen = indices_path, codec.decompress
|
|
256
|
+
else:
|
|
257
|
+
logger.debug("%s found... (skip)", indices_path)
|
|
258
|
+
|
|
259
|
+
if chosen is not None:
|
|
260
|
+
return found, *chosen
|
|
261
|
+
|
|
262
|
+
# there is no any Packages indices file found / 404 everywhere
|
|
263
|
+
if self.params.is_flat:
|
|
264
|
+
raise PackagesNotFoundException("there is no indices file.")
|
|
265
|
+
|
|
266
|
+
raise PackagesNotFoundException(
|
|
267
|
+
f"missing indices file for arch {arch} in component {comp}."
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
def _read_indices(self, root_release: RootRelease, path: str) -> bytes:
|
|
271
|
+
"""Read an index no larger than the Release records, before its digest is checked.
|
|
272
|
+
|
|
273
|
+
Args:
|
|
274
|
+
root_release: The root release object.
|
|
275
|
+
path: The path of the index file.
|
|
276
|
+
|
|
277
|
+
Raises:
|
|
278
|
+
ChecksumException: The file is larger than the size a checked Release records.
|
|
279
|
+
PackagesException: The file is stored past `MAX_INDICES_BYTES`, whatever size a
|
|
280
|
+
Release records.
|
|
281
|
+
|
|
282
|
+
Returns:
|
|
283
|
+
The stored index bytes -> [VALIDATION.md](VALIDATION.md#object-sizes).
|
|
284
|
+
"""
|
|
285
|
+
declared: int | None = None
|
|
286
|
+
if self.params.verify_digests:
|
|
287
|
+
key = self.params.release_key(path)
|
|
288
|
+
sizes = [
|
|
289
|
+
size
|
|
290
|
+
for release in (root_release, self.release)
|
|
291
|
+
if (size := release.declared_size(key)) is not None
|
|
292
|
+
]
|
|
293
|
+
declared = max(sizes) if sizes else None
|
|
294
|
+
# a recorded size tightens the cap, never lifts it: the Release is remote metadata
|
|
295
|
+
limit = MAX_INDICES_BYTES if declared is None else min(declared, MAX_INDICES_BYTES)
|
|
296
|
+
try:
|
|
297
|
+
return read_capped(self.storage, path, limit)
|
|
298
|
+
except SizeLimitError as exc:
|
|
299
|
+
if declared is None or declared > MAX_INDICES_BYTES:
|
|
300
|
+
raise PackagesException(f"Stored indices {exc}.") from exc
|
|
301
|
+
raise ChecksumException(f"Mismatch for {path}: {exc}.") from exc
|
|
302
|
+
|
|
303
|
+
def _verify_indices_checksum(
|
|
304
|
+
self, root_release: RootRelease, path: str, digests: dict[str, str]
|
|
305
|
+
) -> bool:
|
|
306
|
+
"""Verify checksum of Packages file.
|
|
307
|
+
|
|
308
|
+
Args:
|
|
309
|
+
root_release: The root release object.
|
|
310
|
+
path: The path of the Packages file.
|
|
311
|
+
digests: The checksums to verify.
|
|
312
|
+
|
|
313
|
+
Returns:
|
|
314
|
+
True if the checksum is valid, False otherwise.
|
|
315
|
+
"""
|
|
316
|
+
# map the full path back to the key the Release lists it under
|
|
317
|
+
path = self.params.release_key(path)
|
|
318
|
+
|
|
319
|
+
return root_release.verify_digests(path, digests) or self.release.verify_digests(
|
|
320
|
+
path, digests
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
def load(
|
|
324
|
+
self,
|
|
325
|
+
root_release: RootRelease,
|
|
326
|
+
comp: str,
|
|
327
|
+
arch: str,
|
|
328
|
+
) -> None:
|
|
329
|
+
"""Load 'Packages' indices file from repo; an empty one loads no package.
|
|
330
|
+
|
|
331
|
+
Args:
|
|
332
|
+
root_release: The root release object.
|
|
333
|
+
comp: The component name.
|
|
334
|
+
arch: The architecture name.
|
|
335
|
+
|
|
336
|
+
Raises:
|
|
337
|
+
ChecksumException: Checksum mismatch for Packages file, or a file larger than the
|
|
338
|
+
size a checked Release records.
|
|
339
|
+
PackagesNotFoundException: No Packages* file the Release lists is in storage.
|
|
340
|
+
PackagesException: The index is truncated, does not decode, is stored or decodes
|
|
341
|
+
past `MAX_INDICES_BYTES`, or is not UTF-8; a missing one raises the
|
|
342
|
+
`PackagesNotFoundException` subclass.
|
|
343
|
+
"""
|
|
344
|
+
found, indices_path, decompress_fun = self._find_packages_file(root_release, comp, arch)
|
|
345
|
+
# replaced, not appended: a repeated load, or one after `setup`, reads storage afresh
|
|
346
|
+
self.found_indices_paths = found
|
|
347
|
+
self.used_indices_path = indices_path
|
|
348
|
+
enc_data = self._read_indices(root_release, indices_path)
|
|
349
|
+
|
|
350
|
+
# Verify the digest BEFORE deciding the payload is "legitimately empty".
|
|
351
|
+
# `_find_packages_file` only guarantees the key exists in Release and in
|
|
352
|
+
# storage; a truncated / 0-byte object against a non-empty digest in
|
|
353
|
+
# Release must fail loudly instead of being silently treated as
|
|
354
|
+
# "no packages for this arch" (which would mask repo-vs-pool drift
|
|
355
|
+
# and bypass a strict load in the caller).
|
|
356
|
+
if self.params.verify_digests:
|
|
357
|
+
digests = calc_digests(enc_data)
|
|
358
|
+
if not self._verify_indices_checksum(root_release, indices_path, digests):
|
|
359
|
+
raise ChecksumException(f"Mismatch for {indices_path}.")
|
|
360
|
+
|
|
361
|
+
if not enc_data:
|
|
362
|
+
logger.info("No packages indices.")
|
|
363
|
+
return
|
|
364
|
+
|
|
365
|
+
try:
|
|
366
|
+
raw = decompress_fun(enc_data, MAX_INDICES_BYTES)
|
|
367
|
+
except SizeLimitError as exc:
|
|
368
|
+
raise PackagesException(
|
|
369
|
+
f"Decompressed indices {indices_path} exceeds soft cap ({MAX_INDICES_BYTES} bytes)."
|
|
370
|
+
) from exc
|
|
371
|
+
except CorruptStreamError as exc:
|
|
372
|
+
raise PackagesException(f"Cannot decode indices {indices_path}: {exc}") from exc
|
|
373
|
+
|
|
374
|
+
# Debian Policy 5.1: control data is UTF-8 -> [VALIDATION.md](VALIDATION.md#index-decoding)
|
|
375
|
+
try:
|
|
376
|
+
data = raw.decode("utf-8")
|
|
377
|
+
except UnicodeDecodeError as exc:
|
|
378
|
+
raise PackagesException(f"Cannot decode indices {indices_path}: {exc}") from exc
|
|
379
|
+
|
|
380
|
+
pkgs_total = self._parse_indices(data)
|
|
381
|
+
|
|
382
|
+
if comp and arch:
|
|
383
|
+
logger.info("Found %s packages in %s arch %s.", pkgs_total, comp, arch)
|
|
384
|
+
else:
|
|
385
|
+
logger.info("Found %s packages.", pkgs_total)
|
|
386
|
+
|
|
387
|
+
def save(self) -> dict[str, dict[str, str]] | None:
|
|
388
|
+
"""Write serialized content to every Packages indices file, each in its own codec.
|
|
389
|
+
|
|
390
|
+
Returns:
|
|
391
|
+
The filename and digests of the encoded data. If the Packages object has not been
|
|
392
|
+
modified, return None.
|
|
393
|
+
"""
|
|
394
|
+
if not self.modified:
|
|
395
|
+
return None
|
|
396
|
+
|
|
397
|
+
data = self._serialize_indices()
|
|
398
|
+
raw = data.encode("utf-8")
|
|
399
|
+
ret: dict[str, dict[str, str]] = {}
|
|
400
|
+
|
|
401
|
+
for filename in self.found_indices_paths:
|
|
402
|
+
enc_data = codec_for(filename).compress(raw)
|
|
403
|
+
|
|
404
|
+
logger.info("Save %s...", filename)
|
|
405
|
+
self.storage.put(filename, enc_data)
|
|
406
|
+
ret[filename] = calc_digests(enc_data)
|
|
407
|
+
|
|
408
|
+
# Per-arch Release is saved exactly once, not once per indices filename.
|
|
409
|
+
rdigests = self.release.save()
|
|
410
|
+
if rdigests is not None and self.release.filename is not None:
|
|
411
|
+
ret[self.release.filename] = rdigests
|
|
412
|
+
|
|
413
|
+
return ret
|
|
414
|
+
|
|
415
|
+
def dump(self, packages: Iterable[Package] | None = None) -> dict[str, dict[str, Any]]:
|
|
416
|
+
"""Dump packages object to a dict.
|
|
417
|
+
|
|
418
|
+
Args:
|
|
419
|
+
packages: The packages to list; None lists every package of the index.
|
|
420
|
+
|
|
421
|
+
Returns:
|
|
422
|
+
The dictionary representation of the packages object.
|
|
423
|
+
"""
|
|
424
|
+
listed = self.packages.values() if packages is None else packages
|
|
425
|
+
ret: dict[str, dict[str, Any]] = {
|
|
426
|
+
"packages": {"files": self.found_indices_paths, "data": list(listed)}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
# add release
|
|
430
|
+
if not self.release.is_empty():
|
|
431
|
+
ret["release"] = self.release.dump()
|
|
432
|
+
|
|
433
|
+
return ret
|
|
434
|
+
|
|
435
|
+
def insert(self, package: Package) -> None:
|
|
436
|
+
"""Add package to the Packages object.
|
|
437
|
+
|
|
438
|
+
Args:
|
|
439
|
+
package: The package to add.
|
|
440
|
+
"""
|
|
441
|
+
self.packages[str(package)] = package
|
|
442
|
+
self.modified = True
|
|
443
|
+
|
|
444
|
+
def matching(self, fields: dict[str, re.Pattern[str]]) -> set[str]:
|
|
445
|
+
"""Get the packages every pattern finds a match in.
|
|
446
|
+
|
|
447
|
+
Args:
|
|
448
|
+
fields: Compiled pattern per package field; a package matches when every pattern finds a
|
|
449
|
+
match in its field → [VALIDATION.md](VALIDATION.md#package-pattern-matching).
|
|
450
|
+
|
|
451
|
+
Returns:
|
|
452
|
+
Filenames of the matching packages.
|
|
453
|
+
"""
|
|
454
|
+
return {
|
|
455
|
+
pkg_name
|
|
456
|
+
for pkg_name, package in self.packages.items()
|
|
457
|
+
if all(
|
|
458
|
+
field in package.fields and rx.search(package.fields[field])
|
|
459
|
+
for field, rx in fields.items()
|
|
460
|
+
)
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
def remove(self, fields: dict[str, re.Pattern[str]]) -> set[str]:
|
|
464
|
+
"""Drop the matching packages from this index; never touch storage.
|
|
465
|
+
|
|
466
|
+
An `all` package is listed in several arch indices, so the caller deletes each returned
|
|
467
|
+
payload once → `RepoRW.remove_pkg`.
|
|
468
|
+
|
|
469
|
+
Args:
|
|
470
|
+
fields: Compiled pattern per package field; a package matches when every pattern finds a
|
|
471
|
+
match in its field → [VALIDATION.md](VALIDATION.md#package-pattern-matching).
|
|
472
|
+
|
|
473
|
+
Raises:
|
|
474
|
+
PackagesException: If a matched package has an unsafe Filename; nothing is dropped.
|
|
475
|
+
|
|
476
|
+
Returns:
|
|
477
|
+
Filenames of the dropped packages; empty when nothing matched.
|
|
478
|
+
"""
|
|
479
|
+
removed = self.matching(fields)
|
|
480
|
+
for pkg_name in removed:
|
|
481
|
+
if is_unsafe_path(pkg_name):
|
|
482
|
+
raise PackagesException(f"unsafe filename: {pkg_name}")
|
|
483
|
+
|
|
484
|
+
for pkg_name in removed:
|
|
485
|
+
self.packages.pop(pkg_name)
|
|
486
|
+
if removed:
|
|
487
|
+
self.modified = True
|
|
488
|
+
return removed
|
|
489
|
+
|
|
490
|
+
def get_indices_paths(self) -> list[str]:
|
|
491
|
+
"""Get list of all indices files.
|
|
492
|
+
|
|
493
|
+
Returns:
|
|
494
|
+
List of all indices file paths.
|
|
495
|
+
"""
|
|
496
|
+
ret: list[str] = []
|
|
497
|
+
ret.extend(self.found_indices_paths)
|
|
498
|
+
ret.extend(self.release.get_indices_paths())
|
|
499
|
+
|
|
500
|
+
return ret
|
|
501
|
+
|
|
502
|
+
def verify_checksum(self, filename: str, incoming_digests: dict[str, str]) -> bool:
|
|
503
|
+
"""Verify checksum of a package.
|
|
504
|
+
|
|
505
|
+
Args:
|
|
506
|
+
filename: The filename to verify.
|
|
507
|
+
incoming_digests: The checksums to verify.
|
|
508
|
+
|
|
509
|
+
Returns:
|
|
510
|
+
True if the checksum is valid, False otherwise.
|
|
511
|
+
"""
|
|
512
|
+
# every container keys by the `Filename` field itself, so the index is the lookup
|
|
513
|
+
package = self.packages.get(filename)
|
|
514
|
+
if package is None:
|
|
515
|
+
return False
|
|
516
|
+
|
|
517
|
+
return compare_digests(package.get_digests(), incoming_digests)
|
|
518
|
+
|
|
519
|
+
def __len__(self) -> int:
|
|
520
|
+
"""Return the number of packages.
|
|
521
|
+
|
|
522
|
+
Returns:
|
|
523
|
+
The number of packages.
|
|
524
|
+
"""
|
|
525
|
+
return len(self.packages)
|