|
3 | 3 | # SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | 5 | from abc import abstractmethod |
| 6 | +import dataclasses |
6 | 7 | from functools import reduce |
7 | 8 | import hashlib |
8 | | -import os |
| 9 | +import json |
9 | 10 | import re |
| 11 | +import sys |
10 | 12 | from typing import Generator, Tuple, Type |
11 | 13 | from pathlib import Path |
12 | 14 | from urllib.request import urlretrieve |
13 | 15 | from ..dpkg import package |
14 | 16 | from ..snapshot import client as sdlclient |
15 | 17 |
|
16 | 18 |
|
| 19 | +class PackageResolverCache: |
| 20 | + """ |
| 21 | + Maps packages to RemoteFile instances to avoid expensive calls to the upstream mirror. |
| 22 | + This dummy implementation can be used to not cache. |
| 23 | + """ |
| 24 | + |
| 25 | + def lookup( |
| 26 | + self, p: package.SourcePackage | package.BinaryPackage |
| 27 | + ) -> list["sdlclient.RemoteFile"] | None: |
| 28 | + return None |
| 29 | + |
| 30 | + def insert( |
| 31 | + self, p: package.SourcePackage | package.BinaryPackage, files: list["sdlclient.RemoteFile"] |
| 32 | + ) -> None: |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class PersistentResolverCache(PackageResolverCache): |
| 37 | + """ |
| 38 | + Trivial implementation of a file-backed cache. Each cache entry is stored as individual file |
| 39 | + in the cachedir. |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__(self, cachedir: Path): |
| 43 | + self.cachedir = cachedir |
| 44 | + cachedir.mkdir(exist_ok=True) |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def _package_hash(p: package.SourcePackage | package.BinaryPackage) -> str: |
| 48 | + return hashlib.sha256( |
| 49 | + json.dumps({"name": p.name, "version": p.version}, sort_keys=True).encode("utf-8") |
| 50 | + ).hexdigest() |
| 51 | + |
| 52 | + def _entry_path(self, hash: str) -> Path: |
| 53 | + return self.cachedir / f"{hash}.json" |
| 54 | + |
| 55 | + def lookup( |
| 56 | + self, p: package.SourcePackage | package.BinaryPackage |
| 57 | + ) -> list["sdlclient.RemoteFile"] | None: |
| 58 | + hash = self._package_hash(p) |
| 59 | + entry = self._entry_path(hash) |
| 60 | + if not entry.is_file(): |
| 61 | + return None |
| 62 | + with open(entry, "r") as f: |
| 63 | + data = json.load(f) |
| 64 | + return [sdlclient.RemoteFile(**d) for d in data] |
| 65 | + |
| 66 | + def insert( |
| 67 | + self, p: package.SourcePackage | package.BinaryPackage, files: list["sdlclient.RemoteFile"] |
| 68 | + ) -> None: |
| 69 | + hash = self._package_hash(p) |
| 70 | + entry = self._entry_path(hash) |
| 71 | + with open(entry.with_suffix(".tmp"), "w") as f: |
| 72 | + json.dump([dataclasses.asdict(rf) for rf in files], f) |
| 73 | + entry.with_suffix(".tmp").rename(entry) |
| 74 | + |
| 75 | + |
17 | 76 | class PackageResolver: |
18 | 77 | def __init__(self): |
19 | 78 | self.purl_regex = re.compile(r"pkg:deb\/debian\/(.*)@(.*)[?]arch=(.*)$") |
@@ -44,17 +103,27 @@ def package_from_purl(self, purl: str) -> Tuple[str, str, str]: |
44 | 103 |
|
45 | 104 | @staticmethod |
46 | 105 | def resolve( |
47 | | - sdl: sdlclient.SnapshotDataLake, p: package.SourcePackage | package.BinaryPackage |
48 | | - ) -> Generator["sdlclient.RemoteFile", None, None]: |
| 106 | + sdl: sdlclient.SnapshotDataLake, |
| 107 | + p: package.SourcePackage | package.BinaryPackage, |
| 108 | + cache: PackageResolverCache = PackageResolverCache(), |
| 109 | + ) -> list["sdlclient.RemoteFile"]: |
49 | 110 | """ |
50 | 111 | Resolve a local package to references on the upstream snapshot mirror |
51 | 112 | """ |
| 113 | + cached_files = cache.lookup(p) |
| 114 | + if cached_files: |
| 115 | + return cached_files |
| 116 | + |
| 117 | + # Determine which type of package and fetch files |
52 | 118 | if isinstance(p, package.SourcePackage): |
53 | | - return sdlclient.SourcePackage(sdl, p.name, p.version).srcfiles() |
54 | | - elif isinstance(p, package.BinaryPackage): |
55 | | - return sdlclient.BinaryPackage(sdl, p.name, p.version, None, None).files( |
| 119 | + files = sdlclient.SourcePackage(sdl, p.name, p.version).srcfiles() |
| 120 | + else: |
| 121 | + files = sdlclient.BinaryPackage(sdl, p.name, p.version, None, None).files( |
56 | 122 | arch=p.architecture |
57 | 123 | ) |
| 124 | + files_list = list(files) |
| 125 | + cache.insert(p, files_list) |
| 126 | + return files_list |
58 | 127 |
|
59 | 128 | @staticmethod |
60 | 129 | def create(filename: Path) -> Type["PackageResolver"]: |
|
0 commit comments