yndexer.py 2.0 KB

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