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

Richard Purdie <[email protected]>
Newsgroups org.yoctoproject.lists.yocto-patches
Message-ID <[email protected]>
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 <[email protected]>
---
 scripts/utils.py | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.