Add getter functions for Signed objects

These are equivalent to the edit_X() context managers but for cases
where user is not interested in creating a new version of the metadata.

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
This commit is contained in:
Jussi Kukkonen 2023-03-03 11:19:38 +02:00
parent c3411dc59e
commit 79eb91d278
2 changed files with 34 additions and 11 deletions

View file

@ -176,15 +176,10 @@ def submit_role(self, role: str, data: bytes) -> bool:
if not targetpath.startswith(f"{role}/"):
raise ValueError(f"targets allowed under {role}/ only")
targets_md = self.role_cache["targets"][-1]
targets_md = self.open("targets")
targets_md.verify_delegate(role, md)
if role in self.role_cache:
current_md = self.role_cache[role][-1]
current_ver = current_md.signed.version
else:
current_ver = 0
if md.signed.version != current_ver + 1:
if md.signed.version != self.targets(role).version + 1:
raise ValueError("Invalid version {md.signed.version}")
except (RepositoryError, ValueError) as e:

View file

@ -107,7 +107,7 @@ def edit_root(self) -> Generator[Root, None, None]:
"""Context manager for editing root metadata. See edit()"""
with self.edit(Root.type) as root:
if not isinstance(root, Root):
raise RuntimeError("Unexpected Root type")
raise RuntimeError("Unexpected root type")
yield root
@contextmanager
@ -115,7 +115,7 @@ def edit_timestamp(self) -> Generator[Timestamp, None, None]:
"""Context manager for editing timestamp metadata. See edit()"""
with self.edit(Timestamp.type) as timestamp:
if not isinstance(timestamp, Timestamp):
raise RuntimeError("Unexpected Timestamp type")
raise RuntimeError("Unexpected timestamp type")
yield timestamp
@contextmanager
@ -123,7 +123,7 @@ def edit_snapshot(self) -> Generator[Snapshot, None, None]:
"""Context manager for editing snapshot metadata. See edit()"""
with self.edit(Snapshot.type) as snapshot:
if not isinstance(snapshot, Snapshot):
raise RuntimeError("Unexpected Snapshot type")
raise RuntimeError("Unexpected snapshot type")
yield snapshot
@contextmanager
@ -131,9 +131,37 @@ def edit_targets(self, rolename: str) -> Generator[Targets, None, None]:
"""Context manager for editing targets metadata. See edit()"""
with self.edit(rolename) as targets:
if not isinstance(targets, Targets):
raise RuntimeError(f"Unexpected Targets ({rolename}) type")
raise RuntimeError(f"Unexpected targets ({rolename}) type")
yield targets
def root(self) -> Root:
"""Read current root metadata"""
root = self.open(Root.type).signed
if not isinstance(root, Root):
raise RuntimeError("Unexpected root type")
return root
def timestamp(self) -> Timestamp:
"""Read current timestamp metadata"""
timestamp = self.open(Timestamp.type).signed
if not isinstance(timestamp, Timestamp):
raise RuntimeError("Unexpected timestamp type")
return timestamp
def snapshot(self) -> Snapshot:
"""Read current snapshot metadata"""
snapshot = self.open(Snapshot.type).signed
if not isinstance(snapshot, Snapshot):
raise RuntimeError("Unexpected snapshot type")
return snapshot
def targets(self, rolename: str) -> Targets:
"""Read current targets metadata"""
targets = self.open(rolename).signed
if not isinstance(targets, Targets):
raise RuntimeError("Unexpected targets type")
return targets
def do_snapshot(
self, force: bool = False
) -> Tuple[bool, Dict[str, MetaFile]]: