changeset 164:a68a4e22549c

Extend treepkg.run.call so that data can be fed to stdin of the subprocess.
author Bernhard Herzog <bh@intevation.de>
date Fri, 20 Jun 2008 14:06:27 +0000
parents 674d14305e97
children c7ac67366492
files test/test_run.py treepkg/run.py
diffstat 2 files changed, 22 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/test/test_run.py	Fri Jun 20 10:19:26 2008 +0000
+++ b/test/test_run.py	Fri Jun 20 14:06:27 2008 +0000
@@ -48,3 +48,16 @@
         else:
             # test OK
             pass
+
+    def test_call_inputdata(self):
+        """Test call with inputdata passed to the subprocesses stdin"""
+        # FIXME: If the feature being tested is not implemented
+        # properly, it's likely that the subprocess will wait
+        # indefinitely waiting for input.
+        data = "1"
+        subprocess_cmd = [sys.executable, "-c",
+                          "import sys; sys.exit(int(raw_input("")))"]
+        try:
+            call(subprocess_cmd, inputdata=data)
+        except SubprocessError, exc:
+            self.assertEquals(exc.returncode, 1)
--- a/treepkg/run.py	Fri Jun 20 10:19:26 2008 +0000
+++ b/treepkg/run.py	Fri Jun 20 14:06:27 2008 +0000
@@ -20,13 +20,15 @@
         self.returncode = returncode
 
 
-def call(command, suppress_output=False, extra_env=None, **kw):
+def call(command, suppress_output=False, extra_env=None, inputdata=None, **kw):
     """Run command as a subprocess and wait until it is finished.
 
     The command should be given as a list of strings to avoid problems
     with shell quoting.  If the command exits with a return code other
     than 0, a SubprocessError is raised.
     """
+    if inputdata is not None:
+        kw["stdin"] = subprocess.PIPE
     if suppress_output:
         kw["stdout"] = open(os.devnull, "w")
         kw["stderr"] = open(os.devnull, "w")
@@ -35,7 +37,12 @@
         if env is None:
             env = os.environ.copy()
         env.update(extra_env)
-    ret = subprocess.call(command, env=env, **kw)
+
+    process = subprocess.Popen(command, env=env, **kw)
+    if inputdata is not None:
+        process.stdin.write(inputdata)
+        process.stdin.close()
+    ret = process.wait()
     if ret != 0:
         raise SubprocessError(command, ret)
 
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)