Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/interface.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 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 | ||||
from __future__ import annotations | |||||
from dataclasses import dataclass | from dataclasses import dataclass | ||||
from datetime import datetime | from datetime import datetime | ||||
import enum | import enum | ||||
from typing import Dict, Generator, Iterable, Optional, Set, Union | from types import TracebackType | ||||
from typing import Dict, Generator, Iterable, Optional, Set, Type, Union | |||||
from typing_extensions import Protocol, runtime_checkable | from typing_extensions import Protocol, runtime_checkable | ||||
from swh.core.api import remote_api_endpoint | from swh.core.api import remote_api_endpoint | ||||
from swh.model.model import Sha1Git | from swh.model.model import Sha1Git | ||||
from .model import DirectoryEntry, FileEntry, OriginEntry, RevisionEntry | from .model import DirectoryEntry, FileEntry, OriginEntry, RevisionEntry | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | class RelationData: | ||||
""" | """ | ||||
dst: Sha1Git | dst: Sha1Git | ||||
path: Optional[bytes] | path: Optional[bytes] | ||||
@runtime_checkable | @runtime_checkable | ||||
class ProvenanceStorageInterface(Protocol): | class ProvenanceStorageInterface(Protocol): | ||||
def __enter__(self) -> ProvenanceStorageInterface: | |||||
... | |||||
def __exit__( | |||||
self, | |||||
exc_type: Optional[Type[BaseException]], | |||||
exc_val: Optional[BaseException], | |||||
exc_tb: Optional[TracebackType], | |||||
) -> None: | |||||
... | |||||
@remote_api_endpoint("close") | |||||
def close(self) -> None: | |||||
"""Close connection to the storage and release resources.""" | |||||
... | |||||
@remote_api_endpoint("content_add") | @remote_api_endpoint("content_add") | ||||
def content_add( | def content_add( | ||||
self, cnts: Union[Iterable[Sha1Git], Dict[Sha1Git, Optional[datetime]]] | self, cnts: Union[Iterable[Sha1Git], Dict[Sha1Git, Optional[datetime]]] | ||||
) -> bool: | ) -> bool: | ||||
"""Add blobs identified by sha1 ids, with an optional associated date (as paired | """Add blobs identified by sha1 ids, with an optional associated date (as paired | ||||
in `cnts`) to the provenance storage. Return a boolean stating whether the | in `cnts`) to the provenance storage. Return a boolean stating whether the | ||||
information was successfully stored. | information was successfully stored. | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | def location_add(self, paths: Iterable[bytes]) -> bool: | ||||
... | ... | ||||
@remote_api_endpoint("location_get_all") | @remote_api_endpoint("location_get_all") | ||||
def location_get_all(self) -> Set[bytes]: | def location_get_all(self) -> Set[bytes]: | ||||
"""Retrieve all paths present in the provenance model. | """Retrieve all paths present in the provenance model. | ||||
This method is used only in tests.""" | This method is used only in tests.""" | ||||
... | ... | ||||
@remote_api_endpoint("open") | |||||
def open(self) -> None: | |||||
"""Open connection to the storage and allocate necessary resources.""" | |||||
... | |||||
@remote_api_endpoint("origin_add") | @remote_api_endpoint("origin_add") | ||||
def origin_add(self, orgs: Dict[Sha1Git, str]) -> bool: | def origin_add(self, orgs: Dict[Sha1Git, str]) -> bool: | ||||
"""Add origins identified by sha1 ids, with their corresponding url (as paired | """Add origins identified by sha1 ids, with their corresponding url (as paired | ||||
in `orgs`) to the provenance storage. Return a boolean stating if the | in `orgs`) to the provenance storage. Return a boolean stating if the | ||||
information was successfully stored. | information was successfully stored. | ||||
""" | """ | ||||
... | ... | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | class ProvenanceStorageInterface(Protocol): | ||||
def with_path(self) -> bool: | def with_path(self) -> bool: | ||||
... | ... | ||||
@runtime_checkable | @runtime_checkable | ||||
class ProvenanceInterface(Protocol): | class ProvenanceInterface(Protocol): | ||||
storage: ProvenanceStorageInterface | storage: ProvenanceStorageInterface | ||||
def __enter__(self) -> ProvenanceInterface: | |||||
... | |||||
def __exit__( | |||||
self, | |||||
exc_type: Optional[Type[BaseException]], | |||||
exc_val: Optional[BaseException], | |||||
exc_tb: Optional[TracebackType], | |||||
) -> None: | |||||
... | |||||
def close(self) -> None: | |||||
"""Close connection to the underlying `storage` and release resources.""" | |||||
... | |||||
def flush(self) -> None: | def flush(self) -> None: | ||||
"""Flush internal cache to the underlying `storage`.""" | """Flush internal cache to the underlying `storage`.""" | ||||
... | ... | ||||
def content_add_to_directory( | def content_add_to_directory( | ||||
self, directory: DirectoryEntry, blob: FileEntry, prefix: bytes | self, directory: DirectoryEntry, blob: FileEntry, prefix: bytes | ||||
) -> None: | ) -> None: | ||||
"""Associate `blob` with `directory` in the provenance model. `prefix` is the | """Associate `blob` with `directory` in the provenance model. `prefix` is the | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | class ProvenanceInterface(Protocol): | ||||
def directory_set_date_in_isochrone_frontier( | def directory_set_date_in_isochrone_frontier( | ||||
self, directory: DirectoryEntry, date: datetime | self, directory: DirectoryEntry, date: datetime | ||||
) -> None: | ) -> None: | ||||
"""Associate `date` to `directory` as it's earliest known date as an isochrone | """Associate `date` to `directory` as it's earliest known date as an isochrone | ||||
frontier in the provenance model. | frontier in the provenance model. | ||||
""" | """ | ||||
... | ... | ||||
def open(self) -> None: | |||||
"""Open connection to the underlying `storage` and allocate necessary | |||||
resources. | |||||
""" | |||||
... | |||||
def origin_add(self, origin: OriginEntry) -> None: | def origin_add(self, origin: OriginEntry) -> None: | ||||
"""Add `origin` to the provenance model.""" | """Add `origin` to the provenance model.""" | ||||
... | ... | ||||
def revision_add(self, revision: RevisionEntry) -> None: | def revision_add(self, revision: RevisionEntry) -> None: | ||||
"""Add `revision` to the provenance model. This implies storing `revision`'s | """Add `revision` to the provenance model. This implies storing `revision`'s | ||||
date in the model, thus `revision.date` must be a valid date. | date in the model, thus `revision.date` must be a valid date. | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines |