make_java_srclists.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import os
  2. import sys
  3. import argparse
  4. # Explicitly enable local imports
  5. # Don't forget to add imported scripts to inputs of the calling command!
  6. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  7. import process_command_files as pcf
  8. import java_pack_to_file as jcov
  9. def writelines(f, rng):
  10. f.writelines(item + '\n' for item in rng)
  11. def add_rel_src_to_coverage(coverage, src, source_root):
  12. rel = os.path.relpath(src, source_root)
  13. if not rel.startswith('..' + os.path.sep):
  14. coverage.append(rel)
  15. def main():
  16. args = pcf.get_args(sys.argv[1:])
  17. parser = argparse.ArgumentParser()
  18. parser.add_argument('--moddir')
  19. parser.add_argument('--java')
  20. parser.add_argument('--groovy')
  21. parser.add_argument('--kotlin')
  22. parser.add_argument('--coverage')
  23. parser.add_argument('--source-root')
  24. args, remaining_args = parser.parse_known_args(args)
  25. java = []
  26. kotlin = []
  27. groovy = []
  28. coverage = []
  29. cur_resources_list_file = None
  30. cur_jsources_list_file = None
  31. cur_srcdir = None
  32. cur_resources = []
  33. cur_jsources = []
  34. FILE_ARG = 1
  35. RESOURCES_DIR_ARG = 2
  36. SRCDIR_ARG = 3
  37. JSOURCES_DIR_ARG = 4
  38. next_arg = FILE_ARG
  39. for src in remaining_args:
  40. if next_arg == RESOURCES_DIR_ARG:
  41. assert cur_resources_list_file is None
  42. cur_resources_list_file = src
  43. next_arg = FILE_ARG
  44. continue
  45. elif next_arg == JSOURCES_DIR_ARG:
  46. assert cur_jsources_list_file is None
  47. cur_jsources_list_file = src
  48. next_arg = FILE_ARG
  49. continue
  50. elif next_arg == SRCDIR_ARG:
  51. assert cur_srcdir is None
  52. cur_srcdir = src if os.path.isabs(src) else os.path.join(args.moddir, src)
  53. next_arg = FILE_ARG
  54. continue
  55. if src.endswith(".java"):
  56. java.append(src)
  57. kotlin.append(src)
  58. if args.coverage and args.source_root:
  59. add_rel_src_to_coverage(coverage, src, args.source_root)
  60. elif args.kotlin and src.endswith(".kt"):
  61. kotlin.append(src)
  62. if args.coverage and args.source_root:
  63. add_rel_src_to_coverage(coverage, src, args.source_root)
  64. elif args.groovy and src.endswith(".groovy"):
  65. groovy.append(src)
  66. else:
  67. if src == '--resources':
  68. if cur_resources_list_file is not None:
  69. with open(cur_resources_list_file, 'w') as f:
  70. writelines(f, cur_resources)
  71. cur_resources_list_file = None
  72. cur_srcdir = None
  73. cur_resources = []
  74. next_arg = RESOURCES_DIR_ARG
  75. continue
  76. if src == '--jsources':
  77. if cur_jsources_list_file is not None:
  78. with open(cur_jsources_list_file, 'w') as f:
  79. writelines(f, cur_jsources)
  80. cur_jsources_list_file = None
  81. cur_jsources = []
  82. next_arg = JSOURCES_DIR_ARG
  83. continue
  84. elif src == '--srcdir':
  85. next_arg = SRCDIR_ARG
  86. continue
  87. else:
  88. assert cur_srcdir is not None and cur_resources_list_file is not None
  89. cur_resources.append(os.path.relpath(src, cur_srcdir))
  90. if cur_jsources_list_file is not None:
  91. assert cur_srcdir is not None
  92. cur_jsources.append(os.path.relpath(src, cur_srcdir))
  93. if cur_resources_list_file is not None:
  94. with open(cur_resources_list_file, 'w') as f:
  95. writelines(f, cur_resources)
  96. if cur_jsources_list_file is not None:
  97. with open(cur_jsources_list_file, 'w') as f:
  98. writelines(f, cur_jsources)
  99. if args.java:
  100. with open(args.java, 'w') as f:
  101. writelines(f, java)
  102. if args.kotlin:
  103. with open(args.kotlin, 'w') as f:
  104. writelines(f, kotlin)
  105. if args.groovy:
  106. with open(args.groovy, 'w') as f:
  107. writelines(f, groovy)
  108. if args.coverage:
  109. jcov.write_coverage_sources(args.coverage, args.source_root, coverage)
  110. return 0
  111. if __name__ == '__main__':
  112. sys.exit(main())