diff options
author | Jürg Billeter <j@bitron.ch> | 2020-01-22 14:17:30 +0100 |
---|---|---|
committer | Jürg Billeter <j@bitron.ch> | 2020-02-11 21:08:59 +0100 |
commit | 49c9e58af6b4980628485636d813699e4e7e3ffb (patch) | |
tree | fc2d3f9403d3547cc69065d8be30f5a26d50de7d | |
parent | 6cd3dadf3c1d8f14a867c4444af6d2f15019ae18 (diff) | |
download | buildstream-49c9e58af6b4980628485636d813699e4e7e3ffb.tar.gz |
sandbox: Add _create_empty_file() method
-rw-r--r-- | src/buildstream/sandbox/_sandboxreapi.py | 3 | ||||
-rw-r--r-- | src/buildstream/sandbox/sandbox.py | 46 |
2 files changed, 48 insertions, 1 deletions
diff --git a/src/buildstream/sandbox/_sandboxreapi.py b/src/buildstream/sandbox/_sandboxreapi.py index 5a3cd5e76..ee7fc72ae 100644 --- a/src/buildstream/sandbox/_sandboxreapi.py +++ b/src/buildstream/sandbox/_sandboxreapi.py @@ -227,3 +227,6 @@ class _SandboxREAPIBatch(_SandboxBatch): label = command.label or cmdline quoted_label = shlex.quote("'{}'".format(label)) self.script += " || (echo Command {} failed with exitcode $? >&2 ; exit 1)\n".format(quoted_label) + + def create_empty_file(self, name): + self.script += "touch -- {}\n".format(shlex.quote(name)) diff --git a/src/buildstream/sandbox/sandbox.py b/src/buildstream/sandbox/sandbox.py index 31eb6eb11..b82e2da59 100644 --- a/src/buildstream/sandbox/sandbox.py +++ b/src/buildstream/sandbox/sandbox.py @@ -580,6 +580,30 @@ class Sandbox: return False + # _create_empty_file() + # + # Creates an empty file in the current working directory. + # + # If this is called outside a batch context, the file is created + # immediately. + # + # If this is called in a batch context, creating the file is deferred. + # + # Args: + # path (str): The path of the file to be created + # + def _create_empty_file(self, name): + if self.__batch: + batch_file = _SandboxBatchFile(name) + + current_group = self.__batch.current_group + current_group.append(batch_file) + else: + vdir = self.get_virtual_directory() + cwd = self._get_work_directory() + cwd_vdir = vdir.descend(*cwd.lstrip(os.sep).split(os.sep), create=True) + cwd_vdir._create_empty_file(name) + # _get_element_name() # # Get the plugin's element full name @@ -655,6 +679,12 @@ class _SandboxBatch: "Command failed with exitcode {}".format(exitcode), detail=label, collect=self.collect ) + def create_empty_file(self, name): + vdir = self.sandbox.get_virtual_directory() + cwd = self.sandbox._get_work_directory() + cwd_vdir = vdir.descend(*cwd.lstrip(os.sep).split(os.sep), create=True) + cwd_vdir._create_empty_file(name) + # _SandboxBatchItem() # @@ -705,4 +735,18 @@ class _SandboxBatchGroup(_SandboxBatchItem): item.execute(batch) def combined_label(self): - return "\n".join(item.combined_label() for item in self.children) + return "\n".join(filter(None, (item.combined_label() for item in self.children))) + + +# _SandboxBatchFile() +# +# A file creation item in a command batch. +# +class _SandboxBatchFile(_SandboxBatchItem): + def __init__(self, name): + super().__init__() + + self.name = name + + def execute(self, batch): + batch.create_empty_file(self.name) |