Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/service.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-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 Affero General Public License version 3, or any later version | # License: GNU Affero 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 itertools | import itertools | ||||
import os | import os | ||||
import re | import re | ||||
from collections import defaultdict | from collections import defaultdict | ||||
from typing import Any, Dict, List, Set, Iterator, Optional, Tuple | from typing import Any, Dict, List, Set, Iterable, Iterator, Optional, Tuple | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | ||||
from swh.storage.algos import diff, revisions_walker | from swh.storage.algos import diff, revisions_walker | ||||
from swh.storage.algos.snapshot import snapshot_get_latest | |||||
from swh.vault.exc import NotFoundExc as VaultNotFoundExc | from swh.vault.exc import NotFoundExc as VaultNotFoundExc | ||||
from swh.web import config | from swh.web import config | ||||
from swh.web.common import converters | from swh.web.common import converters | ||||
from swh.web.common import query | from swh.web.common import query | ||||
from swh.web.common.exc import BadInputExc, NotFoundExc | from swh.web.common.exc import BadInputExc, NotFoundExc | ||||
from swh.web.common.origin_visits import get_origin_visit | from swh.web.common.origin_visits import get_origin_visit | ||||
from swh.web.common.typing import OriginInfo, OriginVisitInfo | from swh.web.common.typing import OriginInfo, OriginVisitInfo | ||||
▲ Show 20 Lines • Show All 974 Lines • ▼ Show 20 Lines | ): | ||||
snapshot = storage.snapshot_get_branches( | snapshot = storage.snapshot_get_branches( | ||||
snapshot_id_bin, branches_from.encode(), branches_count, target_types | snapshot_id_bin, branches_from.encode(), branches_count, target_types | ||||
) | ) | ||||
if not snapshot: | if not snapshot: | ||||
raise NotFoundExc("Snapshot with id %s not found!" % snapshot_id) | raise NotFoundExc("Snapshot with id %s not found!" % snapshot_id) | ||||
return converters.from_snapshot(snapshot) | return converters.from_snapshot(snapshot) | ||||
def lookup_latest_origin_snapshot(origin, allowed_statuses=None): | def lookup_latest_origin_snapshot( | ||||
origin: str, allowed_statuses: Iterable[str] = None | |||||
) -> Optional[Dict[str, Any]]: | |||||
"""Return information about the latest snapshot of an origin. | """Return information about the latest snapshot of an origin. | ||||
.. warning:: At most 1000 branches contained in the snapshot | .. warning:: At most 1000 branches contained in the snapshot | ||||
will be returned for performance reasons. | will be returned for performance reasons. | ||||
Args: | Args: | ||||
origin: URL or integer identifier of the origin | origin: URL or integer identifier of the origin | ||||
allowed_statuses: list of visit statuses considered | allowed_statuses: list of visit statuses considered | ||||
to find the latest snapshot for the visit. For instance, | to find the latest snapshot for the visit. For instance, | ||||
``allowed_statuses=['full']`` will only consider visits that | ``allowed_statuses=['full']`` will only consider visits that | ||||
have successfully run to completion. | have successfully run to completion. | ||||
Returns: | Returns: | ||||
A dict filled with the snapshot content. | A dict filled with the snapshot content. | ||||
""" | """ | ||||
snapshot = storage.snapshot_get_latest(origin, allowed_statuses) | snp = snapshot_get_latest( | ||||
return converters.from_snapshot(snapshot) | storage, origin, allowed_statuses=allowed_statuses, branches_count=1000 | ||||
) | |||||
return converters.from_snapshot(snp.to_dict()) if snp is not None else None | |||||
anlambert: I would rather write
```lang=python
return converters.from_snapshot(snapshot.to_dict()) if… | |||||
Done Inline Actionsyeah, i'd prefer the first one, but it's too long about 1 char ;) I'll rename to snp so it's fine ;) the second i stopped using since when using mypy when there are types complain I missed adding types to that function, I'll fix that if you don't mind ;) ardumont: yeah, i'd prefer the first one, but it's too long about 1 char ;)
I'll rename to snp so it's… | |||||
def lookup_snapshot_branch_name_from_tip_revision( | def lookup_snapshot_branch_name_from_tip_revision( | ||||
snapshot_id: str, revision_id: str | snapshot_id: str, revision_id: str | ||||
) -> Optional[str]: | ) -> Optional[str]: | ||||
"""Check if a revision corresponds to the tip of a snapshot branch | """Check if a revision corresponds to the tip of a snapshot branch | ||||
Args: | Args: | ||||
snapshot_id: hexadecimal representation of a snapshot id | snapshot_id: hexadecimal representation of a snapshot id | ||||
▲ Show 20 Lines • Show All 251 Lines • Show Last 20 Lines |
I would rather write
or