Fix errRun to not exit until all of stdout and stderr have been read.

This commit is contained in:
Bob Lantz
2012-05-09 22:59:30 -07:00
parent 55cf19c4de
commit cfd381134f
+10 -8
View File
@@ -74,31 +74,33 @@ def errRun( *cmd, **kwargs ):
# cmd goes to stderr, output goes to stdout # cmd goes to stderr, output goes to stdout
info( cmd, '\n' ) info( cmd, '\n' )
popen = Popen( cmd, stdout=PIPE, stderr=stderr, shell=shell ) popen = Popen( cmd, stdout=PIPE, stderr=stderr, shell=shell )
# We use poll() because select() doesn't work with large fd numbers # We use poll() because select() doesn't work with large fd numbers,
# and thus communicate() doesn't work either
out, err = '', '' out, err = '', ''
poller = poll() poller = poll()
poller.register( popen.stdout, POLLIN ) poller.register( popen.stdout, POLLIN )
fdtofile = { popen.stdout.fileno(): popen.stdout } fdtofile = { popen.stdout.fileno(): popen.stdout }
outDone, errDone = False, True
if popen.stderr: if popen.stderr:
fdtofile[ popen.stderr.fileno() ] = popen.stderr fdtofile[ popen.stderr.fileno() ] = popen.stderr
poller.register( popen.stderr, POLLIN ) poller.register( popen.stderr, POLLIN )
while True: errDone = False
while not outDone or not errDone:
readable = poller.poll() readable = poller.poll()
# Tell pylint to ignore unused variable event
# pylint: disable-msg=W0612
for fd, event in readable: for fd, event in readable:
# pylint: enable-msg=W0612
f = fdtofile[ fd ] f = fdtofile[ fd ]
data = f.read( 1024 ) data = f.read( 1024 )
if echo: if echo:
output( data ) output( data )
if f == popen.stdout: if f == popen.stdout:
out += data out += data
if data == '':
outDone = True
elif f == popen.stderr: elif f == popen.stderr:
err += data err += data
returncode = popen.poll() if data == '':
if returncode is not None: errDone = True
break returncode = popen.wait()
return out, err, returncode return out, err, returncode
def errFail( *cmd, **kwargs ): def errFail( *cmd, **kwargs ):