From d9bb08208429349566139cd5fc65693c2493c372 Mon Sep 17 00:00:00 2001 From: Julien Fortin Date: Wed, 10 Aug 2016 17:48:18 +0200 Subject: [PATCH] ifupdown: utils: user command output on stdout Ticket: None Reviewed By: Roopa, Nikhil G Testing Done: $ cat /etc/network/interfaces auto eth0 iface eth0 inet dhcp up ls -l $ I realized that user command's output wasn't on stdout but in a pipe This commit fixes this case, the user cmd output is now on stdout. Signed-off-by: Julien Fortin --- ifupdown/utils.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ifupdown/utils.py b/ifupdown/utils.py index 6aeba0b..53c783a 100644 --- a/ifupdown/utils.py +++ b/ifupdown/utils.py @@ -171,7 +171,8 @@ class utils(): close_fds=False, stdout=True, stdin=None, - stderr=subprocess.STDOUT): + stderr=subprocess.STDOUT, + user_cmd=False): """ exec's commands using subprocess Popen Args: @@ -184,20 +185,30 @@ class utils(): return '' cmd_output = None + if user_cmd: + stdout = True + elif stdout: + stdout = subprocess.PIPE + else: + stdout = cls.DEVNULL try: ch = subprocess.Popen(cmd, env=env, shell=shell, close_fds=close_fds, stdin=subprocess.PIPE if stdin else None, - stdout=subprocess.PIPE if stdout else cls.DEVNULL, + stdout=stdout, stderr=stderr) utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT) if stdout or stdin: cmd_output = ch.communicate(input=stdin)[0] cmd_returncode = ch.wait() except Exception as e: - raise Exception('cmd \'%s\' failed (%s)' % (' '.join(cmd), str(e))) + if user_cmd: + raise Exception('cmd \'%s\' failed (%s)' % (cmd, str(e))) + else: + raise Exception('cmd \'%s\' failed (%s)' % + (' '.join(cmd), str(e))) finally: utils.disable_subprocess_signal_forwarding(signal.SIGINT) if cmd_returncode != 0: @@ -216,7 +227,8 @@ class utils(): close_fds=close_fds, stdout=stdout, stdin=stdin, - stderr=stderr) + stderr=stderr, + user_cmd=True) @classmethod def exec_command(cls, cmd, env=None, close_fds=False, stdout=True,