Skip to content

Commit 87c74a8

Browse files
committed
examples: Maintain a meta info cache
This is not required for the demo but is more realistic: we keep a cache of targets versions so that we can produce a new snapshot whenever one is needed, without accessing all of the targets metadata to do so. Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent dd36b73 commit 87c74a8

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

examples/repository/_simplerepo.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def __init__(self) -> None:
5959
self.signer_cache: Dict[str, List[Signer]] = defaultdict(list)
6060
# all target content
6161
self.target_cache: Dict[str, bytes] = {}
62+
# version cache for snapshot and all targets, updated in close()
63+
self._snapshot_info = MetaFile(1)
64+
self._targets_infos = defaultdict(lambda: MetaFile(1))
6265

6366
# setup a basic repository, generate signing key per top-level role
6467
with self.edit("root", init=True) as root:
@@ -73,14 +76,11 @@ def __init__(self) -> None:
7376

7477
@property
7578
def targets_infos(self) -> Dict[str, MetaFile]:
76-
# TODO should track changes to snapshot meta and not recreate it here
77-
targets: Targets = self.role_cache["targets"][-1].signed
78-
return {"targets.json": MetaFile(targets.version)}
79+
return self._targets_infos
7980

8081
@property
8182
def snapshot_info(self) -> MetaFile:
82-
snapshot = self.role_cache["snapshot"][-1].signed
83-
return MetaFile(snapshot.version)
83+
return self._snapshot_info
8484

8585
def open(self, role: str, init: bool = False) -> Metadata:
8686
"""Return current Metadata for role from 'storage' (or create a new one)"""
@@ -110,7 +110,12 @@ def close(self, role: str, md: Metadata, sign_only: bool = False) -> None:
110110
for signer in self.signer_cache[role]:
111111
md.sign(signer, append=True)
112112

113+
# store new metadata version, update version caches
113114
self.role_cache[role].append(md)
115+
if role == "snapshot":
116+
self._snapshot_info.version = md.signed.version
117+
elif role not in ["root", "timestamp"]:
118+
self._targets_infos[f"{role}.json"].version = md.signed.version
114119

115120
def add_target(self, path: str, content: str) -> None:
116121
"""Add a target to repository"""

tuf/repository/_repository.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def close(self, role: str, md: Metadata, sign_only: bool = False) -> None:
4545
If sign_only, then just append signatures of all available keys.
4646
4747
If not sign_only, update expiry and version and replace signatures
48-
with ones from all available keys."""
48+
with ones from all available keys. Keep snapshot_info and targets_infos
49+
updated."""
4950
raise NotImplementedError
5051

5152
@property

0 commit comments

Comments
 (0)