Commits

Thomas Waldmann committed df8543e Draft

improved exec_cmd

Comments (0)

Files changed (1)

 import subprocess
 
 
-def exec_cmd(cmd, input=None):
-    p = subprocess.Popen(cmd, shell=True,
-                        close_fds=not subprocess.mswindows,
-                        bufsize=1024,
-                        stdin=subprocess.PIPE,
-                        stdout=subprocess.PIPE,
-                        stderr=subprocess.PIPE)
+def exec_cmd(cmd, input=None, **kw):
+    """execute cmd, give it <input> as stdin, return its stdout output,
+    stderr output and return code.
+    """
+    p = subprocess.Popen(cmd, **kw)
     data, errors = p.communicate(input)
     return data, errors, p.returncode
 
+
+def exec_shell(cmd, input=None,
+               shell=True,
+               close_fds=not subprocess.mswindows,
+               bufsize=1024,
+               stdin=subprocess.PIPE,
+               stdout=subprocess.PIPE,
+               stderr=subprocess.PIPE,
+               **kw):
+    """execute cmd as a shell command"""
+    kw.update(dict(
+        shell=shell,
+        close_fds=close_fds,
+        bufsize=bufsize,
+        stdin=stdin,
+        stdout=stdout,
+        stderr=stderr,
+    ))
+    return exec_cmd(cmd, input, **kw)
+