diff --git a/swh/model/cli.py b/swh/model/cli.py --- a/swh/model/cli.py +++ b/swh/model/cli.py @@ -57,22 +57,16 @@ def swhid_of_file(path) -> CoreSWHID: from swh.model.from_disk import Content - from swh.model.hashutil import hash_to_bytes - object = Content.from_file(path=path).get_data() - return CoreSWHID( - object_type=ObjectType.CONTENT, object_id=hash_to_bytes(object["sha1_git"]) - ) + object = Content.from_file(path=path) + return object.swhid def swhid_of_file_content(data) -> CoreSWHID: from swh.model.from_disk import Content - from swh.model.hashutil import hash_to_bytes - object = Content.from_bytes(mode=644, data=data).get_data() - return CoreSWHID( - object_type=ObjectType.CONTENT, object_id=hash_to_bytes(object["sha1_git"]) - ) + object = Content.from_bytes(mode=644, data=data) + return object.swhid def model_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> Directory: @@ -88,13 +82,8 @@ def swhid_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> CoreSWHID: - from swh.model.hashutil import hash_to_bytes - obj = model_of_dir(path, exclude_patterns) - - return CoreSWHID( - object_type=ObjectType.DIRECTORY, object_id=hash_to_bytes(obj.get_data()["id"]) - ) + return obj.swhid def swhid_of_origin(url): diff --git a/swh/model/from_disk.py b/swh/model/from_disk.py --- a/swh/model/from_disk.py +++ b/swh/model/from_disk.py @@ -19,7 +19,12 @@ from . import model from .exceptions import InvalidDirectoryPath from .hashutil import MultiHash -from .identifiers import directory_entry_sort_key, directory_identifier +from .identifiers import ( + CoreSWHID, + ObjectType, + directory_entry_sort_key, + directory_identifier, +) from .identifiers import identifier_to_bytes as id_to_bytes from .identifiers import identifier_to_str as id_to_str from .merkle import MerkleLeaf, MerkleNode @@ -207,6 +212,14 @@ obj = cls(ret) return obj + @property + def swhid(self) -> CoreSWHID: + """return node identifier as a SWHID + """ + return CoreSWHID( + object_type=ObjectType[self.object_type.upper()], object_id=self.hash + ) + def __repr__(self): return "Content(id=%s)" % id_to_str(self.hash) @@ -482,6 +495,14 @@ return self.__entries + @property + def swhid(self) -> CoreSWHID: + """return node identifier as a SWHID + """ + return CoreSWHID( + object_type=ObjectType[self.object_type.upper()], object_id=self.hash + ) + def compute_hash(self): return id_to_bytes(directory_identifier({"entries": self.entries})) diff --git a/swh/model/tests/test_from_disk.py b/swh/model/tests/test_from_disk.py --- a/swh/model/tests/test_from_disk.py +++ b/swh/model/tests/test_from_disk.py @@ -531,6 +531,22 @@ self.assertContentEqual(conv_content, content) self.assertIn(hash_to_hex(conv_content.hash), repr(conv_content)) + def test_content_swhid(self): + for _, content in self.contents.items(): + content_res = Content.from_bytes(mode=content["mode"], data=content["data"]) + content_swhid = "swh:1:cnt:" + hash_to_hex(content["sha1_git"]) + assert str(content_res.swhid) == content_swhid + + +class TestDirectory(DataMixin, unittest.TestCase): + def setUp(self): + super().setUp() + + def test_directory_swhid(self): + directory_swhid = "swh:1:dir:" + hash_to_hex(self.empty_directory["id"]) + directory = Directory.from_disk(path=self.tmpdir_name) + assert str(directory.swhid) == directory_swhid + class SymlinkToContent(DataMixin, unittest.TestCase): def setUp(self):