[elbe-devel] [PATCH v3 12/52] do and chroot in 'shellhelper.py'

Torben Hohn torben.hohn at linutronix.de
Thu Jul 4 15:44:28 CEST 2019


On Thu, Jun 27, 2019 at 02:44:26PM +0200, dion at linutronix.de wrote:
> From: Olivier Dion <dion at linutronix.de>
> 
> 'do' and 'chroot', formerly 'ASCIIDocLog::do' and
> 'ASCIIDocLog::chroot', are now global functions.
> 
> * chroot
> 
>   Is a wrapper around 'do' and simply put "chroot" in front of the
>   command to excute.
> 
> * do
> 
>   Will call 'Popen' and redirect its outputs to 'logging'.
> 
> * get_command_out
> 
>   Work similary to 'do', but only print stderr.
> 
> Signed-off-by: Olivier Dion <dion at linutronix.de>

Reviewed-by: Torben Hohn <torben.hohn at linutronix.de>

> ---
>  elbepack/shellhelper.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 61 insertions(+)
> 
> diff --git a/elbepack/shellhelper.py b/elbepack/shellhelper.py
> index 8de9e8c2..0895c0b0 100644
> --- a/elbepack/shellhelper.py
> +++ b/elbepack/shellhelper.py
> @@ -6,8 +6,16 @@
>  # SPDX-License-Identifier: GPL-3.0-or-later
>  
>  import os
> +import logging
> +
>  from subprocess import Popen, PIPE, STDOUT, call
>  
> +from elbepack.log import async_logging
> +
> +
> +log = logging.getLogger("log")
> +soap = logging.getLogger("soap")
> +
>  
>  class CommandError(Exception):
>      def __init__(self, cmd, returncode):
> @@ -84,3 +92,56 @@ def system_out_stderr(cmd, stdin=None, allow_fail=False, env_add=None):
>              raise CommandError(cmd, code)
>  
>      return out, err
> +
> +
> +def do(cmd, allow_fail=False, stdin=None, env_add=None):
> +    new_env = os.environ.copy()
> +    if env_add:
> +        new_env.update(env_add)
> +
> +    logging.info(cmd, extra={"context":"[CMD] "})
> +
> +    r, w = os.pipe()
> +
> +    if stdin is None:
> +        p = Popen(cmd, shell=True, stdout=w, stderr=STDOUT, env=new_env)
> +    else:
> +        p = Popen(cmd, shell=True, stdin=PIPE, stdout=w, stderr=STDOUT, env=new_env)
> +
> +    async_logging(r, w, soap, log)
> +    p.communicate(input=stdin)
> +
> +    if p.returncode and not allow_fail:
> +        raise CommandError(cmd, p.returncode)
> +
> +
> +
> +def chroot(directory, cmd, env_add=None, **kwargs):
> +    new_env = {"LANG":"C",
> +               "LANGUAGE":"C",
> +               "LC_ALL":"C"}
> +    if env_add:
> +        new_env.update(env_add)
> +    chcmd = 'chroot %s %s' % (directory, cmd)
> +    do(chcmd, env_add=new_env, **kwargs)
> +
> +def get_command_out(cmd, stdin=None, allow_fail=False, env_add={}):
> +    new_env = os.environ.copy()
> +    new_env.update(env_add)
> +
> +    logging.info(cmd, extra={"context":"[CMD] "})
> +
> +    r, w = os.pipe()
> +
> +    if stdin is None:
> +        p = Popen(cmd, shell=True, stdout=PIPE, stderr=w, env=new_env)
> +    else:
> +        p = Popen(cmd, shell=True, stdin=PIPE, stdout=w, stderr=STDOUT, env=new_env)
> +
> +    async_logging(r, w, soap, log)
> +    stdout, stderr = p.communicate(input=stdin)
> +
> +    if p.returncode and not allow_fail:
> +        raise CommandError(cmd, p.returncode)
> +
> +    return stdout
> -- 
> 2.11.0
> 
> 
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel

-- 
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy 
can be found here): https://linutronix.de/kontakt/Datenschutz.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen | 
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700 
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner



More information about the elbe-devel mailing list