Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/loader.py
# Copyright (C) 2019-2021 The Software Heritage developers | # Copyright (C) 2019-2021 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 hashlib | |||||
from itertools import islice | from itertools import islice | ||||
import json | import json | ||||
import logging | import logging | ||||
import os | import os | ||||
import string | |||||
import sys | import sys | ||||
import tempfile | import tempfile | ||||
from typing import ( | from typing import ( | ||||
Any, | Any, | ||||
Dict, | Dict, | ||||
Generic, | Generic, | ||||
Iterable, | Iterable, | ||||
Iterator, | Iterator, | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | class BasePackageInfo: | ||||
directory_extrinsic_metadata = attr.ib( | directory_extrinsic_metadata = attr.ib( | ||||
type=List[RawExtrinsicMetadataCore], default=[], kw_only=True, | type=List[RawExtrinsicMetadataCore], default=[], kw_only=True, | ||||
) | ) | ||||
# TODO: add support for metadata for directories and contents | # TODO: add support for metadata for directories and contents | ||||
@property | @property | ||||
def ID_KEYS(self): | def MANIFEST_FORMAT(self) -> string.Template: | ||||
raise NotImplementedError(f"{self.__class__.__name__} is missing ID_KEYS") | """A string.Template object used to format a manifest, which is hashed | ||||
to get the extid of this package info object""" | |||||
raise NotImplementedError( | |||||
f"{self.__class__.__name__} is missing MANIFEST_FORMAT " | |||||
f"or an override of extid()" | |||||
) | |||||
def artifact_identity(self): | def extid(self) -> bytes: | ||||
return [getattr(self, k) for k in self.ID_KEYS] | """Returns a unique intrinsic identifier of this package info""" | ||||
manifest = self.MANIFEST_FORMAT.substitute( | |||||
{k: str(v) for (k, v) in attr.asdict(self).items()} | |||||
) | |||||
return hashlib.sha256(manifest.encode()).digest() | |||||
TPackageInfo = TypeVar("TPackageInfo", bound=BasePackageInfo) | TPackageInfo = TypeVar("TPackageInfo", bound=BasePackageInfo) | ||||
class PackageLoader(BaseLoader, Generic[TPackageInfo]): | class PackageLoader(BaseLoader, Generic[TPackageInfo]): | ||||
# Origin visit type (str) set by the loader | # Origin visit type (str) set by the loader | ||||
visit_type = "" | visit_type = "" | ||||
▲ Show 20 Lines • Show All 714 Lines • Show Last 20 Lines |