diff options
author | William Salmon <will.salmon@codethink.co.uk> | 2019-05-15 17:21:49 +0100 |
---|---|---|
committer | William Salmon <will.salmon@codethink.co.uk> | 2019-07-25 10:08:18 +0100 |
commit | 43505dfd6da8b6eb3f4d4e544f3cdc0e2a31a947 (patch) | |
tree | 57c2c4e41df4c5c32b776818f83e5458a782b896 /src/buildstream/storage | |
parent | 3e5790d151faaddbeb376eb8cb24178fa9899c17 (diff) | |
download | buildstream-43505dfd6da8b6eb3f4d4e544f3cdc0e2a31a947.tar.gz |
Implmented export_to_tar for casbaseddirectory
Diffstat (limited to 'src/buildstream/storage')
-rw-r--r-- | src/buildstream/storage/_casbaseddirectory.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/src/buildstream/storage/_casbaseddirectory.py b/src/buildstream/storage/_casbaseddirectory.py index ea4862e50..7bd9ceea0 100644 --- a/src/buildstream/storage/_casbaseddirectory.py +++ b/src/buildstream/storage/_casbaseddirectory.py @@ -28,6 +28,9 @@ See also: :ref:`sandboxing`. """ import os +import stat +import tarfile as tarfilelib +from io import StringIO from .._protos.build.bazel.remote.execution.v2 import remote_execution_pb2 from .directory import Directory, VirtualDirectoryError, _FileType @@ -413,7 +416,33 @@ class CasBasedDirectory(Directory): self.cas_cache.checkout(to_directory, self._get_digest(), can_link=can_link) def export_to_tar(self, tarfile, destination_dir, mtime=BST_ARBITRARY_TIMESTAMP): - raise NotImplementedError() + for filename, entry in self.index.items(): + arcname = os.path.join(destination_dir, filename) + if entry.type == _FileType.DIRECTORY: + tarinfo = tarfilelib.TarInfo(arcname) + tarinfo.mtime = mtime + tarinfo.type = tarfilelib.DIRTYPE + tarinfo.mode = 0o755 + tarfile.addfile(tarinfo) + self.descend(filename).export_to_tar(tarfile, arcname, mtime) + elif entry.type == _FileType.REGULAR_FILE: + source_name = self.cas_cache.objpath(entry.digest) + tarinfo = tarfilelib.TarInfo(arcname) + tarinfo.mtime = mtime + tarinfo.mode |= entry.is_executable & stat.S_IXUSR + tarinfo.size = os.path.getsize(source_name) + with open(source_name, "rb") as f: + tarfile.addfile(tarinfo, f) + elif entry.type == _FileType.SYMLINK: + tarinfo = tarfilelib.TarInfo(arcname) + tarinfo.mtime = mtime + tarinfo.mode |= entry.is_executable & stat.S_IXUSR + tarinfo.linkname = entry.target + tarinfo.type = tarfilelib.SYMTYPE + f = StringIO(entry.target) + tarfile.addfile(tarinfo, f) + else: + raise VirtualDirectoryError("can not export file type {} to tar".format(entry.type)) def _mark_changed(self): """ It should not be possible to externally modify a CAS-based |