Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/converters.py
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015 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 | ||||
from typing import Optional, Dict | from typing import Optional, Dict | ||||
from swh.core.utils import encode_with_unescape | from swh.core.utils import encode_with_unescape | ||||
from swh.model import identifiers | from swh.model import identifiers | ||||
from swh.model.identifiers import parse_swhid | |||||
from swh.model.model import ( | |||||
MetadataAuthority, | |||||
MetadataAuthorityType, | |||||
MetadataFetcher, | |||||
MetadataTargetType, | |||||
RawExtrinsicMetadata, | |||||
) | |||||
from swh.model.hashutil import MultiHash | from swh.model.hashutil import MultiHash | ||||
from .utils import map_optional | |||||
DEFAULT_AUTHOR = { | DEFAULT_AUTHOR = { | ||||
"fullname": None, | "fullname": None, | ||||
"name": None, | "name": None, | ||||
"email": None, | "email": None, | ||||
} | } | ||||
DEFAULT_DATE = { | DEFAULT_DATE = { | ||||
▲ Show 20 Lines • Show All 261 Lines • ▼ Show 20 Lines | def db_to_release(db_release): | ||||
} | } | ||||
if "object_id" in db_release: | if "object_id" in db_release: | ||||
ret["object_id"] = db_release["object_id"] | ret["object_id"] = db_release["object_id"] | ||||
return ret | return ret | ||||
def db_to_raw_extrinsic_metadata(row) -> RawExtrinsicMetadata: | |||||
type_ = MetadataTargetType(row["raw_extrinsic_metadata.type"]) | |||||
id_ = row["raw_extrinsic_metadata.id"] | |||||
if type_ != MetadataTargetType.ORIGIN: | |||||
id_ = parse_swhid(id_) | |||||
return RawExtrinsicMetadata( | |||||
type=type_, | |||||
id=id_, | |||||
authority=MetadataAuthority( | |||||
type=MetadataAuthorityType(row["metadata_authority.type"]), | |||||
url=row["metadata_authority.url"], | |||||
), | |||||
fetcher=MetadataFetcher( | |||||
name=row["metadata_fetcher.name"], version=row["metadata_fetcher.version"], | |||||
), | |||||
discovery_date=row["discovery_date"], | |||||
format=row["format"], | |||||
metadata=row["raw_extrinsic_metadata.metadata"], | |||||
origin=row["origin"], | |||||
visit=row["visit"], | |||||
snapshot=map_optional(parse_swhid, row["snapshot"]), | |||||
release=map_optional(parse_swhid, row["release"]), | |||||
revision=map_optional(parse_swhid, row["revision"]), | |||||
path=row["path"], | |||||
directory=map_optional(parse_swhid, row["directory"]), | |||||
) | |||||
def origin_url_to_sha1(origin_url): | def origin_url_to_sha1(origin_url): | ||||
"""Convert an origin URL to a sha1. Encodes URL to utf-8.""" | """Convert an origin URL to a sha1. Encodes URL to utf-8.""" | ||||
return MultiHash.from_data(origin_url.encode("utf-8"), {"sha1"}).digest()["sha1"] | return MultiHash.from_data(origin_url.encode("utf-8"), {"sha1"}).digest()["sha1"] |