3 # Thomas Nagy, 2016-2018 (ita)
5 import os
, sys
, traceback
, base64
, signal
9 import pickle
as cPickle
12 import subprocess32
as subprocess
17 TimeoutExpired
= subprocess
.TimeoutExpired
18 except AttributeError:
19 class TimeoutExpired(Exception):
23 txt
= sys
.stdin
.readline().strip()
25 # parent process probably ended
27 [cmd
, kwargs
, cargs
] = cPickle
.loads(base64
.b64decode(txt
))
30 if not 'close_fds' in kwargs
:
32 kwargs
['close_fds'] = False
35 out
, err
, ex
, trace
= (None, None, None, None)
37 proc
= subprocess
.Popen(cmd
, **kwargs
)
39 out
, err
= proc
.communicate(**cargs
)
40 except TimeoutExpired
:
41 if kwargs
.get('start_new_session') and hasattr(os
, 'killpg'):
42 os
.killpg(proc
.pid
, signal
.SIGKILL
)
45 out
, err
= proc
.communicate()
46 exc
= TimeoutExpired(proc
.args
, timeout
=cargs
['timeout'], output
=out
)
50 except Exception as e
:
51 exc_type
, exc_value
, tb
= sys
.exc_info()
52 exc_lines
= traceback
.format_exception(exc_type
, exc_value
, tb
)
53 trace
= str(cmd
) + '\n' + ''.join(exc_lines
)
54 ex
= e
.__class
__.__name
__
56 # it is just text so maybe we do not need to pickle()
57 tmp
= [ret
, out
, err
, ex
, trace
]
58 obj
= base64
.b64encode(cPickle
.dumps(tmp
))
59 sys
.stdout
.write(obj
.decode())
60 sys
.stdout
.write('\n')
66 except KeyboardInterrupt: