[elbe-devel] [PATCH v2 02/28] do and chroot in 'shellhelper.py'

Torben Hohn torben.hohn at linutronix.de
Wed Jun 26 14:13:42 CEST 2019


On Fri, Jun 21, 2019 at 07:39:54PM +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>

looks good, one issue inline.

> ---
>  elbepack/shellhelper.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 68 insertions(+)
> 
> diff --git a/elbepack/shellhelper.py b/elbepack/shellhelper.py
> index 8de9e8c2..19fccfee 100644
> --- a/elbepack/shellhelper.py
> +++ b/elbepack/shellhelper.py
> @@ -6,8 +6,17 @@
>  # SPDX-License-Identifier: GPL-3.0-or-later
>  
>  import os
> +import textwrap
> +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 +93,62 @@ def system_out_stderr(cmd, stdin=None, allow_fail=False, env_add=None):
>              raise CommandError(cmd, code)
>  
>      return out, err
> +
> +
> +cmd_header_wrap = textwrap.TextWrapper(width=70,
> +                                       subsequent_indent='  ',
> +                                       break_long_words=False,
> +                                       break_on_hyphens=False)
> +def wrap_cmd(cmd):
> +    return ' \\\n'.join(cmd_header_wrap.wrap(cmd))
> +
> +
> +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, **kwargs):
> +    os.environ["LANG"] = "C"
> +    os.environ["LANGUAGE"] = "C"
> +    os.environ["LC_ALL"] = "C"

please use env_add for these.


> +    chcmd = 'chroot %s %s' % (directory, cmd)
> +    do(chcmd, **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("$ %s", wrap_cmd(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