2
0
mirror of https://gitlab.com/ita1024/waf.git synced 2024-11-22 09:57:15 +01:00

Error handling enhancements

This commit is contained in:
Thomas Nagy 2016-02-26 20:11:58 +01:00
parent f56f22bb50
commit 1ee5adc3b3
No known key found for this signature in database
GPG Key ID: 67A565EDFDF90E64
2 changed files with 49 additions and 37 deletions

View File

@ -830,8 +830,7 @@ def get_process():
process_lock.release()
return get_process()
def run_process(cmd, kwargs, cargs={}):
if os.name == 'java' or sys.platform == 'cli' or not kwargs.get('stdout', None) or not kwargs.get('stderr', None):
def run_regular_process(cmd, kwargs, cargs={}):
proc = subprocess.Popen(cmd, **kwargs)
if kwargs.get('stdout', None) or kwargs.get('stderr', None):
out, err = proc.communicate(**cargs)
@ -840,19 +839,33 @@ def run_process(cmd, kwargs, cargs={}):
out, err = (None, None)
status = proc.wait(**cargs)
return status, out, err
else:
def run_prefork_process(cmd, kwargs, cargs):
proc = get_process()
obj = base64.b64encode(cPickle.dumps([cmd, kwargs, cargs]))
proc.stdin.write(obj)
proc.stdin.write('\n'.encode())
proc.stdin.flush()
obj = proc.stdout.readline()
process_pool.append(proc)
if not obj:
raise OSError('Preforked sub-process died %r' % proc.getpid())
ret, out, err, ex = cPickle.loads(base64.b64decode(obj))
process_pool.append(proc)
ret, out, err, ex, trace = cPickle.loads(base64.b64decode(obj))
if ex:
# TODO
raise OSError(ex)
if ex == 'OSError':
raise OSError(trace)
elif ex == 'ValueError':
raise ValueError(trace)
else:
# something really strange
raise Exception(trace)
return ret, out, err
def run_process(cmd, kwargs, cargs={}):
if os.name == 'java' or sys.platform == 'cli' or not kwargs.get('stdout', None) or not kwargs.get('stderr', None):
return run_regular_process(cmd, kwargs, cargs)
else:
return run_prefork_process(cmd, kwargs, cargs)

View File

@ -1,9 +1,6 @@
#! /usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2015 (ita)
"""
"""
# Thomas Nagy, 2016 (ita)
import os, threading, sys, signal, time, traceback, base64
try:
@ -16,34 +13,36 @@ try:
except ImportError:
import subprocess
while 1:
def run():
txt = sys.stdin.readline().strip()
if not txt:
# parent process probably ended
break
sys.exit(1)
[cmd, kwargs, cargs] = cPickle.loads(base64.b64decode(txt))
cargs = cargs or {}
ret = 1
out, err = (None, None)
ex = None
out, err, ex, trace = (None, None, None, None)
try:
proc = subprocess.Popen(cmd, **kwargs)
out, err = proc.communicate(**cargs)
ret = proc.returncode
except OSError as e:
# TODO
except (OSError, ValueError, Exception) as e:
exc_type, exc_value, tb = sys.exc_info()
exc_lines = traceback.format_exception(exc_type, exc_value, tb)
ex = str(cmd) + '\n' + ''.join(exc_lines)
except ValueError as e:
# TODO
ex = str(e)
trace = str(cmd) + '\n' + ''.join(exc_lines)
ex = e.__class__.__name__
# it is just text so maybe we do not need to pickle()
tmp = [ret, out, err, ex]
tmp = [ret, out, err, ex, trace]
obj = base64.b64encode(cPickle.dumps(tmp))
sys.stdout.write(obj.decode())
sys.stdout.write('\n')
sys.stdout.flush()
while 1:
try:
run()
except KeyboardInterrupt:
break