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

dion at linutronix.de dion at linutronix.de
Mon Jul 8 17:11:07 CEST 2019


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




More information about the elbe-devel mailing list