mirror of
https://gitlab.com/ita1024/waf.git
synced 2024-11-22 01:46:15 +01:00
Error handling enhancements
This commit is contained in:
parent
f56f22bb50
commit
1ee5adc3b3
@ -830,29 +830,42 @@ def get_process():
|
||||
process_lock.release()
|
||||
return get_process()
|
||||
|
||||
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)
|
||||
status = proc.returncode
|
||||
else:
|
||||
out, err = (None, None)
|
||||
status = proc.wait(**cargs)
|
||||
return status, out, err
|
||||
|
||||
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()
|
||||
if not obj:
|
||||
raise OSError('Preforked sub-process died %r' % proc.getpid())
|
||||
|
||||
process_pool.append(proc)
|
||||
ret, out, err, ex, trace = cPickle.loads(base64.b64decode(obj))
|
||||
if 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):
|
||||
proc = subprocess.Popen(cmd, **kwargs)
|
||||
if kwargs.get('stdout', None) or kwargs.get('stderr', None):
|
||||
out, err = proc.communicate(**cargs)
|
||||
status = proc.returncode
|
||||
else:
|
||||
out, err = (None, None)
|
||||
status = proc.wait(**cargs)
|
||||
return status, out, err
|
||||
return run_regular_process(cmd, kwargs, cargs)
|
||||
else:
|
||||
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)
|
||||
|
||||
ret, out, err, ex = cPickle.loads(base64.b64decode(obj))
|
||||
if ex:
|
||||
# TODO
|
||||
raise OSError(ex)
|
||||
return ret, out, err
|
||||
return run_prefork_process(cmd, kwargs, cargs)
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user