gen_java_codenav_entry.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import print_function
  2. import argparse
  3. import datetime
  4. import os
  5. import subprocess
  6. import sys
  7. import tarfile
  8. def extract_kindexes(kindexes):
  9. for kindex in kindexes:
  10. with tarfile.TarFile(kindex) as tf:
  11. for fname in [i for i in tf.getnames() if i.endswith('.kzip')]:
  12. tf.extract(fname)
  13. yield fname
  14. def just_do_it(java, kythe, kythe_to_proto, out_name, binding_only, kindexes):
  15. temp_out_name = out_name + '.temp'
  16. kindex_inputs = list(extract_kindexes(kindexes))
  17. open(temp_out_name, 'w').close()
  18. start = datetime.datetime.now()
  19. for kindex in kindex_inputs:
  20. print('[INFO] Processing:', kindex, file=sys.stderr)
  21. indexer_start = datetime.datetime.now()
  22. p = subprocess.Popen(
  23. [java, '-jar', os.path.join(kythe, 'indexers/java_indexer.jar'), kindex], stdout=subprocess.PIPE
  24. )
  25. indexer_out, _ = p.communicate()
  26. print('[INFO] Indexer execution time:', (
  27. datetime.datetime.now() - indexer_start
  28. ).total_seconds(), 'seconds', file=sys.stderr)
  29. if p.returncode:
  30. raise Exception('java_indexer failed with exit code {}'.format(p.returncode))
  31. dedup_start = datetime.datetime.now()
  32. p = subprocess.Popen([os.path.join(kythe, 'tools/dedup_stream')], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  33. dedup_out, _ = p.communicate(indexer_out)
  34. print('[INFO] Dedup execution time:', (
  35. datetime.datetime.now() - dedup_start
  36. ).total_seconds(), 'seconds', file=sys.stderr)
  37. if p.returncode:
  38. raise Exception('dedup_stream failed with exit code {}'.format(p.returncode))
  39. entrystream_start = datetime.datetime.now()
  40. p = subprocess.Popen(
  41. [os.path.join(kythe, 'tools/entrystream'), '--write_json'],
  42. stdin=subprocess.PIPE,
  43. stdout=open(temp_out_name, 'a'),
  44. )
  45. p.communicate(dedup_out)
  46. if p.returncode:
  47. raise Exception('entrystream failed with exit code {}'.format(p.returncode))
  48. print('[INFO] Entrystream execution time:', (
  49. datetime.datetime.now() - entrystream_start
  50. ).total_seconds(), 'seconds', file=sys.stderr)
  51. preprocess_start = datetime.datetime.now()
  52. subprocess.check_call(
  53. [kythe_to_proto, '--preprocess-entry', '--entries', temp_out_name, '--out', out_name]
  54. + (['--only-binding-data'] if binding_only else [])
  55. )
  56. print('[INFO] Preprocessing execution time:', (
  57. datetime.datetime.now() - preprocess_start
  58. ).total_seconds(), 'seconds', file=sys.stderr)
  59. print('[INFO] Total execution time:', (datetime.datetime.now() - start).total_seconds(), 'seconds', file=sys.stderr)
  60. if __name__ == '__main__':
  61. parser = argparse.ArgumentParser()
  62. parser.add_argument("--java", help="java path")
  63. parser.add_argument("--kythe", help="kythe path")
  64. parser.add_argument("--kythe-to-proto", help="kythe_to_proto tool path")
  65. parser.add_argument("--out-name", help="entries json out name")
  66. parser.add_argument("--binding-only", action="store_true", default=False, help="filter only binding data")
  67. parser.add_argument("kindexes", nargs='*')
  68. args = parser.parse_args()
  69. just_do_it(args.java, args.kythe, args.kythe_to_proto, args.out_name, args.binding_only, args.kindexes)