diff --git a/bazel/labgrid/client/common.py b/bazel/labgrid/client/common.py index 3995fa48478cdfecc227211faa0dad68eab091ba..05f106b9101ca6f8a2f268f60a1f2b331ad4468b 100644 --- a/bazel/labgrid/client/common.py +++ b/bazel/labgrid/client/common.py @@ -27,6 +27,7 @@ def labgrid_client(*args, env=None, timeout=10, output_as_yaml=True, url=""): ) except (CalledProcessError, TimeoutExpired) as e: stderr.write(e.output) + stderr.write(e.stderr) raise if process.stdout: diff --git a/bazel/labgrid/local/BUILD.bazel b/bazel/labgrid/local/BUILD.bazel index 0575aeac3131b46e04c67f95f4f504d5f4cdf3ec..fc69814df37ea050e1b199ce3171d1fa2e95a5c8 100644 --- a/bazel/labgrid/local/BUILD.bazel +++ b/bazel/labgrid/local/BUILD.bazel @@ -1,4 +1,4 @@ -load("@rules_python//python:defs.bzl", "py_library", "py_test") +load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") py_library( name = "local", @@ -23,6 +23,13 @@ py_library( ], ) +py_binary( + name = "harness", + srcs = ["harness.py"], + visibility = ["//visibility:public"], + deps = [":local"], +) + py_test( name = "test", size = "small", diff --git a/bazel/labgrid/local/harness.py b/bazel/labgrid/local/harness.py new file mode 100644 index 0000000000000000000000000000000000000000..4bc0c8386f435fcc39da0db727a90479e971f342 --- /dev/null +++ b/bazel/labgrid/local/harness.py @@ -0,0 +1,44 @@ +from argparse import ArgumentParser +from subprocess import CalledProcessError +from sys import stderr, stdout +from time import sleep + +from bazel.labgrid.local import harness + + +def arguments(prsr: ArgumentParser) -> None: + prsr.add_argument( + "--place", + help="The name of a place to use.", + type=str, + default="test_place", + ) + prsr.add_argument( + "tags", + metavar="KEY=VALUE", + nargs="*", + help="Tags to set on the place.", + default={"fake": "device"}, + ) + + +def main(): + prsr = ArgumentParser() + arguments(prsr) + parsed = prsr.parse_args() + + try: + with harness(parsed.tags, parsed.place) as test_harness: + stdout.write(f"Coordinator URL: {test_harness.coordinator_url}\n") + while True: + sleep(1) + continue + except KeyboardInterrupt: + exit(0) + except CalledProcessError as e: + stderr.write(e.stderr) + exit(1) + + +if __name__ == "__main__": + main() diff --git a/bazel/labgrid/local/local.py b/bazel/labgrid/local/local.py index 8112abb486c0579be2ec5664e9e86b13ccbd6264..11eca192ccf185531dffc3f9b908597fc8ade051 100644 --- a/bazel/labgrid/local/local.py +++ b/bazel/labgrid/local/local.py @@ -4,7 +4,6 @@ import socket from contextlib import contextmanager from dataclasses import dataclass from typing import Iterator, Mapping - from bazel.labgrid.local.coordinator import coordinator from bazel.labgrid.local.exporter import exporter from bazel.labgrid.client.common import labgrid_client @@ -28,17 +27,17 @@ def harness( place: str = "test_place", ) -> Iterator[Harness]: with coordinator() as url: - labgrid_client("--place", place, "create", url=url) - try: - labgrid_client( - "--place", - place, - "set-tags", - *(f"{k}={v}" for k, v in tags.items()), - url=url, - ) - - with exporter(url=url): + with exporter(url=url): + delete_place = True + try: + labgrid_client("--place", place, "create", url=url) + labgrid_client( + "--place", + place, + "set-tags", + *(f"{k}={v}" for k, v in tags.items()), + url=url, + ) # add resources to the "place" labgrid_client( "--place", @@ -52,5 +51,11 @@ def harness( tags=tags, place=place, ) - finally: - labgrid_client("--place", place, "delete", url=url) + except KeyboardInterrupt: + # A keyboard interrupt sends`SIGINT` to subprocesses (including the coordinator), + # so we shouldn't try to communicate with it as it won't be available anymore + delete_place = False + raise + finally: + if delete_place: + labgrid_client("--place", place, "delete", url=url)