diff --git a/labgrid/executor/BUILD.bazel b/labgrid/executor/BUILD.bazel index 3194c4812302de2b91e070bec88c4bec7aca10cc..8dae9ea7f5f78fbf132d06e0732f9a7e281c66dc 100644 --- a/labgrid/executor/BUILD.bazel +++ b/labgrid/executor/BUILD.bazel @@ -2,14 +2,14 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") load(":args.bzl", "args") args( - name = "args", + name = "default.args", ) py_binary( name = "executor", srcs = ["executor.py"], data = [ - ":args", + ":default.args", "//labgrid/config:data", "//labgrid/config:tools", ], @@ -42,9 +42,6 @@ py_test( "echo", "$${DATA}", ], - data = [ - ":args", - ], main = "executor.py", # TODO: remove this when we have a default LabGrid configuration toolchain for `arm64-linux` target_compatible_with = [ @@ -66,9 +63,6 @@ py_test( "echo", "$${DATA}", ], - data = [ - ":args", - ], main = "executor.py", # TODO: remove this when we have a default LabGrid configuration toolchain for `arm64-linux` target_compatible_with = [ diff --git a/labgrid/executor/executor.py b/labgrid/executor/executor.py index 49d5e0754c12bb450ce7d6f2ee57a0c7130a7258..5e68394aff080c23e4d8fbf6680858b2f8d6846f 100644 --- a/labgrid/executor/executor.py +++ b/labgrid/executor/executor.py @@ -3,21 +3,39 @@ from __future__ import annotations from argparse import ArgumentParser, ArgumentTypeError -from contextlib import AbstractContextManager, ExitStack +from contextlib import ExitStack from importlib import import_module -from inspect import signature from os import environ -from pathlib import Path, PurePath +from pathlib import Path from platform import machine, system from shutil import which from subprocess import CalledProcessError, run -from sys import argv, stderr +from sys import argv from python.runfiles import Runfiles from bazel.labgrid.executor.manager import Data, Manager +class RunfileNotFoundError(FileNotFoundError): + pass + + +def runfile(path: Path) -> Path: + runfiles = Runfiles.Create() + resolved = Path(runfiles.Rlocation(path)) + if not resolved.exists(): + raise RunfileNotFoundError(path) + return resolved + + +def resolve(value: str) -> str: + try: + return str(runfile(value)) + except RunfileNotFoundError: + return value + + def load(value: str) -> Manager: name, found, attr = value.partition(":") if not found: @@ -72,9 +90,15 @@ def main(exe: Path, *args: str) -> int: arguments(prsr) - runfiles = Runfiles.Create() - baked = Path(runfiles.Rlocation("rules_labgrid/labgrid/executor/args")) - args = (f"@{baked}", *args) + arg_files = [] + for path in ["rules_labgrid/labgrid/executor/default.args", "extra.args"]: + try: + baked = runfile(path) + except RunfileNotFoundError: + pass + else: + arg_files.append(f"@{baked}") + args = (*arg_files, *args) parsed = prsr.parse_args(args) @@ -89,7 +113,7 @@ def main(exe: Path, *args: str) -> int: with ExitStack() as stack: for manager in parsed.managers: data = stack.enter_context(manager(data)) - env = {k: resolve_runfile(v) for k, v in parsed.env} + env = {k: resolve(v) for k, v in parsed.env} env |= {k: v for k, v in data.env.items()} try: @@ -102,14 +126,6 @@ def main(exe: Path, *args: str) -> int: return 0 -def resolve_runfile(value): - r = Runfiles.Create() - location = r.Rlocation(value) - if Path(location).exists(): - return location - return value - - def entry(): exit(main(Path(argv[0]), *argv[1:]))