From fa471ad0f21bce6020a5e7ff37f7cd3c8354c58e Mon Sep 17 00:00:00 2001 From: Luke Hackwell Date: Thu, 24 Apr 2025 14:36:33 +0100 Subject: [PATCH 1/3] fix(run): directory structure on device matches external Bazel rules --- labgrid/run/__main__.py | 9 ++++++++- labgrid/run/run.py | 8 +++++--- labgrid/test/rule.bzl | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/labgrid/run/__main__.py b/labgrid/run/__main__.py index 621254e3..ae8eab7b 100644 --- a/labgrid/run/__main__.py +++ b/labgrid/run/__main__.py @@ -3,7 +3,7 @@ from __future__ import annotations from argparse import REMAINDER, ArgumentParser, ArgumentTypeError from os import environ, getenv -from pathlib import Path +from pathlib import Path, PurePath from string import Template from subprocess import CalledProcessError from sys import argv, stderr @@ -24,6 +24,12 @@ def arguments(prsr: ArgumentParser) -> None: prsr.add_argument( "arguments", metavar="ARG", nargs=REMAINDER, help="Command to run over SSH." ) + prsr.add_argument( + "--program-prefix", + type=PurePath, + default=PurePath(""), + help="Prefix of the path to the program on the device.", + ) prsr.add_argument( "--env", help=( @@ -108,6 +114,7 @@ def main(exe: Path, *args: str) -> int: return run( parsed.program, *parsed.arguments, + program_prefix=parsed.program_prefix, downloads=parsed.downloads, uploads=parsed.uploads, env=dict(parsed.env), diff --git a/labgrid/run/run.py b/labgrid/run/run.py index e7a04dbc..c5b242f0 100644 --- a/labgrid/run/run.py +++ b/labgrid/run/run.py @@ -1,6 +1,6 @@ from __future__ import annotations -from pathlib import Path +from pathlib import Path, PurePath from shlex import join from typing import ( Iterator, @@ -13,13 +13,15 @@ from bazel.labgrid.runner import FileTransfer, runner def run( program: Path, *arguments: str, + program_prefix: PurePath, downloads: Iterator[FileTransfer], uploads: Iterator[FileTransfer], env: Mapping[str, str], ) -> int: with runner() as r: - uploads += [FileTransfer(program.name, program, include_runfiles=True)] - cmd = join((f"./{program.name}", *arguments)) + program_remote = program_prefix / program.name + uploads += [FileTransfer(program_remote, program, include_runfiles=True)] + cmd = join((f"./{program_remote}", *arguments)) r.put(uploads) code = r.run(cmd, env) diff --git a/labgrid/test/rule.bzl b/labgrid/test/rule.bzl index c89651de..d8ad436f 100644 --- a/labgrid/test/rule.bzl +++ b/labgrid/test/rule.bzl @@ -53,7 +53,7 @@ def _run_env(target): def _expand(ctx, data, value): custom_var = { - "RUNFILES_DIR": "{}.runfiles".format(ctx.executable.src.basename), + "RUNFILES_DIR": "{}.runfiles".format(ctx.executable.src.short_path), } value = value.replace("$(location", paths.join("$(RUNFILES_DIR)", "$(rlocationpath")) value = ctx.expand_location(value, targets = data) @@ -86,6 +86,7 @@ def implementation(ctx): args = ctx.actions.args() args.add("--") args.add(to_rlocation_path(ctx, ctx.executable._run)) + args.add_all(["--program-prefix", paths.dirname(ctx.executable.src.short_path)]) args.add_all(env.items(), map_each = _env) args.add_all(get.items(), map_each = _get) args.add("--") -- GitLab From 2c0c1d7a7c0b7b70b9cacd0f575f24befa4aac8a Mon Sep 17 00:00:00 2001 From: Luke Hackwell Date: Thu, 24 Apr 2025 16:18:18 +0100 Subject: [PATCH 2/3] fix(runner): check runfiles exist before transfer --- bazel/labgrid/runner/runner.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bazel/labgrid/runner/runner.py b/bazel/labgrid/runner/runner.py index db8096ff..78279e81 100644 --- a/bazel/labgrid/runner/runner.py +++ b/bazel/labgrid/runner/runner.py @@ -25,14 +25,14 @@ from .tools import Tools, Tool @dataclass(frozen=True) class FileTransfer: remote: PurePath - local: PurePath + local: Path optional: bool # FIXME: Decide whether or not to transfer runfiles implicitly instead of using a flag include_runfiles: bool def __init__(self, remote: str, local: str, optional=False, include_runfiles=False): object.__setattr__(self, "remote", PurePath(remote)) - object.__setattr__(self, "local", PurePath(local)) + object.__setattr__(self, "local", Path(local)) object.__setattr__(self, "optional", optional) object.__setattr__(self, "include_runfiles", include_runfiles) @@ -94,7 +94,8 @@ class Runner: if upload.include_runfiles: src_runfiles = local.with_suffix(".runfiles") dest_runfiles = remote.with_suffix(".runfiles") - self._transfer.put(f"{src_runfiles}", f"{dest_runfiles}") + if src_runfiles.exists(): + self._transfer.put(f"{src_runfiles}", f"{dest_runfiles}") def get(self, downloads: Iterator[FileTransfer], code: int): """Files to transfer from the device to the host. -- GitLab From 13fde9b0cb2c4559ef5360fe464821ce3f296f69 Mon Sep 17 00:00:00 2001 From: Luke Hackwell Date: Thu, 24 Apr 2025 16:18:59 +0100 Subject: [PATCH 3/3] refactor(runner): remove unused variable --- bazel/labgrid/runner/runner.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bazel/labgrid/runner/runner.py b/bazel/labgrid/runner/runner.py index 78279e81..b0365bcc 100644 --- a/bazel/labgrid/runner/runner.py +++ b/bazel/labgrid/runner/runner.py @@ -72,7 +72,6 @@ class Runner: self._transfer = transfer self._shell = shell self._exec_root = None - self._runfiles_dir = environ.get("RUNFILES_DIR") self._paths_to_remove_on_exit = [] self._tools = None -- GitLab