ipautil: allow redirecting command output to standard output in run()

https://fedorahosted.org/freeipa/ticket/5527

Reviewed-By: Tomas Babej <tbabej@redhat.com>
This commit is contained in:
Jan Cholasta 2015-12-14 12:45:45 +01:00 committed by Tomas Babej
parent b248dfda39
commit f49cdfe392

View File

@ -286,7 +286,7 @@ class _RunResult(collections.namedtuple('_RunResult',
def run(args, stdin=None, raiseonerr=True, nolog=(), env=None, def run(args, stdin=None, raiseonerr=True, nolog=(), env=None,
capture_output=False, skip_output=False, cwd=None, capture_output=False, skip_output=False, cwd=None,
runas=None, timeout=None, suplementary_groups=[], runas=None, timeout=None, suplementary_groups=[],
capture_error=False, encoding=None): capture_error=False, encoding=None, redirect_output=False):
""" """
Execute an external command. Execute an external command.
@ -323,6 +323,7 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None,
:param encoding: For Python 3, the encoding to use for output, :param encoding: For Python 3, the encoding to use for output,
error_output, and (if it's not bytes) stdin. error_output, and (if it's not bytes) stdin.
If None, the current encoding according to locale is used. If None, the current encoding according to locale is used.
:param redirect_output: Redirect (error) output to standard (error) output.
:return: An object with these attributes: :return: An object with these attributes:
@ -362,6 +363,13 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None,
raise ValueError('skip_output is incompatible with ' raise ValueError('skip_output is incompatible with '
'capture_output or capture_error') 'capture_output or capture_error')
if redirect_output and (capture_output or capture_error):
raise ValueError('redirect_output is incompatible with '
'capture_output or capture_error')
if skip_output and redirect_output:
raise ValueError('skip_output is incompatible with redirect_output')
if env is None: if env is None:
# copy default env # copy default env
env = copy.deepcopy(os.environ) env = copy.deepcopy(os.environ)
@ -370,6 +378,9 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None,
p_in = subprocess.PIPE p_in = subprocess.PIPE
if skip_output: if skip_output:
p_out = p_err = open(paths.DEV_NULL, 'w') p_out = p_err = open(paths.DEV_NULL, 'w')
elif redirect_output:
p_out = sys.stdout
p_err = sys.stderr
else: else:
p_out = subprocess.PIPE p_out = subprocess.PIPE
p_err = subprocess.PIPE p_err = subprocess.PIPE
@ -432,7 +443,7 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None,
# The command and its output may include passwords that we don't want # The command and its output may include passwords that we don't want
# to log. Replace those. # to log. Replace those.
if skip_output: if skip_output or redirect_output:
output_log = None output_log = None
error_log = None error_log = None
else: else: