build_java_codenav_index.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import sys
  2. import re
  3. import os
  4. import subprocess
  5. FAKE_ARCADIA_ROOT = 'fake_arcadia_root'
  6. FAKE_BUILD_ROOT = 'fake_build_root'
  7. def modify_sources_file(origin, target, source_roots_map):
  8. def _cut_source_root(src):
  9. for pref, fake_root in source_roots_map.items():
  10. if src.startswith(pref):
  11. return os.path.join(fake_root, os.path.relpath(src, pref))
  12. return src
  13. with open(origin) as o:
  14. srcs = [i for line in o for i in re.split('\\s+', line) if i]
  15. new_srcs = map(_cut_source_root, srcs)
  16. with open(target, 'w') as t:
  17. t.write(' '.join(new_srcs))
  18. def just_do_it(argv):
  19. corpus_name, build_root, arcadia_root, sources_file, javac_tail_cmd = argv[0], argv[1], argv[2], argv[3], argv[4:]
  20. fake_arcadia_root = os.path.join(build_root, FAKE_ARCADIA_ROOT)
  21. fake_build_root = os.path.join(build_root, FAKE_BUILD_ROOT)
  22. fake_source_roots = {
  23. arcadia_root: fake_arcadia_root,
  24. build_root: fake_build_root,
  25. }
  26. modify_sources_file(sources_file, os.path.join(os.path.dirname(sources_file), '_' + os.path.basename(sources_file)), fake_source_roots)
  27. kindex_data_root = '{}/kindex'.format(os.path.join(build_root, os.path.dirname(corpus_name)))
  28. if not os.path.exists(kindex_data_root):
  29. os.makedirs(kindex_data_root)
  30. env = os.environ.copy()
  31. env['KYTHE_ROOT_DIRECTORY'] = build_root
  32. env['KYTHE_OUTPUT_DIRECTORY'] = kindex_data_root
  33. env['KYTHE_CORPUS'] = os.path.relpath(corpus_name, build_root)
  34. os.symlink(arcadia_root, fake_arcadia_root)
  35. os.symlink(build_root, fake_build_root)
  36. try:
  37. subprocess.check_call(javac_tail_cmd, env=env)
  38. finally:
  39. os.unlink(fake_arcadia_root)
  40. os.unlink(fake_build_root)
  41. if __name__ == '__main__':
  42. just_do_it(sys.argv[1:])