aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPapoteur <papoteur@mageialinux-online.org>2015-11-30 22:46:47 +0100
committerPapoteur <papoteur@mageialinux-online.org>2015-11-30 22:46:47 +0100
commit6507f60a1b99d049178ca54cd8dc996faaae44b8 (patch)
treecc4230b29816147c3646469073181207ec7d62ac
parenteef97f32ab89f59d4d0d3eb9e557a5a3cffcefc2 (diff)
downloadmgarepo-6507f60a1b99d049178ca54cd8dc996faaae44b8.tar
mgarepo-6507f60a1b99d049178ca54cd8dc996faaae44b8.tar.gz
mgarepo-6507f60a1b99d049178ca54cd8dc996faaae44b8.tar.bz2
mgarepo-6507f60a1b99d049178ca54cd8dc996faaae44b8.tar.xz
mgarepo-6507f60a1b99d049178ca54cd8dc996faaae44b8.zip
Revert last changes in util.py
-rw-r--r--MgaRepo/util.py69
1 files changed, 37 insertions, 32 deletions
diff --git a/MgaRepo/util.py b/MgaRepo/util.py
index ea2214d..75073ee 100644
--- a/MgaRepo/util.py
+++ b/MgaRepo/util.py
@@ -8,40 +8,24 @@ import sys
import os
import re
import select
-from io import BytesIO
+from io import StringIO
import httplib2
-#import 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_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)
- 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
-
+# Our own version of commands' getstatusoutput(). 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 execcmd(*cmd, **kwargs):
cmdstr = " ".join(cmd)
if kwargs.get('info'):
@@ -50,12 +34,33 @@ def execcmd(*cmd, **kwargs):
prefix='LANG=C LANGUAGE=C LC_ALL=C '
if kwargs.get("show"):
if kwargs.get("geterr"):
- o, output, status = commands_exec(prefix + cmdstr)
+ err = StringIO()
+ 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)).decode('utf8')
+ edata = None
+ if ef in r:
+ edata = (os.read(ef, 8192)).decode('utf8')
+ err.write(edata)
+ sys.stderr.write(edata)
+
+ status = p.poll()
+ if status is not None and odata == '' and edata == '':
+ break
+ output = err.getvalue()
else:
status = os.system(prefix + cmdstr)
output = ""
else:
- output, e, status = commands_exec(prefix + cmdstr)
+ status, output = commands_getstatusoutput(prefix + cmdstr)
verbose = config.getbool("global", "verbose", 0)
if status != 0 and not kwargs.get("noerror"):
if kwargs.get("cleanerr") and not verbose:
@@ -64,7 +69,7 @@ def execcmd(*cmd, **kwargs):
raise Error("command failed: %s\n%s\n" % (cmdstr, output))
if verbose:
print(output)
- sys.stdout.buffer.write(output)
+ sys.stdout.write(output)
return status, output
def get_auth(username=None, password=None):