diff --git a/bazel/labgrid/driver/BUILD.bazel b/bazel/labgrid/driver/BUILD.bazel index bb93750bc1addcf4ecb672c79b9d16a0b4f7b3e6..d65f93e728de31caf00a0b2a016dd0a54f58269c 100644 --- a/bazel/labgrid/driver/BUILD.bazel +++ b/bazel/labgrid/driver/BUILD.bazel @@ -8,6 +8,7 @@ py_library( ], visibility = ["//visibility:public"], deps = [ + "//bazel/python/runfiles", "//labgrid:pkg", ], ) diff --git a/bazel/labgrid/driver/localhost.py b/bazel/labgrid/driver/localhost.py index 7bc672dfa1653997f2c72ae29195acad473f6c40..ed74c388eadd6e0d07737c43ec8a96326ea0e60f 100644 --- a/bazel/labgrid/driver/localhost.py +++ b/bazel/labgrid/driver/localhost.py @@ -1,10 +1,11 @@ import attr -from os import linesep +from os import environ, linesep from shutil import copy, copytree from subprocess import run, PIPE from pathlib import Path +from bazel.python.runfiles import exclude_runfile_env_vars from labgrid.factory import target_factory from labgrid.step import step from labgrid.driver import Driver @@ -26,8 +27,15 @@ class LocalhostDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtoco return self._run(cmd, timeout=timeout, codec=codec, decodeerrors=decodeerrors) def _run(self, cmd, *, timeout=30.0, codec="utf-8", decodeerrors="strict"): + env = exclude_runfile_env_vars(environ) process = run( - cmd, stdout=PIPE, stderr=PIPE, shell=True, timeout=timeout, encoding=codec + cmd, + stdout=PIPE, + stderr=PIPE, + env=env, + shell=True, + timeout=timeout, + encoding=codec, ) stdout = process.stdout.rstrip().split(linesep) stderr = process.stderr.rstrip().split(linesep) diff --git a/bazel/labgrid/runner/runner.py b/bazel/labgrid/runner/runner.py index a2f88d36ce822cf23c97d61cc39a56c09041d4db..b92b93a1f34ae4c53965945d6fdefbc695a22e09 100644 --- a/bazel/labgrid/runner/runner.py +++ b/bazel/labgrid/runner/runner.py @@ -59,15 +59,6 @@ def runner(): log_level = _log_level(environ.get("LOG_LEVEL", "ERROR")) tools = _Tools() - runfiles_dir = None - # Allow any Bazel binaries used in the configuration to work - try: - # FIXME: Convert into context manager - runfiles_dir = environ["RUNFILES_DIR"] - del environ["RUNFILES_DIR"] - except KeyError: - pass - # Activating the StepLogger requires a DEBUG level of verbosity basicConfig(level=log_level) if log_level <= logging.DEBUG: @@ -87,7 +78,7 @@ def runner(): tools.mkdir, tools.mv, ) as temp: - r = Runner(temp, transfer, shell, runfiles_dir) + r = Runner(temp, transfer, shell) # Transfer 'env' binary over to the device r.put([FileTransfer(r.exec_path / tools.env.name, tools.env)]) @@ -96,11 +87,11 @@ def runner(): class Runner: - def __init__(self, exec_path, transfer, shell, runfiles_dir): + def __init__(self, exec_path, transfer, shell): self.exec_path = exec_path self.transfer = transfer self.shell = shell - self.runfiles_dir = runfiles_dir + self.runfiles_dir = environ.get("RUNFILES_DIR") def put(self, uploads: Iterator[FileTransfer]): for upload in uploads: diff --git a/bazel/labgrid/strategy/BUILD.bazel b/bazel/labgrid/strategy/BUILD.bazel index c5c1180fec23a7a974ac4e97588ad322d1c2b2ae..50801afa4289854de8f9b266be48ee7287cfb9b5 100644 --- a/bazel/labgrid/strategy/BUILD.bazel +++ b/bazel/labgrid/strategy/BUILD.bazel @@ -12,6 +12,7 @@ py_library( visibility = ["//visibility:public"], deps = [ "//bazel/labgrid/driver", + "//bazel/python/runfiles", "//labgrid:pkg", ], ) diff --git a/bazel/labgrid/strategy/transition.py b/bazel/labgrid/strategy/transition.py index e81531833b10c6819c42dfcaff657f5f40ab7b09..57f61a3e25d463cd0fe0f691140599cbdcb2b922 100644 --- a/bazel/labgrid/strategy/transition.py +++ b/bazel/labgrid/strategy/transition.py @@ -2,6 +2,7 @@ from contextlib import contextmanager from dataclasses import dataclass from os import environ +from bazel.python.runfiles import unset_runfile_env_vars from labgrid.strategy import Strategy @@ -23,10 +24,12 @@ class State: @contextmanager def transition(strategy: Strategy, state: State): try: - if state.initial is not None: - strategy.force(state.initial) - strategy.transition(state.desired) + with unset_runfile_env_vars(): + if state.initial is not None: + strategy.force(state.initial) + strategy.transition(state.desired) yield finally: if state.final is not None: - strategy.transition(state.final) + with unset_runfile_env_vars(): + strategy.transition(state.final) diff --git a/bazel/python/runfiles/__init__.py b/bazel/python/runfiles/__init__.py index 724ec35947baf337e07cd391898c84d2ce614e09..498271e46d540cf85f3e3c8dc70fc186b8b2cd51 100644 --- a/bazel/python/runfiles/__init__.py +++ b/bazel/python/runfiles/__init__.py @@ -1,3 +1,13 @@ -from .runfiles import runfile, RunfileNotFoundError +from .runfiles import ( + runfile, + RunfileNotFoundError, + exclude_runfile_env_vars, + unset_runfile_env_vars, +) -__all__ = ["runfile", "RunfileNotFoundError"] +__all__ = [ + "runfile", + "RunfileNotFoundError", + "exclude_runfile_env_vars", + "unset_runfile_env_vars", +] diff --git a/bazel/python/runfiles/runfiles.py b/bazel/python/runfiles/runfiles.py index 3ebbb9a6de625d2c4441198f53721cb2768b51d1..e2e54f527bc55055d359e2f3a6bac71c1c8913aa 100644 --- a/bazel/python/runfiles/runfiles.py +++ b/bazel/python/runfiles/runfiles.py @@ -1,7 +1,12 @@ +from contextlib import contextmanager +from os import environ from pathlib import Path +from typing import Dict from python.runfiles import Runfiles +ENV_VARS = ["RUNFILES_DIR", "RUNFILES_MANIFEST_FILE"] + class RunfileNotFoundError(FileNotFoundError): pass @@ -13,3 +18,17 @@ def runfile(path: Path) -> Path: if resolved and Path(resolved).exists(): return Path(resolved) raise RunfileNotFoundError(path) + + +def exclude_runfile_env_vars(env: Dict[str, str]) -> Dict[str, str]: + return {k: v for k, v in env.items() if k not in ENV_VARS} + + +@contextmanager +def unset_runfile_env_vars(): + captured = {k: v for k, v in environ.items() if k in ENV_VARS} + for k in captured.keys(): + del environ[k] + yield + for k, v in captured.items(): + environ[k] = v diff --git a/labgrid/executor/executor.py b/labgrid/executor/executor.py index a614e623748fe8a64c1bbce4523287350a82695a..63cbdd79debe468701f1eb8ec6dcb3916cd33b31 100644 --- a/labgrid/executor/executor.py +++ b/labgrid/executor/executor.py @@ -13,7 +13,11 @@ from subprocess import CalledProcessError, run from sys import argv from bazel.labgrid.manager import Data, Manager -from bazel.python.runfiles import runfile, RunfileNotFoundError +from bazel.python.runfiles import ( + exclude_runfile_env_vars, + runfile, + RunfileNotFoundError, +) def resolve(value: str) -> str: @@ -96,7 +100,7 @@ def main(exe: Path, *args: str) -> int: if machine() == "arm64" and system() == "Darwin": cmd = (which("sh"), *cmd) - data = Data(env={k: v for k, v in environ.items() if k != "RUNFILES_DIR"}) + data = Data(env=exclude_runfile_env_vars(environ)) with ExitStack() as stack: for manager in parsed.managers: data = stack.enter_context(manager(data))