[master 3/5] Add an option to execReadlines to filter out stderr.

dashea installerbot-noreply at redhat.com
Tue Jul 7 18:34:14 UTC 2015


From: David Shea <dshea at redhat.com>

---
 pyanaconda/iutil.py                  | 11 ++++++++---
 tests/pyanaconda_tests/iutil_test.py | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 6888253..7d80ed7 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -392,7 +392,7 @@ def execWithCaptureBinary(command, argv, stdin=None, root='/', log_output=False,
     return _run_program(argv, stdin=stdin, root=root, log_output=log_output,
                         filter_stderr=filter_stderr, binary_output=True)[1]
 
-def execReadlines(command, argv, stdin=None, root='/', env_prune=None):
+def execReadlines(command, argv, stdin=None, root='/', env_prune=None, filter_stderr=False):
     """ Execute an external command and return the line output of the command
         in real-time.
 
@@ -407,9 +407,9 @@ def execReadlines(command, argv, stdin=None, root='/', env_prune=None):
         :param argv: The argument list
         :param stdin: The file object to read stdin from.
         :param stdout: Optional file object to redirect stdout and stderr to.
-        :param stderr: not used
         :param root: The directory to chroot to before running command.
         :param env_prune: environment variable to remove before execution
+        :param filter_stderr: Whether stderr should be excluded from the returned output
 
         Output from the file is not logged to program.log
         This returns an iterator with the lines from the command until it has finished
@@ -456,8 +456,13 @@ def __next__(self):
 
     argv = [command] + argv
 
+    if filter_stderr:
+        stderr = subprocess.DEVNULL
+    else:
+        stderr = subprocess.STDOUT
+
     try:
-        proc = startProgram(argv, root=root, stdin=stdin, env_prune=env_prune, bufsize=1)
+        proc = startProgram(argv, root=root, stdin=stdin, stderr=stderr, env_prune=env_prune, bufsize=1)
     except OSError as e:
         with program_log_lock:
             program_log.error("Error running %s: %s", argv[0], e.strerror)
diff --git a/tests/pyanaconda_tests/iutil_test.py b/tests/pyanaconda_tests/iutil_test.py
index ad0bc82..090d84a 100644
--- a/tests/pyanaconda_tests/iutil_test.py
+++ b/tests/pyanaconda_tests/iutil_test.py
@@ -288,6 +288,42 @@ def _hup_handler(signum, frame):
         finally:
             signal.signal(signal.SIGHUP, old_HUP_handler)
 
+    def exec_readlines_test_filter_stderr(self):
+        """Test execReadlines and filter_stderr."""
+
+        # Test that stderr is normally included
+        with tempfile.NamedTemporaryFile(mode="w+t") as testscript:
+            testscript.write("""#!/bin/sh
+echo "one"
+echo "two" >&2
+echo "three"
+exit 0
+""")
+            testscript.flush()
+
+            with timer(5):
+                rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
+                self.assertEqual(next(rl_iterator), "one")
+                self.assertEqual(next(rl_iterator), "two")
+                self.assertEqual(next(rl_iterator), "three")
+                self.assertRaises(StopIteration, rl_iterator.__next__)
+
+        # Test that filter stderr removes the middle line
+        with tempfile.NamedTemporaryFile(mode="w+t") as testscript:
+            testscript.write("""#!/bin/sh
+echo "one"
+echo "two" >&2
+echo "three"
+exit 0
+""")
+            testscript.flush()
+
+            with timer(5):
+                rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name], filter_stderr=True)
+                self.assertEqual(next(rl_iterator), "one")
+                self.assertEqual(next(rl_iterator), "three")
+                self.assertRaises(StopIteration, rl_iterator.__next__)
+
     def start_program_preexec_fn_test(self):
         """Test passing preexec_fn to startProgram."""
 


-- 
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/bf623e38f0a745ba477a5f9a9236a18d8ec58ffa


More information about the anaconda-patches mailing list