From 3b44a7785e22ee82dee28533b1e825c26491b2b3 Mon Sep 17 00:00:00 2001 From: Raoul Hidalgo Charman Date: Wed, 1 May 2019 15:41:38 +0100 Subject: Directory: add `import_single_file` method This a new method which deals with importing a single file. Implemented for both FileBasedDirectory and CasBasedDirectory. Part of #983 --- src/buildstream/storage/_casbaseddirectory.py | 12 ++++++++++++ src/buildstream/storage/_filebaseddirectory.py | 10 ++++++++++ src/buildstream/storage/directory.py | 4 ++++ 3 files changed, 26 insertions(+) diff --git a/src/buildstream/storage/_casbaseddirectory.py b/src/buildstream/storage/_casbaseddirectory.py index 2aff29b98..12ef482dc 100644 --- a/src/buildstream/storage/_casbaseddirectory.py +++ b/src/buildstream/storage/_casbaseddirectory.py @@ -386,6 +386,18 @@ class CasBasedDirectory(Directory): return result + def import_single_file(self, srcpath): + result = FileListResult() + if self._check_replacement(os.path.basename(srcpath), + os.path.dirname(srcpath), + result): + self._add_file(os.path.dirname(srcpath), + os.path.basename(srcpath), + modified=os.path.basename(srcpath) + in result.overwritten) + result.files_written.append(srcpath) + return result + def set_deterministic_mtime(self): """ Sets a static modification time for all regular files in this directory. Since we don't store any modification time, we don't need to do anything. diff --git a/src/buildstream/storage/_filebaseddirectory.py b/src/buildstream/storage/_filebaseddirectory.py index 9a746f731..90911da86 100644 --- a/src/buildstream/storage/_filebaseddirectory.py +++ b/src/buildstream/storage/_filebaseddirectory.py @@ -28,6 +28,7 @@ See also: :ref:`sandboxing`. """ import os +import shutil import stat import time @@ -111,6 +112,15 @@ class FileBasedDirectory(Directory): os.utime(os.path.join(self.external_directory, f), times=(cur_time, cur_time)) return import_result + def import_single_file(self, srcpath): + dstpath = os.path.join(self.external_directory, os.path.basename(srcpath)) + result = FileListResult() + if os.path.exists(dstpath): + result.ignored.append(dstpath) + else: + shutil.copyfile(srcpath, dstpath, follow_symlinks=False) + return result + def _mark_changed(self): pass diff --git a/src/buildstream/storage/directory.py b/src/buildstream/storage/directory.py index bad818fef..c40e1bdb9 100644 --- a/src/buildstream/storage/directory.py +++ b/src/buildstream/storage/directory.py @@ -103,6 +103,10 @@ class Directory(): raise NotImplementedError() + def import_single_file(self, external_pathspec): + """Imports a single file from an external path""" + raise NotImplementedError() + def export_files(self, to_directory, *, can_link=False, can_destroy=False): """Copies everything from this into to_directory. -- cgit v1.2.1