[elbe-devel] [PATCH 01/20] shellhelper: add env_add parameter to allow setting environment

Manuel Traut manut at linutronix.de
Fri Nov 23 10:27:39 CET 2018


On 11:27 Fri 12 Oct     , Torben Hohn wrote:
> 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>

Reviewed-by: Manuel Traut <manut at linutronix.de>

and merged into devel/elbe-3.0

> ---
>  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
> 
> 
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel



More information about the elbe-devel mailing list