yndexer.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. [
  47. yndexer,
  48. input_file,
  49. '-pb2',
  50. '-i',
  51. 'arc::{}'.format(arc_root),
  52. '-i',
  53. 'build::{}'.format(build_root),
  54. '-i',
  55. '.IGNORE::/',
  56. '-o',
  57. os.path.dirname(output_file),
  58. '-n',
  59. os.path.basename(output_file).rsplit('.ydx.pb2', 1)[0],
  60. '--',
  61. ]
  62. + tail_args
  63. + [
  64. '-resource-dir',
  65. resource_dir,
  66. ]
  67. )
  68. process = Process(yndexer_args)
  69. result = process.wait(timeout=timeout)
  70. if result != 0:
  71. print >> sys.stderr, 'Yndexing process finished with code', result
  72. touch(output_file)