aboutsummaryrefslogtreecommitdiffstats
path: root/MgaRepo/util.py
diff options
context:
space:
mode:
authorPapoteur <papoteur@mageialinux-online.org>2015-11-29 08:06:35 +0100
committerPapoteur <papoteur@mageialinux-online.org>2015-11-29 08:06:35 +0100
commit4efedcc1a68b65ba80c20b6d51579cd61da46537 (patch)
treef990a27367d14592ce6403b2740e70d14f5c0f1a /MgaRepo/util.py
parent9f0f55fd9ac2985a7f2e7c52010e00c9b3364fb1 (diff)
downloadmgarepo-4efedcc1a68b65ba80c20b6d51579cd61da46537.tar
mgarepo-4efedcc1a68b65ba80c20b6d51579cd61da46537.tar.gz
mgarepo-4efedcc1a68b65ba80c20b6d51579cd61da46537.tar.bz2
mgarepo-4efedcc1a68b65ba80c20b6d51579cd61da46537.tar.xz
mgarepo-4efedcc1a68b65ba80c20b6d51579cd61da46537.zip
Manage exec command with subprocess.Popen in more cases
Diffstat (limited to 'MgaRepo/util.py')
-rw-r--r--MgaRepo/util.py68
1 files changed, 33 insertions, 35 deletions
diff --git a/MgaRepo/util.py b/MgaRepo/util.py
index 84c8a63..9d1086d 100644
--- a/MgaRepo/util.py
+++ b/MgaRepo/util.py
@@ -12,18 +12,39 @@ from io import BytesIO
import httplib2
#import commands
-# Our own version of commands' getstatusoutput(). We have a commands
+# Our own version of commands' commands_exec(). We have a commands
# module directory, so we can't import Python's standard module
-def commands_getstatusoutput(cmd):
- """Return (status, output) of executing cmd in a shell."""
- import os
- pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
- text = pipe.read()
- sts = pipe.close()
- if sts is None: sts = 0
- if text[-1:] == '\n': text = text[:-1]
- return sts, text
+def commands_exec(cmdstr, **kwargs):
+ err = BytesIO()
+ out = BytesIO()
+ pstdin = kwargs.get("stdin") if kwargs.get("stdin") else None
+ p = subprocess.Popen(cmdstr, shell=True,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ stdin=pstdin)
+ of = p.stdout.fileno()
+ ef = p.stderr.fileno()
+ while True:
+ r,w,x = select.select((of,ef), (), ())
+ odata = None
+ if of in r:
+ odata = os.read(of, 8192)
+ out.write(odata)
+ sys.stdout.buffer.write(odata)
+
+ edata = None
+ if ef in r:
+ edata = os.read(ef, 8192)
+ err.write(edata)
+ sys.stderr.buffer.write(edata)
+
+ status = p.poll()
+ if status is not None and odata == b'' and edata == b'':
+ break
+ e = err.getvalue().decode('utf8')
+ o = out.getvalue().decode('utf8')
+ return o, e, status
+
def execcmd(*cmd, **kwargs):
cmdstr = " ".join(cmd)
if kwargs.get('info'):
@@ -32,35 +53,12 @@ def execcmd(*cmd, **kwargs):
prefix='LANG=C LANGUAGE=C LC_ALL=C '
if kwargs.get("show"):
if kwargs.get("geterr"):
- err = BytesIO()
- pstdin = kwargs.get("stdin") if kwargs.get("stdin") else None
- p = subprocess.Popen(prefix + cmdstr, shell=True,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE,
- stdin=pstdin)
- of = p.stdout.fileno()
- ef = p.stderr.fileno()
- while True:
- r,w,x = select.select((of,ef), (), ())
- odata = None
- if of in r:
- odata = os.read(of, 8192)
- sys.stdout.buffer.write(odata)
-
- edata = None
- if ef in r:
- edata = os.read(ef, 8192)
- err.write(edata)
- sys.stderr.buffer.write(edata)
-
- status = p.poll()
- if status is not None and odata == b'' and edata == b'':
- break
- output = err.getvalue()
+ o, output, status = commands_exec(prefix + cmdstr)
else:
status = os.system(prefix + cmdstr)
output = ""
else:
- status, output = commands_getstatusoutput(prefix + cmdstr)
+ output, e, status = commands_exec(prefix + cmdstr)
verbose = config.getbool("global", "verbose", 0)
if status != 0 and not kwargs.get("noerror"):
if kwargs.get("cleanerr") and not verbose: