Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/cookers/revision_flat.py
# Copyright (C) 2016-2017 The Software Heritage developers | # Copyright (C) 2016-2017 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 tarfile | |||||
import tempfile | import tempfile | ||||
from pathlib import Path | from pathlib import Path | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from .base import BaseVaultCooker, DirectoryBuilder, get_tar_bytes | from .base import BaseVaultCooker, DirectoryBuilder | ||||
class RevisionFlatCooker(BaseVaultCooker): | class RevisionFlatCooker(BaseVaultCooker): | ||||
"""Cooker to create a directory bundle """ | """Cooker to create a directory bundle """ | ||||
CACHE_TYPE_KEY = 'revision_flat' | CACHE_TYPE_KEY = 'revision_flat' | ||||
def check_exists(self): | def check_exists(self): | ||||
return not list(self.storage.revision_missing([self.obj_id])) | return not list(self.storage.revision_missing([self.obj_id])) | ||||
def prepare_bundle(self): | def prepare_bundle(self): | ||||
"""Cook the requested revision into a Bundle | """Cook the requested revision into a Bundle | ||||
Returns: | Returns: | ||||
bytes that correspond to the bundle | bytes that correspond to the bundle | ||||
""" | """ | ||||
directory_builder = DirectoryBuilder(self.storage) | directory_builder = DirectoryBuilder(self.storage) | ||||
with tempfile.TemporaryDirectory(suffix='.cook') as root_tmp: | with tempfile.TemporaryDirectory(suffix='.cook') as root_tmp: | ||||
root = Path(root_tmp) | root = Path(root_tmp) | ||||
for revision in self.storage.revision_log([self.obj_id]): | for revision in self.storage.revision_log([self.obj_id]): | ||||
revdir = root / hashutil.hash_to_hex(revision['id']) | revdir = root / hashutil.hash_to_hex(revision['id']) | ||||
revdir.mkdir() | revdir.mkdir() | ||||
directory_builder.build_directory(revision['directory'], | directory_builder.build_directory(revision['directory'], | ||||
str(revdir).encode()) | str(revdir).encode()) | ||||
# FIXME: stream the bytes! this tarball can be HUUUUUGE | tar = tarfile.open(fileobj=self.fileobj, mode='w') | ||||
yield get_tar_bytes(root_tmp, hashutil.hash_to_hex(self.obj_id), | tar.add(root_tmp, arcname=hashutil.hash_to_hex(self.obj_id)) | ||||
self.max_bundle_size) |