Skip to content

Commit 24b1db6

Browse files
kamui-finjku
authored andcommitted
feat: generate hash-prefixed path names for target
Signed-off-by: Kamui <fin-kamui@pm.me>
1 parent db02702 commit 24b1db6

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

tests/test_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,12 @@ def test_targetfile_from_data(self) -> None:
725725
targetfile_from_data = TargetFile.from_data(target_file_path, data)
726726
targetfile_from_data.verify_length_and_hashes(data)
727727

728+
def test_targetfile_hash_prefix_paths(self) -> None:
729+
target = TargetFile(
730+
100, {"sha256": "abc", "md5": "def"}, "public/path/file.ext"
731+
)
732+
self.assertEqual(sorted(target.get_prefixed_paths()), ["public/path/abc.file.ext", "public/path/def.file.ext"])
733+
728734
def test_is_delegated_role(self) -> None:
729735
# test path matches
730736
# see more extensive tests in test_is_target_in_pathpattern()

tuf/api/metadata.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io
3434
import logging
3535
import tempfile
36+
import pathlib
3637
from datetime import datetime
3738
from typing import (
3839
IO,
@@ -1737,6 +1738,14 @@ def verify_length_and_hashes(self, data: Union[bytes, IO[bytes]]) -> None:
17371738
self._verify_length(data, self.length)
17381739
self._verify_hashes(data, self.hashes)
17391740

1741+
def get_prefixed_paths(self) -> List[str]:
1742+
paths = []
1743+
path = pathlib.Path(self.path)
1744+
name, parent = path.name, path.parent
1745+
for hash in self.hashes.values():
1746+
paths.append(str(parent.joinpath(f"{hash}.{name}")))
1747+
return paths
1748+
17401749

17411750
class Targets(Signed):
17421751
"""A container for the signed part of targets metadata.

0 commit comments

Comments
 (0)