[elbe-devel] [PATCH 07/20] commands chroot: use env_add parameter instead of messing with os.environ

Torben Hohn torben.hohn at linutronix.de
Fri Oct 12 11:27:53 CEST 2018


messing with os.environ is risky, because its process wide, and does not
work with threads. We want to ban modifications to os.environ as far as
possible from elbe.

use shellhelper.system(env_add=) instead of os.system().
The old code did not check the return value, keep the old behaviour.

Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>
---
 elbepack/commands/chroot.py | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/elbepack/commands/chroot.py b/elbepack/commands/chroot.py
index 7529fcf7..0c97d97f 100644
--- a/elbepack/commands/chroot.py
+++ b/elbepack/commands/chroot.py
@@ -14,6 +14,7 @@ import os
 
 from elbepack.elbeproject import ElbeProject
 from elbepack.elbexml import ValidationError, ValidationMode
+from elbepack.shellhelper import system
 
 
 def run_command(argv):
@@ -46,11 +47,10 @@ def run_command(argv):
         print("xml validation failed. Bailing out")
         sys.exit(20)
 
-    os.environ["LANG"] = "C"
-    os.environ["LANGUAGE"] = "C"
-    os.environ["LC_ALL"] = "C"
-    # TODO: howto set env in chroot?
-    os.environ["PS1"] = project.xml.text('project/name') + ': \w\$'
+    default_chroot_env = {"LANG": "C",
+                          "LANGUAGE": "C",
+                          "LC_ALL": "C",
+                          "PS1": project.xml.text('project/name') + ': \\w\\$'}
 
     cmd = "/bin/bash"
 
@@ -62,7 +62,11 @@ def run_command(argv):
 
     if opt.target:
         with project.targetfs:
-            os.system("/usr/sbin/chroot %s %s" % (project.targetpath, cmd))
+            system("/usr/sbin/chroot %s %s" % (project.targetpath, cmd),
+                   allow_fail=True,
+                   env_add=default_chroot_env)
     else:
         with project.buildenv:
-            os.system("/usr/sbin/chroot %s %s" % (project.chrootpath, cmd))
+            system("/usr/sbin/chroot %s %s" % (project.chrootpath, cmd),
+                   allow_fail=True,
+                   env_add=default_chroot_env)
-- 
2.11.0




More information about the elbe-devel mailing list