From 54b5c83db20b155181069ddc5969323a03347120 Mon Sep 17 00:00:00 2001 From: Sebastian Birunt Date: Thu, 8 May 2025 09:27:48 +0200 Subject: [PATCH] feat(runner): check non-zero exit code `Runner.run()` provides `check=False` argument. If `check` is true, and the process exits with a non-zero exit code, a `CalledProcessError` exception will be raised. --- bazel/labgrid/runner/runner.py | 4 ++-- examples/custom-runners/archive-transfer/run.py | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/bazel/labgrid/runner/runner.py b/bazel/labgrid/runner/runner.py index 0a15d9f..3f9754c 100644 --- a/bazel/labgrid/runner/runner.py +++ b/bazel/labgrid/runner/runner.py @@ -112,12 +112,12 @@ class Runner: if not download.optional and code == 0: raise e - def run(self, cmd: str, env: Mapping[str, str] = {}) -> int: + def run(self, cmd: str, env: Mapping[str, str] = {}, check=False) -> int: """Run a command on the device with given environment variables.""" cmd = ( f"cd {self._exec_root} && {self._tools.env(self._default_env() | env, cmd)}" ) - out, err, code = self._run(cmd, check=False) + out, err, code = self._run(cmd, check=check) for line in out: stdout.write(f"{line}{linesep}") for line in err: diff --git a/examples/custom-runners/archive-transfer/run.py b/examples/custom-runners/archive-transfer/run.py index 552256d..b7b92ce 100644 --- a/examples/custom-runners/archive-transfer/run.py +++ b/examples/custom-runners/archive-transfer/run.py @@ -32,12 +32,11 @@ def main(): uploads = [archive, unzip] r.put(uploads) + r.run(f"{unzip.remote} {archive.remote}", check=True) - code, _ = r.run(f"{unzip.remote} {archive.remote}") - if code != 0: - raise Exception(code) - - code, stdout = r.run(f"{args.program.name} {''.join(args.arguments)}") + code, stdout = r.run( + f"{args.program.name} {''.join(args.arguments)}", check=True + ) with args.out.open("w") as f: print(stdout, file=f) -- GitLab