diff options
author | Jürg Billeter <j@bitron.ch> | 2018-11-15 22:06:42 +0100 |
---|---|---|
committer | Jürg Billeter <j@bitron.ch> | 2018-11-27 13:41:09 +0000 |
commit | 38d16c6208c6cfe4493763f60d259a3623c0ccbf (patch) | |
tree | 2bed5a7d0ef9defcc220af2fe7eba1b3920a0c36 | |
parent | 223c007313b2fcb8c54e6c0183f77a3f72485c13 (diff) | |
download | buildstream-38d16c6208c6cfe4493763f60d259a3623c0ccbf.tar.gz |
sandbox: Deduplicate code to process cwd, env, and command arguments
-rw-r--r-- | buildstream/sandbox/_sandboxbwrap.py | 12 | ||||
-rw-r--r-- | buildstream/sandbox/_sandboxchroot.py | 12 | ||||
-rw-r--r-- | buildstream/sandbox/_sandboxdummy.py | 12 | ||||
-rw-r--r-- | buildstream/sandbox/_sandboxremote.py | 12 | ||||
-rw-r--r-- | buildstream/sandbox/sandbox.py | 35 |
5 files changed, 38 insertions, 45 deletions
diff --git a/buildstream/sandbox/_sandboxbwrap.py b/buildstream/sandbox/_sandboxbwrap.py index 839780f95..f7b11326a 100644 --- a/buildstream/sandbox/_sandboxbwrap.py +++ b/buildstream/sandbox/_sandboxbwrap.py @@ -58,22 +58,12 @@ class SandboxBwrap(Sandbox): self.die_with_parent_available = kwargs['die_with_parent_available'] self.json_status_available = kwargs['json_status_available'] - def run(self, command, flags, *, cwd=None, env=None): + def _run(self, command, flags, *, cwd, env): stdout, stderr = self._get_output() # Allowable access to underlying storage as we're part of the sandbox root_directory = self.get_virtual_directory()._get_underlying_directory() - # Fallback to the sandbox default settings for - # the cwd and env. - # - cwd = self._get_work_directory(cwd=cwd) - env = self._get_environment(cwd=cwd, env=env) - - # Convert single-string argument to a list - if isinstance(command, str): - command = [command] - if not self._has_command(command[0], env): raise SandboxError("Staged artifacts do not provide command " "'{}'".format(command[0]), diff --git a/buildstream/sandbox/_sandboxchroot.py b/buildstream/sandbox/_sandboxchroot.py index e63a4f237..71422476a 100644 --- a/buildstream/sandbox/_sandboxchroot.py +++ b/buildstream/sandbox/_sandboxchroot.py @@ -49,17 +49,7 @@ class SandboxChroot(Sandbox): self.mount_map = None - def run(self, command, flags, *, cwd=None, env=None): - - # Fallback to the sandbox default settings for - # the cwd and env. - # - cwd = self._get_work_directory(cwd=cwd) - env = self._get_environment(cwd=cwd, env=env) - - # Convert single-string argument to a list - if isinstance(command, str): - command = [command] + def _run(self, command, flags, *, cwd, env): if not self._has_command(command[0], env): raise SandboxError("Staged artifacts do not provide command " diff --git a/buildstream/sandbox/_sandboxdummy.py b/buildstream/sandbox/_sandboxdummy.py index 0e3754c1b..4cc3aae9c 100644 --- a/buildstream/sandbox/_sandboxdummy.py +++ b/buildstream/sandbox/_sandboxdummy.py @@ -25,17 +25,7 @@ class SandboxDummy(Sandbox): super().__init__(*args, **kwargs) self._reason = kwargs.get("dummy_reason", "no reason given") - def run(self, command, flags, *, cwd=None, env=None): - - # Fallback to the sandbox default settings for - # the cwd and env. - # - cwd = self._get_work_directory(cwd=cwd) - env = self._get_environment(cwd=cwd, env=env) - - # Convert single-string argument to a list - if isinstance(command, str): - command = [command] + def _run(self, command, flags, *, cwd, env): if not self._has_command(command[0], env): raise SandboxError("Staged artifacts do not provide command " diff --git a/buildstream/sandbox/_sandboxremote.py b/buildstream/sandbox/_sandboxremote.py index 60a053e0c..caf76d163 100644 --- a/buildstream/sandbox/_sandboxremote.py +++ b/buildstream/sandbox/_sandboxremote.py @@ -212,7 +212,7 @@ class SandboxRemote(Sandbox): new_dir = CasBasedDirectory(self._get_context().artifactcache.cas, ref=dir_digest) self._set_virtual_directory(new_dir) - def run(self, command, flags, *, cwd=None, env=None): + def _run(self, command, flags, *, cwd, env): # Upload sources upload_vdir = self.get_virtual_directory() @@ -230,16 +230,6 @@ class SandboxRemote(Sandbox): if not cascache.verify_digest_pushed(self._get_project(), upload_vdir.ref): raise SandboxError("Failed to verify that source has been pushed to the remote artifact cache.") - # Fallback to the sandbox default settings for - # the cwd and env. - # - cwd = self._get_work_directory(cwd=cwd) - env = self._get_environment(cwd=cwd, env=env) - - # We want command args as a list of strings - if isinstance(command, str): - command = [command] - # Now transmit the command to execute operation = self.run_remote_command(command, upload_vdir.ref, cwd, env) diff --git a/buildstream/sandbox/sandbox.py b/buildstream/sandbox/sandbox.py index a76b2d9d2..a7b03f31a 100644 --- a/buildstream/sandbox/sandbox.py +++ b/buildstream/sandbox/sandbox.py @@ -234,7 +234,40 @@ class Sandbox(): function must make sure the directory will be created if it does not exist yet, even if a workspace is being used. """ - raise ImplError("Sandbox of type '{}' does not implement run()" + + # Fallback to the sandbox default settings for + # the cwd and env. + # + cwd = self._get_work_directory(cwd=cwd) + env = self._get_environment(cwd=cwd, env=env) + + # Convert single-string argument to a list + if isinstance(command, str): + command = [command] + + return self._run(command, flags, cwd=cwd, env=env) + + ##################################################### + # Abstract Methods for Sandbox implementations # + ##################################################### + + # _run() + # + # Abstract method for running a single command + # + # Args: + # command (list): The command to run in the sandboxed environment, as a list + # of strings starting with the binary to run. + # flags (:class:`.SandboxFlags`): The flags for running this command. + # cwd (str): The sandbox relative working directory in which to run the command. + # env (dict): A dictionary of string key, value pairs to set as environment + # variables inside the sandbox environment. + # + # Returns: + # (int): The program exit code. + # + def _run(self, command, flags, *, cwd, env): + raise ImplError("Sandbox of type '{}' does not implement _run()" .format(type(self).__name__)) ################################################ |