Changeset View
Changeset View
Standalone View
Standalone View
swh/model/from_disk.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 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 | ||||
import datetime | import datetime | ||||
import enum | import enum | ||||
import os | import os | ||||
import stat | import stat | ||||
import attr | import attr | ||||
from attrs_strict import type_validator | from attrs_strict import type_validator | ||||
from typing import List, Optional, Iterable, Any | from typing import Any, Iterable, List, Optional, Tuple | ||||
from typing_extensions import Final | from typing_extensions import Final | ||||
from .hashutil import MultiHash | from .hashutil import MultiHash | ||||
from .merkle import MerkleLeaf, MerkleNode | from .merkle import MerkleLeaf, MerkleNode | ||||
from .identifiers import ( | from .identifiers import ( | ||||
directory_entry_sort_key, | directory_entry_sort_key, | ||||
directory_identifier, | directory_identifier, | ||||
identifier_to_bytes as id_to_bytes, | identifier_to_bytes as id_to_bytes, | ||||
▲ Show 20 Lines • Show All 252 Lines • ▼ Show 20 Lines | ): | ||||
if case_sensitive: | if case_sensitive: | ||||
return dirname not in names | return dirname not in names | ||||
else: | else: | ||||
return dirname.lower() not in names | return dirname.lower() not in names | ||||
return named_filter | return named_filter | ||||
def iter_directory( | |||||
directory, | |||||
) -> Tuple[List[model.Content], List[model.SkippedContent], List[model.Directory]]: | |||||
"""Return the directory listing from a disk-memory directory instance. | |||||
Raises: | |||||
TypeError in case an unexpected object type is listed. | |||||
Returns: | |||||
Tuple of respectively iterable of content, skipped content and directories. | |||||
""" | |||||
contents: List[model.Content] = [] | |||||
skipped_contents: List[model.SkippedContent] = [] | |||||
directories: List[model.Directory] = [] | |||||
for obj in directory.iter_tree(): | |||||
obj = obj.to_model() | |||||
obj_type = obj.object_type | |||||
if obj_type in ("content", "content_file"): | |||||
anlambert: You should use model constants here for readability:
```lang=python
if obj_type in (model. | |||||
ardumontAuthorUnsubmitted Done Inline Actionsyeah, i'm kinda lazy today ;) ardumont: yeah, i'm kinda lazy today ;) | |||||
# FIXME: read the data from disk later (when the | |||||
# storage buffer is flushed). | |||||
obj = obj.with_data() | |||||
contents.append(obj) | |||||
elif obj_type == "skipped_content": | |||||
anlambertUnsubmitted Done Inline Actionssame here anlambert: same here | |||||
skipped_contents.append(obj) | |||||
elif obj_type == "directory": | |||||
anlambertUnsubmitted Done Inline Actionssame here anlambert: same here | |||||
directories.append(obj) | |||||
else: | |||||
raise TypeError(f"Unexpected object type from disk: {obj}") | |||||
return contents, skipped_contents, directories | |||||
class Directory(MerkleNode): | class Directory(MerkleNode): | ||||
"""Representation of a Software Heritage directory as a node in a Merkle Tree. | """Representation of a Software Heritage directory as a node in a Merkle Tree. | ||||
This class can be used to generate, from an on-disk directory, all the | This class can be used to generate, from an on-disk directory, all the | ||||
objects that need to be sent to the Software Heritage archive. | objects that need to be sent to the Software Heritage archive. | ||||
The :func:`from_disk` constructor allows you to generate the data structure | The :func:`from_disk` constructor allows you to generate the data structure | ||||
from a directory on disk. The resulting :class:`Directory` can then be | from a directory on disk. The resulting :class:`Directory` can then be | ||||
▲ Show 20 Lines • Show All 159 Lines • Show Last 20 Lines |
You should use model constants here for readability: