yndexer.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import sys
  2. import subprocess
  3. import threading
  4. import os
  5. import re
  6. rx_resource_dir = re.compile(r'libraries: =([^:]*)')
  7. def _try_to_kill(process):
  8. try:
  9. process.kill()
  10. except Exception:
  11. pass
  12. def touch(path):
  13. if not os.path.exists(path):
  14. with open(path, 'w'):
  15. pass
  16. class Process(object):
  17. def __init__(self, args):
  18. self._process = subprocess.Popen(args)
  19. self._event = threading.Event()
  20. self._result = None
  21. thread = threading.Thread(target=self._run)
  22. thread.setDaemon(True)
  23. thread.start()
  24. def _run(self):
  25. self._process.communicate()
  26. self._result = self._process.returncode
  27. self._event.set()
  28. def wait(self, timeout):
  29. self._event.wait(timeout=timeout)
  30. _try_to_kill(self._process)
  31. return self._result
  32. if __name__ == '__main__':
  33. args = sys.argv
  34. yndexer = args[1]
  35. timeout = int(args[2])
  36. arc_root = args[3]
  37. build_root = args[4]
  38. input_file = args[5]
  39. output_file = args[-1]
  40. tail_args = args[6:-1]
  41. subprocess.check_call(tail_args)
  42. clang = tail_args[0]
  43. out = subprocess.check_output([clang, '-print-search-dirs'])
  44. resource_dir = rx_resource_dir.search(out).group(1)
  45. yndexer_args = [
  46. yndexer, input_file,
  47. '-pb2',
  48. '-i', 'arc::{}'.format(arc_root),
  49. '-i', 'build::{}'.format(build_root),
  50. '-i', '.IGNORE::/',
  51. '-o', os.path.dirname(output_file),
  52. '-n', os.path.basename(output_file).rsplit('.ydx.pb2', 1)[0],
  53. '--'
  54. ] + tail_args + [
  55. '-resource-dir', resource_dir,
  56. ]
  57. process = Process(yndexer_args)
  58. result = process.wait(timeout=timeout)
  59. if result != 0:
  60. print >> sys.stderr, 'Yndexing process finished with code', result
  61. touch(output_file)