[PATCH v3 1/2] binman: bintool: add extra_env parameter to run_cmd

Sergio Prado sergio.prado at e-labworks.com
Thu Apr 23 17:45:35 CEST 2026


Add an optional extra_env dict parameter to run_cmd() and
run_cmd_result(). When provided, the key/value pairs are merged into
the subprocess environment for that invocation only, leaving the parent
process environment unchanged.

This might be needed by bintools that must inject environment variables
into specific tool calls without polluting the global environment.

Signed-off-by: Sergio Prado <sergio.prado at e-labworks.com>
---
 tools/binman/bintool.py | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/tools/binman/bintool.py b/tools/binman/bintool.py
index 9c76c8881a46..9d4f31b8afa1 100644
--- a/tools/binman/bintool.py
+++ b/tools/binman/bintool.py
@@ -272,7 +272,8 @@ class Bintool:
                 show_status(col.RED, 'Failures', status[FAIL])
         return not status[FAIL]
 
-    def run_cmd_result(self, *args, binary=False, raise_on_error=True):
+    def run_cmd_result(self, *args, binary=False, raise_on_error=True,
+                       extra_env=None):
         """Run the bintool using command-line arguments
 
         Args:
@@ -281,6 +282,8 @@ class Bintool:
             binary (bool): True to return output as bytes instead of str
             raise_on_error (bool): True to raise a ValueError exception if the
                 tool returns a non-zero return code
+            extra_env (dict): Extra environment variables to set for this
+                invocation, or None
 
         Returns:
             CommandResult: Resulting output from the bintool, or None if the
@@ -291,6 +294,10 @@ class Bintool:
         name = os.path.expanduser(self.name)  # Expand paths containing ~
         all_args = (name,) + args
         env = tools.get_env_with_path()
+        if extra_env:
+            if env is None:
+                env = dict(os.environ)
+            env.update(extra_env)
         tout.debug(f"bintool: {' '.join(all_args)}")
         result = command.run_pipe(
             [all_args], capture=True, capture_stderr=True, env=env,
@@ -315,18 +322,20 @@ class Bintool:
             tout.debug(result.stderr)
         return result
 
-    def run_cmd(self, *args, binary=False):
+    def run_cmd(self, *args, binary=False, extra_env=None):
         """Run the bintool using command-line arguments
 
         Args:
             args (list of str): Arguments to provide, in addition to the bintool
                 name
             binary (bool): True to return output as bytes instead of str
+            extra_env (dict): Extra environment variables to set for this
+                invocation, or None
 
         Returns:
             str or bytes: Resulting stdout from the bintool
         """
-        result = self.run_cmd_result(*args, binary=binary)
+        result = self.run_cmd_result(*args, binary=binary, extra_env=extra_env)
         if result:
             return result.stdout
 
-- 
2.34.1



More information about the U-Boot mailing list