Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/cookers/revision_flat.py
# Copyright (C) 2016-2019 The Software Heritage developers | # Copyright (C) 2016-2019 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 pathlib import Path | from pathlib import Path | ||||
import tarfile | import tarfile | ||||
import tempfile | import tempfile | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.model.identifiers import ObjectType | |||||
from swh.vault.cookers.base import BaseVaultCooker | from swh.vault.cookers.base import BaseVaultCooker | ||||
from swh.vault.cookers.utils import revision_log | from swh.vault.cookers.utils import revision_log | ||||
from swh.vault.to_disk import DirectoryBuilder | from swh.vault.to_disk import DirectoryBuilder | ||||
class RevisionFlatCooker(BaseVaultCooker): | class RevisionFlatCooker(BaseVaultCooker): | ||||
"""Cooker to create a revision_flat bundle """ | """Cooker to create a revision_flat bundle """ | ||||
CACHE_TYPE_KEY = "revision_flat" | BUNDLE_TYPE = "flat" | ||||
SUPPORTED_OBJECT_TYPES = {ObjectType.REVISION} | |||||
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.swhid.object_id])) | ||||
def prepare_bundle(self): | def prepare_bundle(self): | ||||
with tempfile.TemporaryDirectory(prefix="tmp-vault-revision-") as td: | with tempfile.TemporaryDirectory(prefix="tmp-vault-revision-") as td: | ||||
root = Path(td) | root = Path(td) | ||||
for revision in revision_log(self.storage, self.obj_id): | for revision in revision_log(self.storage, self.swhid.object_id): | ||||
revdir = root / hashutil.hash_to_hex(revision["id"]) | revdir = root / hashutil.hash_to_hex(revision["id"]) | ||||
revdir.mkdir() | revdir.mkdir() | ||||
directory_builder = DirectoryBuilder( | directory_builder = DirectoryBuilder( | ||||
self.storage, str(revdir).encode(), revision["directory"] | self.storage, str(revdir).encode(), revision["directory"] | ||||
) | ) | ||||
directory_builder.build() | directory_builder.build() | ||||
with tarfile.open(fileobj=self.fileobj, mode="w:gz") as tar: | with tarfile.open(fileobj=self.fileobj, mode="w:gz") as tar: | ||||
tar.add(td, arcname=hashutil.hash_to_hex(self.obj_id)) | tar.add(td, arcname=self.swhid) |