Skip to content
Snippets Groups Projects
Commit e4566a66 authored by Daniele Serafini's avatar Daniele Serafini
Browse files

from_disk: get swhid from Content/Directory objects

Closes T3393
parent e09446a6
No related branches found
No related tags found
1 merge request!189swh-model: get SWHID from Content/Directory objects in from_disk
...@@ -57,22 +57,16 @@ class CoreSWHIDParamType(click.ParamType): ...@@ -57,22 +57,16 @@ class CoreSWHIDParamType(click.ParamType):
def swhid_of_file(path) -> CoreSWHID: def swhid_of_file(path) -> CoreSWHID:
from swh.model.from_disk import Content from swh.model.from_disk import Content
from swh.model.hashutil import hash_to_bytes
object = Content.from_file(path=path).get_data() object = Content.from_file(path=path)
return CoreSWHID( return object.swhid()
object_type=ObjectType.CONTENT, object_id=hash_to_bytes(object["sha1_git"])
)
def swhid_of_file_content(data) -> CoreSWHID: def swhid_of_file_content(data) -> CoreSWHID:
from swh.model.from_disk import Content 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() object = Content.from_bytes(mode=644, data=data)
return CoreSWHID( return object.swhid()
object_type=ObjectType.CONTENT, object_id=hash_to_bytes(object["sha1_git"])
)
def model_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> Directory: def model_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> Directory:
...@@ -88,13 +82,8 @@ def model_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> Direc ...@@ -88,13 +82,8 @@ def model_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> Direc
def swhid_of_dir(path: bytes, exclude_patterns: Iterable[bytes] = None) -> CoreSWHID: 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) obj = model_of_dir(path, exclude_patterns)
return obj.swhid()
return CoreSWHID(
object_type=ObjectType.DIRECTORY, object_id=hash_to_bytes(obj.get_data()["id"])
)
def swhid_of_origin(url): def swhid_of_origin(url):
...@@ -301,12 +290,7 @@ def identify( ...@@ -301,12 +290,7 @@ def identify(
for sub_obj in dir_obj.iter_tree(): for sub_obj in dir_obj.iter_tree():
path_name = "path" if "path" in sub_obj.data.keys() else "data" path_name = "path" if "path" in sub_obj.data.keys() else "data"
path = os.fsdecode(sub_obj.data[path_name]) path = os.fsdecode(sub_obj.data[path_name])
swhid = str( swhid = str(sub_obj.swhid())
CoreSWHID(
object_type=ObjectType[sub_obj.object_type.upper()],
object_id=sub_obj.hash,
)
)
msg = f"{swhid}\t{path}" if show_filename else f"{swhid}" msg = f"{swhid}\t{path}" if show_filename else f"{swhid}"
click.echo(msg) click.echo(msg)
else: else:
......
...@@ -19,7 +19,12 @@ from typing_extensions import Final ...@@ -19,7 +19,12 @@ from typing_extensions import Final
from . import model from . import model
from .exceptions import InvalidDirectoryPath from .exceptions import InvalidDirectoryPath
from .hashutil import MultiHash 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_bytes as id_to_bytes
from .identifiers import identifier_to_str as id_to_str from .identifiers import identifier_to_str as id_to_str
from .merkle import MerkleLeaf, MerkleNode from .merkle import MerkleLeaf, MerkleNode
...@@ -207,6 +212,13 @@ class Content(MerkleLeaf): ...@@ -207,6 +212,13 @@ class Content(MerkleLeaf):
obj = cls(ret) obj = cls(ret)
return obj return obj
def swhid(self) -> CoreSWHID:
"""Return node identifier as a SWHID
"""
return CoreSWHID(
object_type=ObjectType.CONTENT, object_id=self.hash
)
def __repr__(self): def __repr__(self):
return "Content(id=%s)" % id_to_str(self.hash) return "Content(id=%s)" % id_to_str(self.hash)
...@@ -482,6 +494,13 @@ class Directory(MerkleNode): ...@@ -482,6 +494,13 @@ class Directory(MerkleNode):
return self.__entries return self.__entries
def swhid(self) -> CoreSWHID:
"""Return node identifier as a SWHID
"""
return CoreSWHID(
object_type=ObjectType.DIRECTORY, object_id=self.hash
)
def compute_hash(self): def compute_hash(self):
return id_to_bytes(directory_identifier({"entries": self.entries})) return id_to_bytes(directory_identifier({"entries": self.entries}))
......
...@@ -531,6 +531,22 @@ class TestContent(DataMixin, unittest.TestCase): ...@@ -531,6 +531,22 @@ class TestContent(DataMixin, unittest.TestCase):
self.assertContentEqual(conv_content, content) self.assertContentEqual(conv_content, content)
self.assertIn(hash_to_hex(conv_content.hash), repr(conv_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): class SymlinkToContent(DataMixin, unittest.TestCase):
def setUp(self): def setUp(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment