diff --git a/bazel/labgrid/runner/runner.py b/bazel/labgrid/runner/runner.py index db8096ff0f1fe333014b7f87782780be3a62ba25..b0365bccbebc8c87fec2141418a3fd07b966190c 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) @@ -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 @@ -94,7 +93,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. diff --git a/labgrid/run/__main__.py b/labgrid/run/__main__.py index 621254e3e65b39490b4b9c3674930ee803bf1c5f..ae8eab7bf5bcd5f44d2b7630fcfbc01076c81710 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 e7a04dbcb3a3974f1429e2208a1ed718b7041ff2..c5b242f0a4921fd8d3fd8b7f8f803527f17bcfd8 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 c89651def5c467f282d302aee293e8f07fbb0b99..d8ad436f7e138058a378ea1e61f6e7f981442ac1 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("--")