Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/service.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 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 os | import os | ||||
from collections import defaultdict | from collections import defaultdict | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.storage.algos import revisions_walker | |||||
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 NotFoundExc | from swh.web.common.exc import NotFoundExc | ||||
from swh.web import config | from swh.web import config | ||||
storage = config.storage() | storage = config.storage() | ||||
vault = config.vault() | vault = config.vault() | ||||
idx_storage = config.indexer_storage() | idx_storage = config.indexer_storage() | ||||
▲ Show 20 Lines • Show All 956 Lines • ▼ Show 20 Lines | for change in changes: | ||||
change['from'] = converters.from_directory_entry(change['from']) | change['from'] = converters.from_directory_entry(change['from']) | ||||
change['to'] = converters.from_directory_entry(change['to']) | change['to'] = converters.from_directory_entry(change['to']) | ||||
if change['from_path']: | if change['from_path']: | ||||
change['from_path'] = change['from_path'].decode('utf-8') | change['from_path'] = change['from_path'].decode('utf-8') | ||||
if change['to_path']: | if change['to_path']: | ||||
change['to_path'] = change['to_path'].decode('utf-8') | change['to_path'] = change['to_path'].decode('utf-8') | ||||
return changes | return changes | ||||
class _RevisionsWalkerProxy(object): | |||||
""" | |||||
Proxy class wrapping a revisions walker iterator from | |||||
swh-storage and performing needed conversions. | |||||
""" | |||||
def __init__(self, rev_walker_type, rev_start, *args, **kwargs): | |||||
rev_start_bin = hashutil.hash_to_bytes(rev_start) | |||||
self.revisions_walker = \ | |||||
revisions_walker.get_revisions_walker(rev_walker_type, | |||||
storage, | |||||
rev_start_bin, | |||||
*args, **kwargs) | |||||
def export_state(self): | |||||
return self.revisions_walker.export_state() | |||||
def __next__(self): | |||||
return converters.from_revision(next(self.revisions_walker)) | |||||
def __iter__(self): | |||||
return self | |||||
def get_revisions_walker(rev_walker_type, rev_start, *args, **kwargs): | |||||
""" | |||||
Utility function to instantiate a revisions walker of a given type, | |||||
see :mod:`swh.storage.algos.revisions_walker`. | |||||
Args: | |||||
rev_walker_type (str): the type of revisions walker to return, | |||||
possible values are: *committer_date*, *dfs*, *dfs_post*, | |||||
*bfs* and *path* | |||||
rev_start (str): hexadecimal representation of a revision identifier | |||||
args (list): position arguments to pass to the revisions walker | |||||
constructor | |||||
kwargs (dict): keyword arguments to pass to the revisions walker | |||||
constructor | |||||
""" | |||||
# first check if the provided revision is valid | |||||
lookup_revision(rev_start) | |||||
return _RevisionsWalkerProxy(rev_walker_type, rev_start, *args, **kwargs) |