From a589bb1dd094a80d375e7b7045f470f3da790e3c Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Thu, 31 Oct 2024 10:39:40 +0000 Subject: [PATCH 1/5] chore: remove unused imports --- labgrid/executor/executor.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/labgrid/executor/executor.py b/labgrid/executor/executor.py index 49d5e075..2e0ab9a2 100644 --- a/labgrid/executor/executor.py +++ b/labgrid/executor/executor.py @@ -3,15 +3,14 @@ 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 -- GitLab From 017eef537160b0eb5481cef5a6fbd30abfc7ab69 Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Thu, 31 Oct 2024 11:35:55 +0000 Subject: [PATCH 2/5] refactor(executor): reuse runfile loading logic --- labgrid/executor/executor.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/labgrid/executor/executor.py b/labgrid/executor/executor.py index 2e0ab9a2..7ed720e4 100644 --- a/labgrid/executor/executor.py +++ b/labgrid/executor/executor.py @@ -17,6 +17,25 @@ 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: @@ -71,8 +90,7 @@ def main(exe: Path, *args: str) -> int: arguments(prsr) - runfiles = Runfiles.Create() - baked = Path(runfiles.Rlocation("rules_labgrid/labgrid/executor/args")) + baked = runfile("rules_labgrid/labgrid/executor/args") args = (f"@{baked}", *args) parsed = prsr.parse_args(args) @@ -88,7 +106,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: @@ -101,14 +119,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:])) -- GitLab From f96ab7bfc945bb14953472d4ed924402357ec94c Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Thu, 31 Oct 2024 11:39:17 +0000 Subject: [PATCH 3/5] feat(executor): load extra args from root-symlinked file Consumers who want to wrap the `//labgrid/executor` as an executable rule can now root-symlink an `extra.args` file with args to be passed to the executor. --- labgrid/executor/executor.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/labgrid/executor/executor.py b/labgrid/executor/executor.py index 7ed720e4..3353b162 100644 --- a/labgrid/executor/executor.py +++ b/labgrid/executor/executor.py @@ -90,8 +90,15 @@ def main(exe: Path, *args: str) -> int: arguments(prsr) - baked = runfile("rules_labgrid/labgrid/executor/args") - args = (f"@{baked}", *args) + arg_files = [] + for path in ["rules_labgrid/labgrid/executor/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) -- GitLab From 3777b37bacc847f90214d3be76f7ade1f72f7f28 Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Mon, 4 Nov 2024 10:50:44 +0000 Subject: [PATCH 4/5] refactor(executor): rename `args` to `default.args` --- labgrid/executor/BUILD.bazel | 4 ++-- labgrid/executor/executor.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/labgrid/executor/BUILD.bazel b/labgrid/executor/BUILD.bazel index 3194c481..18f84038 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", ], diff --git a/labgrid/executor/executor.py b/labgrid/executor/executor.py index 3353b162..5e68394a 100644 --- a/labgrid/executor/executor.py +++ b/labgrid/executor/executor.py @@ -91,7 +91,7 @@ def main(exe: Path, *args: str) -> int: arguments(prsr) arg_files = [] - for path in ["rules_labgrid/labgrid/executor/args", "extra.args"]: + for path in ["rules_labgrid/labgrid/executor/default.args", "extra.args"]: try: baked = runfile(path) except RunfileNotFoundError: -- GitLab From 1e823554508a18c69fb47dd6aa6f32aacd9c7204 Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Tue, 5 Nov 2024 14:03:34 +0000 Subject: [PATCH 5/5] test(executor): remove unused data --- labgrid/executor/BUILD.bazel | 6 ------ 1 file changed, 6 deletions(-) diff --git a/labgrid/executor/BUILD.bazel b/labgrid/executor/BUILD.bazel index 18f84038..8dae9ea7 100644 --- a/labgrid/executor/BUILD.bazel +++ b/labgrid/executor/BUILD.bazel @@ -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 = [ -- GitLab