# HG changeset patch # User Bernhard Herzog # Date 1217498425 0 # Node ID 98de92b816d4c555d2e57e4d99055bdc3e3142d9 # Parent eaadf5350a1a668e5a47b914e8e0bd5ff0fc358a Add logfile argument to treepkg.run.call to log the output of the subprocess. Add a corresponding test case. diff -r eaadf5350a1a -r 98de92b816d4 test/test_run.py --- a/test/test_run.py Wed Jul 30 19:30:21 2008 +0000 +++ b/test/test_run.py Thu Jul 31 10:00:25 2008 +0000 @@ -11,6 +11,8 @@ import os import unittest +from filesupport import FileTestMixin + from treepkg.run import call, capture_output, SubprocessError @@ -62,6 +64,16 @@ except SubprocessError, exc: self.assertEquals(exc.returncode, 1) + +class TestRunWithLogging(unittest.TestCase, FileTestMixin): + + def test_run_with_logfile(self): + logfilename = self.temp_file_name("logfile") + call([sys.executable, "-c", "print \"I'm a lumber jack and I'm OK\""], + logfile=logfilename) + self.checkFileContents(logfilename, "I'm a lumber jack and I'm OK\n") + + class TestCaptureOutput(unittest.TestCase): def test_capture_output_stdout(self): diff -r eaadf5350a1a -r 98de92b816d4 treepkg/run.py --- a/treepkg/run.py Wed Jul 30 19:30:21 2008 +0000 +++ b/treepkg/run.py Thu Jul 31 10:00:25 2008 +0000 @@ -21,7 +21,8 @@ self.output = output -def call(command, suppress_output=False, extra_env=None, inputdata=None, **kw): +def call(command, suppress_output=False, extra_env=None, inputdata=None, + logfile=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 @@ -30,7 +31,9 @@ """ if inputdata is not None: kw["stdin"] = subprocess.PIPE - if suppress_output: + if logfile: + kw["stdout"] = kw["stderr"] = open(logfile, "w") + elif suppress_output: kw["stdout"] = open(os.devnull, "w") kw["stderr"] = open(os.devnull, "w") env = kw.pop("env", None)