changeset 193:98de92b816d4

Add logfile argument to treepkg.run.call to log the output of the subprocess. Add a corresponding test case.
author Bernhard Herzog <bh@intevation.de>
date Thu, 31 Jul 2008 10:00:25 +0000
parents eaadf5350a1a
children 86360f3d5611
files test/test_run.py treepkg/run.py
diffstat 2 files changed, 17 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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):
--- 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)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)