wrapcc.py 914 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. import time
  5. import subprocess
  6. def need_retry(text):
  7. return 'Stack dump' in text
  8. def retry_inf(cmd):
  9. while True:
  10. try:
  11. yield subprocess.check_output(cmd, stderr=subprocess.STDOUT), None
  12. except subprocess.CalledProcessError as e:
  13. yield e.output, e
  14. def retry(cmd):
  15. for n, (out, err) in enumerate(retry_inf(cmd)):
  16. if out:
  17. sys.stderr.write(out)
  18. if n > 5:
  19. raise Exception('all retries failed')
  20. elif need_retry(out):
  21. time.sleep(1 + n)
  22. elif err:
  23. raise err
  24. else:
  25. return
  26. if __name__ == '__main__':
  27. cmd = sys.argv[1:]
  28. if '-c' in cmd:
  29. try:
  30. retry(cmd)
  31. except subprocess.CalledProcessError as e:
  32. sys.exit(e.returncode)
  33. else:
  34. os.execv(cmd[0], cmd)