diff mbox series

[yocto-autobuilder-helper] utils: Improve buildtools env parsing

Message ID 20260818155519.278450-1-richard.purdie@linuxfoundation.org
State New
Headers show
Series [yocto-autobuilder-helper] utils: Improve buildtools env parsing | expand

Commit Message

Richard Purdie Aug. 18, 2026, 3:55 p.m. UTC
The current env scripts have includes which the current parsing code can't
handle and we miss the variables defined there. Switch to a different method
where source the scripts and print the environment, then parse that back in
instead.

There are quite a few variables which can cause problems which we don't want
to touch so skip over the ones we know don't make sense and the buildtools
environment is unlikely to need/change.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 scripts/utils.py | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/scripts/utils.py b/scripts/utils.py
index 4858d34..ab33734 100644
--- a/scripts/utils.py
+++ b/scripts/utils.py
@@ -460,19 +460,27 @@  def enable_tools_tarball(btdir, name, env_glob="/environment-setup*"):
     btenv = glob.glob(btdir + env_glob)
     print("Using %s %s" % (name, btenv))
     # We either parse or wrap all our execution calls, rock and a hard place :(
-    with open(btenv[0], "r") as f:
-        for line in f.readlines():
-            if line.startswith("export "):
-                line = line.strip().split(" ", 1)[1].split("=", 1)
-                if "$PATH" in line[1]:
-                    line[1] = line[1].replace("$PATH", os.environ["PATH"])
-                if line[1].startswith(("'", '"')):
-                    line[1] = line[1][1:-1]
-                os.environ[line[0]] = line[1]
-            elif line.startswith("unset "):
-                line = line.strip().split(" ", 1)[1]
-                if line in os.environ:
-                    del os.environ[line]
+    # Run the environment script, dump the output and parse back in.
+    output = subprocess.check_output(". %s; set" % btenv[0], shell=True, text=True)
+    skip = ['_', 'SHELL', 'OLDPWD', 'PWD', 'TERM', 'PS4', 'USER', 'SHELLOPTS', 'IFS', 'EUID', 'UID', 'HOME', 'SHLVL', 'DIRSTACK', 'OPTERR', 'OPTIND', 'PIPESTATUS', 'PPID', 'GROUPS', 'POSIXLY_CORRECT']
+    env = {}
+    for line in output.splitlines():
+        if "=" not in line:
+             continue
+        key, val = line.split("=", 1)
+        if key in skip:
+            continue
+        if key.startswith("BASH"):
+            continue
+        if val[0] == "'" and val[-1] == "'":
+            val = val[1:-1]
+        env[key] = val
+    for key in os.environ:
+        if key not in env and key not in skip:
+            del os.environ[key]
+
+    for key in env:
+        os.environ[key] = env[key]
 
 # Unlike buildtools (a host/worker property, keyed by worker name globs),
 # the vcontainer-tarball is only needed by specific jobs (e.g.