comparison mxtool/mx.py @ 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 ec224fef3012
children 68529068f08e
comparison
equal deleted inserted replaced
12688:76a6070f7164 12689:697ef4cf18c0
1423 if get_os() == 'windows': 1423 if get_os() == 'windows':
1424 creationflags = subprocess.CREATE_NEW_PROCESS_GROUP 1424 creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
1425 else: 1425 else:
1426 preexec_fn = os.setsid 1426 preexec_fn = os.setsid
1427 1427
1428 if not callable(out) and not callable(err) and timeout is None: 1428 def redirect(stream, f):
1429 # The preexec_fn=os.setsid 1429 for line in iter(stream.readline, ''):
1430 p = subprocess.Popen(args, cwd=cwd, preexec_fn=preexec_fn, creationflags=creationflags, env=env) 1430 f(line)
1431 _currentSubprocess = (p, args) 1431 stream.close()
1432 stdout = out if not callable(out) else subprocess.PIPE
1433 stderr = err if not callable(err) else subprocess.PIPE
1434 p = subprocess.Popen(args, cwd=cwd, stdout=stdout, stderr=stderr, preexec_fn=preexec_fn, creationflags=creationflags, env=env)
1435 _currentSubprocess = (p, args)
1436 if callable(out):
1437 t = Thread(target=redirect, args=(p.stdout, out))
1438 t.daemon = True # thread dies with the program
1439 t.start()
1440 if callable(err):
1441 t = Thread(target=redirect, args=(p.stderr, err))
1442 t.daemon = True # thread dies with the program
1443 t.start()
1444 if timeout is None or timeout == 0:
1432 retcode = waitOn(p) 1445 retcode = waitOn(p)
1433 else: 1446 else:
1434 def redirect(stream, f): 1447 if get_os() == 'windows':
1435 for line in iter(stream.readline, ''): 1448 abort('Use of timeout not (yet) supported on Windows')
1436 f(line) 1449 retcode = _waitWithTimeout(p, args, timeout)
1437 stream.close()
1438 stdout = out if not callable(out) else subprocess.PIPE
1439 stderr = err if not callable(err) else subprocess.PIPE
1440 p = subprocess.Popen(args, cwd=cwd, stdout=stdout, stderr=stderr, preexec_fn=preexec_fn, creationflags=creationflags, env=env)
1441 _currentSubprocess = (p, args)
1442 if callable(out):
1443 t = Thread(target=redirect, args=(p.stdout, out))
1444 t.daemon = True # thread dies with the program
1445 t.start()
1446 if callable(err):
1447 t = Thread(target=redirect, args=(p.stderr, err))
1448 t.daemon = True # thread dies with the program
1449 t.start()
1450 if timeout is None or timeout == 0:
1451 retcode = waitOn(p)
1452 else:
1453 if get_os() == 'windows':
1454 abort('Use of timeout not (yet) supported on Windows')
1455 retcode = _waitWithTimeout(p, args, timeout)
1456 except OSError as e: 1450 except OSError as e:
1457 log('Error executing \'' + ' '.join(args) + '\': ' + str(e)) 1451 log('Error executing \'' + ' '.join(args) + '\': ' + str(e))
1458 if _opts.verbose: 1452 if _opts.verbose:
1459 raise e 1453 raise e
1460 abort(e.errno) 1454 abort(e.errno)