# HG changeset patch # User Bernhard Herzog # Date 1213970787 0 # Node ID a68a4e22549c5b3334b65a58a513e7647295ef89 # Parent 674d14305e97a09a8a4d6e8b65b67fb610f69739 Extend treepkg.run.call so that data can be fed to stdin of the subprocess. diff -r 674d14305e97 -r a68a4e22549c test/test_run.py --- 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) diff -r 674d14305e97 -r a68a4e22549c treepkg/run.py --- 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)