summaryrefslogtreecommitdiffstats
path: root/ipapython
diff options
context:
space:
mode:
authorSimo Sorce <ssorce@redhat.com>2011-01-17 09:17:08 -0500
committerSimo Sorce <ssorce@redhat.com>2011-01-18 10:04:19 -0500
commit373455026e37d93c699e4ced579c75e2af8042aa (patch)
tree4144361e332739fa4eb4cf3cb2e514ab736cf643 /ipapython
parente73efb9a9000c2efb73297340c6268d59a11b6fc (diff)
downloadfreeipa-373455026e37d93c699e4ced579c75e2af8042aa.tar.gz
freeipa-373455026e37d93c699e4ced579c75e2af8042aa.tar.xz
freeipa-373455026e37d93c699e4ced579c75e2af8042aa.zip
Add a way to print output from commands
Instead pof always capturing the output, make it possible to let it go to the standard output pipes. Use this in ipactl to let init scripts show their output. Fixes: https://fedorahosted.org/freeipa/ticket/765
Diffstat (limited to 'ipapython')
-rw-r--r--ipapython/ipautil.py27
1 files changed, 19 insertions, 8 deletions
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index 8ce8bb97..69a410f4 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -92,7 +92,8 @@ def write_tmp_file(txt):
return fd
-def run(args, stdin=None, raiseonerr=True, nolog=(), env=None):
+def run(args, stdin=None, raiseonerr=True,
+ nolog=(), env=None, capture_output=True):
"""
Execute a command and return stdin, stdout and the process return code.
@@ -116,14 +117,23 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None):
If an value isn't found in the list it is silently ignored.
"""
+ p_in = None
+ p_out = None
+ p_err = None
+
if env is None:
env={"PATH": "/bin:/sbin:/usr/kerberos/bin:/usr/kerberos/sbin:/usr/bin:/usr/sbin"}
if stdin:
- p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, env=env)
- stdout,stderr = p.communicate(stdin)
- else:
- p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, env=env)
- stdout,stderr = p.communicate()
+ p_in = subprocess.PIPE
+ if capture_output:
+ p_out = subprocess.PIPE
+ p_err = subprocess.PIPE
+ elif len(nolog):
+ raise RuntimeError("Can't use nolog if output is not captured")
+
+ p = subprocess.Popen(args, stdin=p_in, stdout=p_out, stderr=p_err,
+ close_fds=True, env=env)
+ stdout,stderr = p.communicate(stdin)
# The command and its output may include passwords that we don't want
# to log. Run through the nolog items.
@@ -137,8 +147,9 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None):
stdout = stdout.replace(quoted, 'XXXXXXXX')
stderr = stderr.replace(quoted, 'XXXXXXXX')
logging.info('args=%s' % args)
- logging.info('stdout=%s' % stdout)
- logging.info('stderr=%s' % stderr)
+ if capture_output:
+ logging.info('stdout=%s' % stdout)
+ logging.info('stderr=%s' % stderr)
if p.returncode != 0 and raiseonerr:
raise CalledProcessError(p.returncode, args)