changeset 12689:697ef4cf18c0

mx.run should support streams for out/err even if there is no timeout
author Gilles Duboscq <duboscq@ssw.jku.at>
date Tue, 05 Nov 2013 19:10:52 +0100
parents 76a6070f7164
children c3bdd186e6cf
files mxtool/mx.py
diffstat 1 files changed, 20 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/mxtool/mx.py	Wed Nov 06 11:04:01 2013 +0100
+++ b/mxtool/mx.py	Tue Nov 05 19:10:52 2013 +0100
@@ -1425,34 +1425,28 @@
         else:
             preexec_fn = os.setsid
 
-        if not callable(out) and not callable(err) and timeout is None:
-            # The preexec_fn=os.setsid
-            p = subprocess.Popen(args, cwd=cwd, preexec_fn=preexec_fn, creationflags=creationflags, env=env)
-            _currentSubprocess = (p, args)
+        def redirect(stream, f):
+            for line in iter(stream.readline, ''):
+                f(line)
+            stream.close()
+        stdout = out if not callable(out) else subprocess.PIPE
+        stderr = err if not callable(err) else subprocess.PIPE
+        p = subprocess.Popen(args, cwd=cwd, stdout=stdout, stderr=stderr, preexec_fn=preexec_fn, creationflags=creationflags, env=env)
+        _currentSubprocess = (p, args)
+        if callable(out):
+            t = Thread(target=redirect, args=(p.stdout, out))
+            t.daemon = True  # thread dies with the program
+            t.start()
+        if callable(err):
+            t = Thread(target=redirect, args=(p.stderr, err))
+            t.daemon = True  # thread dies with the program
+            t.start()
+        if timeout is None or timeout == 0:
             retcode = waitOn(p)
         else:
-            def redirect(stream, f):
-                for line in iter(stream.readline, ''):
-                    f(line)
-                stream.close()
-            stdout = out if not callable(out) else subprocess.PIPE
-            stderr = err if not callable(err) else subprocess.PIPE
-            p = subprocess.Popen(args, cwd=cwd, stdout=stdout, stderr=stderr, preexec_fn=preexec_fn, creationflags=creationflags, env=env)
-            _currentSubprocess = (p, args)
-            if callable(out):
-                t = Thread(target=redirect, args=(p.stdout, out))
-                t.daemon = True  # thread dies with the program
-                t.start()
-            if callable(err):
-                t = Thread(target=redirect, args=(p.stderr, err))
-                t.daemon = True  # thread dies with the program
-                t.start()
-            if timeout is None or timeout == 0:
-                retcode = waitOn(p)
-            else:
-                if get_os() == 'windows':
-                    abort('Use of timeout not (yet) supported on Windows')
-                retcode = _waitWithTimeout(p, args, timeout)
+            if get_os() == 'windows':
+                abort('Use of timeout not (yet) supported on Windows')
+            retcode = _waitWithTimeout(p, args, timeout)
     except OSError as e:
         log('Error executing \'' + ' '.join(args) + '\': ' + str(e))
         if _opts.verbose: