Changeset View
Changeset View
Standalone View
Standalone View
swh/model/from_disk.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
"""Conversion from filesystem tree to SWH objects. | """Conversion from filesystem tree to SWH objects. | ||||
This module allows reading a tree of directories and files from a local | This module allows reading a tree of directories and files from a local | ||||
filesystem, and convert them to in-memory data structures, which can then | filesystem, and convert them to in-memory data structures, which can then | ||||
▲ Show 20 Lines • Show All 208 Lines • ▼ Show 20 Lines | def swhid(self) -> CoreSWHID: | ||||
return CoreSWHID(object_type=ObjectType.CONTENT, object_id=self.hash) | return CoreSWHID(object_type=ObjectType.CONTENT, object_id=self.hash) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "Content(id=%s)" % hash_to_hex(self.hash) | return "Content(id=%s)" % hash_to_hex(self.hash) | ||||
def compute_hash(self): | def compute_hash(self): | ||||
return self.data["sha1_git"] | return self.data["sha1_git"] | ||||
def hash_to_int(self, hash: bytes) -> int: | |||||
return int.from_bytes(hash, byteorder="big") | |||||
def to_model(self) -> model.BaseContent: | def to_model(self) -> model.BaseContent: | ||||
"""Builds a `model.BaseContent` object based on this leaf.""" | """Builds a `model.BaseContent` object based on this leaf.""" | ||||
data = self.get_data().copy() | data = self.get_data().copy() | ||||
data.pop("perms", None) | data.pop("perms", None) | ||||
if data["status"] == "absent": | if data["status"] == "absent": | ||||
data.pop("path", None) | data.pop("path", None) | ||||
return model.SkippedContent.from_dict(data) | return model.SkippedContent.from_dict(data) | ||||
elif "data" in data: | elif "data" in data: | ||||
▲ Show 20 Lines • Show All 266 Lines • ▼ Show 20 Lines | class Directory(MerkleNode): | ||||
def swhid(self) -> CoreSWHID: | def swhid(self) -> CoreSWHID: | ||||
"""Return node identifier as a SWHID""" | """Return node identifier as a SWHID""" | ||||
return CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=self.hash) | return CoreSWHID(object_type=ObjectType.DIRECTORY, object_id=self.hash) | ||||
def compute_hash(self): | def compute_hash(self): | ||||
return self.to_model().id | return self.to_model().id | ||||
def hash_to_int(self, hash: bytes) -> int: | |||||
return int.from_bytes(hash, byteorder="big") | |||||
def to_model(self) -> model.Directory: | def to_model(self) -> model.Directory: | ||||
"""Builds a `model.Directory` object based on this node; | """Builds a `model.Directory` object based on this node; | ||||
ignoring its children.""" | ignoring its children.""" | ||||
if self.__model_object is None: | if self.__model_object is None: | ||||
DirectoryEntry = model.DirectoryEntry | DirectoryEntry = model.DirectoryEntry | ||||
entries = [] | entries = [] | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |