]> git.proxmox.com Git - mirror_ifupdown2.git/commitdiff
ifupdown: utils: user command output on stdout
authorJulien Fortin <julien@cumulusnetworks.com>
Wed, 10 Aug 2016 15:48:18 +0000 (17:48 +0200)
committerJulien Fortin <julien@cumulusnetworks.com>
Wed, 10 Aug 2016 15:48:32 +0000 (17:48 +0200)
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 <julien@cumulusnetworks.com>
ifupdown/utils.py

index 6aeba0b6a2556ff0f2f222b9ddec02c154ebe8ad..53c783a21798d5913ff318e5e51d9f87db3c6680 100644 (file)
@@ -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,