[elbe-devel] [PATCH 01/20] shellhelper: add env_add parameter to allow setting environment
Torben Hohn
torben.hohn at linutronix.de
Fri Oct 12 11:27:47 CEST 2018
currently environment variables are setup using os.environ['X']='y'.
these changes are process wide, and pose risks for race conditions,
when more than one elbe build runs inside the daemon.
prepare to remove usage of os.environ by allowing to set environment
variables in shellhelper calls using Popen(env=).
Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>
---
elbepack/shellhelper.py | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)
diff --git a/elbepack/shellhelper.py b/elbepack/shellhelper.py
index 364a0ee7..ab842e16 100644
--- a/elbepack/shellhelper.py
+++ b/elbepack/shellhelper.py
@@ -28,19 +28,25 @@ def system(cmd, allow_fail=False):
raise CommandError(cmd, ret)
-def command_out(cmd, stdin=None, output=PIPE):
+def command_out(cmd, stdin=None, output=PIPE, env_add=None):
+ new_env = os.environ.copy()
+ if env_add:
+ new_env.update(env_add)
+
if stdin is None:
- p = Popen(cmd, shell=True, stdout=output, stderr=STDOUT)
+ p = Popen(cmd, shell=True,
+ stdout=output, stderr=STDOUT, env=new_env)
out, _ = p.communicate()
else:
- p = Popen(cmd, shell=True, stdout=output, stderr=STDOUT, stdin=PIPE)
+ p = Popen(cmd, shell=True,
+ stdout=output, stderr=STDOUT, stdin=PIPE, env=new_env)
out, _ = p.communicate(input=stdin)
return p.returncode, out
-def system_out(cmd, stdin=None, allow_fail=False):
- code, out = command_out(cmd, stdin)
+def system_out(cmd, stdin=None, allow_fail=False, env_add=None):
+ code, out = command_out(cmd, stdin=stdin, env_add=env_add)
if code != 0:
if not allow_fail:
@@ -49,19 +55,25 @@ def system_out(cmd, stdin=None, allow_fail=False):
return out
-def command_out_stderr(cmd, stdin=None):
+def command_out_stderr(cmd, stdin=None, env_add=None):
+ new_env = os.environ.copy()
+ if env_add:
+ new_env.update(env_add)
+
if stdin is None:
- p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
+ p = Popen(cmd, shell=True,
+ stdout=PIPE, stderr=PIPE, env=new_env)
output, stderr = p.communicate()
else:
- p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
+ p = Popen(cmd, shell=True,
+ stdout=PIPE, stderr=PIPE, stdin=PIPE, env=new_env)
output, stderr = p.communicate(input=stdin)
return p.returncode, output, stderr
-def system_out_stderr(cmd, stdin=None, allow_fail=False):
- code, out, err = command_out_stderr(cmd, stdin)
+def system_out_stderr(cmd, stdin=None, allow_fail=False, env_add=None):
+ code, out, err = command_out_stderr(cmd, stdin, env_add)
if code != 0:
if not allow_fail:
--
2.11.0
More information about the elbe-devel
mailing list