Skip to content
Snippets Groups Projects
Commit cb44803e authored by Jürg Billeter's avatar Jürg Billeter
Browse files

buildelement.py: Support batching for integration and build commands

parent a60b49b4
No related branches found
No related tags found
No related merge requests found
......@@ -127,8 +127,9 @@ artifact collection purposes.
"""
import os
from . import Element, Scope, ElementError
from . import Element, Scope
from . import SandboxFlags
from . import utils
# This list is preserved because of an unfortunate situation, we
......@@ -207,6 +208,9 @@ class BuildElement(Element):
# Setup environment
sandbox.set_environment(self.get_environment())
# Active sandbox batch context manager
self.__sandbox_batch_cm = None # pylint: disable=attribute-defined-outside-init
def stage(self, sandbox):
# Stage deps in the sandbox root
......@@ -215,7 +219,7 @@ class BuildElement(Element):
# Run any integration commands provided by the dependencies
# once they are all staged and ready
with self.timed_activity("Integrating sandbox"):
with sandbox.batch(0, label="Integrating sandbox"):
for dep in self.dependencies(Scope.BUILD):
dep.integrate(sandbox)
......@@ -223,16 +227,24 @@ class BuildElement(Element):
self.stage_sources(sandbox, self.get_variable('build-root'))
def assemble(self, sandbox):
# Use the batch context manager from prepare(), if available
batch_cm = self.__sandbox_batch_cm
self.__sandbox_batch_cm = None # pylint: disable=attribute-defined-outside-init
# Run commands
for command_name in _command_steps:
commands = self.__commands[command_name]
if not commands or command_name == 'configure-commands':
continue
if not batch_cm:
batch_cm = sandbox.batch(SandboxFlags.ROOT_READ_ONLY,
collect=self.get_variable('install-root'))
with batch_cm:
# Run commands
for command_name in _command_steps:
commands = self.__commands[command_name]
if not commands or command_name == 'configure-commands':
continue
with self.timed_activity("Running {}".format(command_name)):
for cmd in commands:
self.__run_command(sandbox, cmd, command_name)
with sandbox.batch(SandboxFlags.ROOT_READ_ONLY, label="Running {}".format(command_name)):
for cmd in commands:
self.__run_command(sandbox, cmd, command_name)
# %{install-root}/%{build-root} should normally not be written
# to - if an element later attempts to stage to a location
......@@ -252,11 +264,20 @@ class BuildElement(Element):
return self.get_variable('install-root')
def prepare(self, sandbox):
commands = self.__commands['configure-commands']
if commands:
with self.timed_activity("Running configure-commands"):
for cmd in commands:
self.__run_command(sandbox, cmd, 'configure-commands')
batch_cm = sandbox.batch(SandboxFlags.ROOT_READ_ONLY,
collect=self.get_variable('install-root'))
# Allow use of single batch context manager across prepare and assemble
batch_cm = utils._SplitContextManager(batch_cm)
with batch_cm:
commands = self.__commands['configure-commands']
if commands:
with sandbox.batch(SandboxFlags.ROOT_READ_ONLY, label="Running configure-commands"):
for cmd in commands:
self.__run_command(sandbox, cmd, 'configure-commands')
self.__sandbox_batch_cm = batch_cm # pylint: disable=attribute-defined-outside-init
def generate_script(self):
script = ""
......@@ -282,13 +303,9 @@ class BuildElement(Element):
return commands
def __run_command(self, sandbox, cmd, cmd_name):
self.status("Running {}".format(cmd_name), detail=cmd)
# Note the -e switch to 'sh' means to exit with an error
# if any untested command fails.
#
exitcode = sandbox.run(['sh', '-c', '-e', cmd + '\n'],
SandboxFlags.ROOT_READ_ONLY)
if exitcode != 0:
raise ElementError("Command '{}' failed with exitcode {}".format(cmd, exitcode),
collect=self.get_variable('install-root'))
sandbox.run(['sh', '-c', '-e', cmd + '\n'],
SandboxFlags.ROOT_READ_ONLY,
label=cmd)
......@@ -58,5 +58,5 @@ def test_sandbox_bwrap_return_subprocess(cli, tmpdir, datafiles):
})
result = cli.run(project=project, args=['build', element_name])
result.assert_task_error(error_domain=ErrorDomain.ELEMENT, error_reason=None)
result.assert_task_error(error_domain=ErrorDomain.SANDBOX, error_reason="command-failed")
assert "sandbox-bwrap/command-exit-42.bst|Command 'exit 42' failed with exitcode 42" in result.stderr
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment