jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6KB

  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2016-2018 (ita)
  4. import os, sys, traceback, base64, signal
  5. try:
  6. import cPickle
  7. except ImportError:
  8. import pickle as cPickle
  9. try:
  10. import subprocess32 as subprocess
  11. except ImportError:
  12. import subprocess
  13. try:
  14. TimeoutExpired = subprocess.TimeoutExpired
  15. except AttributeError:
  16. class TimeoutExpired(Exception):
  17. pass
  18. def run():
  19. txt = sys.stdin.readline().strip()
  20. if not txt:
  21. # parent process probably ended
  22. sys.exit(1)
  23. [cmd, kwargs, cargs] = cPickle.loads(base64.b64decode(txt))
  24. cargs = cargs or {}
  25. if not 'close_fds' in kwargs:
  26. # workers have no fds
  27. kwargs['close_fds'] = False
  28. ret = 1
  29. out, err, ex, trace = (None, None, None, None)
  30. try:
  31. proc = subprocess.Popen(cmd, **kwargs)
  32. try:
  33. out, err = proc.communicate(**cargs)
  34. except TimeoutExpired:
  35. if kwargs.get('start_new_session') and hasattr(os, 'killpg'):
  36. os.killpg(proc.pid, signal.SIGKILL)
  37. else:
  38. proc.kill()
  39. out, err = proc.communicate()
  40. exc = TimeoutExpired(proc.args, timeout=cargs['timeout'], output=out)
  41. exc.stderr = err
  42. raise exc
  43. ret = proc.returncode
  44. except Exception as e:
  45. exc_type, exc_value, tb = sys.exc_info()
  46. exc_lines = traceback.format_exception(exc_type, exc_value, tb)
  47. trace = str(cmd) + '\n' + ''.join(exc_lines)
  48. ex = e.__class__.__name__
  49. # it is just text so maybe we do not need to pickle()
  50. tmp = [ret, out, err, ex, trace]
  51. obj = base64.b64encode(cPickle.dumps(tmp))
  52. sys.stdout.write(obj.decode())
  53. sys.stdout.write('\n')
  54. sys.stdout.flush()
  55. while 1:
  56. try:
  57. run()
  58. except KeyboardInterrupt:
  59. break