make_java_srclists.py 4.0 KB

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