gen_java_codenav_entry.py 3.2 KB

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