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
))
31 out
, err
, ex
, trace
= (None, None, None, None)
33 proc
= subprocess
.Popen(cmd
, **kwargs
)
35 out
, err
= proc
.communicate(**cargs
)
36 except TimeoutExpired
:
37 if kwargs
.get('start_new_session') and hasattr(os
, 'killpg'):
38 os
.killpg(proc
.pid
, signal
.SIGKILL
)
41 out
, err
= proc
.communicate()
42 exc
= TimeoutExpired(proc
.args
, timeout
=cargs
['timeout'], output
=out
)
46 except Exception as e
:
47 exc_type
, exc_value
, tb
= sys
.exc_info()
48 exc_lines
= traceback
.format_exception(exc_type
, exc_value
, tb
)
49 trace
= str(cmd
) + '\n' + ''.join(exc_lines
)
50 ex
= e
.__class
__.__name
__
52 # it is just text so maybe we do not need to pickle()
53 tmp
= [ret
, out
, err
, ex
, trace
]
54 obj
= base64
.b64encode(cPickle
.dumps(tmp
))
55 sys
.stdout
.write(obj
.decode())
56 sys
.stdout
.write('\n')
62 except KeyboardInterrupt: