resolve_java_srcs.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import os
  2. import argparse
  3. import re
  4. import sys
  5. def list_all_files(directory, prefix='/', hidden_files=False):
  6. result = []
  7. if os.path.exists(directory):
  8. for i in os.listdir(directory):
  9. abs_path = os.path.join(directory, i)
  10. result += (
  11. list_all_files(os.path.join(directory, abs_path), prefix + i + '/', hidden_files)
  12. if os.path.isdir(abs_path)
  13. else ([prefix + i] if (hidden_files or not i.startswith('.')) else [])
  14. )
  15. return result
  16. def pattern_to_regexp(p):
  17. return (
  18. '^'
  19. + ('/' if not p.startswith('**') else '')
  20. + re.escape(p)
  21. .replace(r'\*\*\/', '[_DIR_]')
  22. .replace(r'\*', '[_FILE_]')
  23. .replace('[_DIR_]', '(.*/)?')
  24. .replace('[_FILE_]', '([^/]*)')
  25. + '$'
  26. )
  27. def resolve_java_srcs(
  28. srcdir, include_patterns, exclude_patterns, all_resources, resolve_kotlin=False, resolve_groovy=False
  29. ):
  30. result = {'java': [], 'not_java': [], 'kotlin': [], 'groovy': []}
  31. include_patterns_normal, include_patterns_hidden, exclude_patterns_normal, exclude_patterns_hidden = [], [], [], []
  32. for vis, hid, patterns in (
  33. (include_patterns_normal, include_patterns_hidden, include_patterns),
  34. (exclude_patterns_normal, exclude_patterns_hidden, exclude_patterns),
  35. ):
  36. for pattern in patterns:
  37. if (pattern if pattern.find('/') == -1 else pattern.rsplit('/', 1)[1]).startswith('.'):
  38. hid.append(pattern)
  39. else:
  40. vis.append(pattern)
  41. re_patterns = map(pattern_to_regexp, vis + hid)
  42. if sys.platform in ('win32', 'darwin'):
  43. re_patterns = [re.compile(i, re.IGNORECASE) for i in re_patterns]
  44. else:
  45. re_patterns = [re.compile(i) for i in re_patterns]
  46. vis[:], hid[:] = re_patterns[: len(vis)], re_patterns[len(vis) :]
  47. for inc_patterns, exc_patterns, with_hidden_files in (
  48. (include_patterns_normal, exclude_patterns_normal, False),
  49. (include_patterns_hidden, exclude_patterns_hidden, True),
  50. ):
  51. for f in list_all_files(srcdir, hidden_files=with_hidden_files):
  52. excluded = False
  53. for exc_re in exc_patterns:
  54. if exc_re.match(f):
  55. excluded = True
  56. break
  57. if excluded:
  58. continue
  59. for inc_re in inc_patterns:
  60. if inc_re.match(f):
  61. s = os.path.normpath(f[1:])
  62. if all_resources or not (f.endswith('.java') or f.endswith('.kt') or f.endswith('.groovy')):
  63. result['not_java'].append(s)
  64. elif f.endswith('.java'):
  65. result['java'].append(os.path.join(srcdir, s))
  66. elif f.endswith('.kt') and resolve_kotlin:
  67. result['kotlin'].append(os.path.join(srcdir, s))
  68. elif f.endswith('.groovy') and resolve_groovy:
  69. result['groovy'].append(os.path.join(srcdir, s))
  70. else:
  71. result['not_java'].append(s)
  72. break
  73. return sorted(result['java']), sorted(result['not_java']), sorted(result['kotlin']), sorted(result['groovy'])
  74. def do_it(
  75. directory,
  76. sources_file,
  77. resources_file,
  78. kotlin_sources_file,
  79. groovy_sources_file,
  80. include_patterns,
  81. exclude_patterns,
  82. resolve_kotlin,
  83. resolve_groovy,
  84. append,
  85. all_resources,
  86. ):
  87. j, r, k, g = resolve_java_srcs(
  88. directory, include_patterns, exclude_patterns, all_resources, resolve_kotlin, resolve_groovy
  89. )
  90. mode = 'a' if append else 'w'
  91. open(sources_file, mode).writelines(i + '\n' for i in j)
  92. open(resources_file, mode).writelines(i + '\n' for i in r)
  93. if kotlin_sources_file:
  94. open(kotlin_sources_file, mode).writelines(i + '\n' for i in k + j)
  95. if groovy_sources_file:
  96. open(groovy_sources_file, mode).writelines(i + '\n' for i in g + j)
  97. if __name__ == '__main__':
  98. parser = argparse.ArgumentParser()
  99. parser.add_argument('-d', '--directory', required=True)
  100. parser.add_argument('-s', '--sources-file', required=True)
  101. parser.add_argument('-r', '--resources-file', required=True)
  102. parser.add_argument('-k', '--kotlin-sources-file', default=None)
  103. parser.add_argument('-g', '--groovy-sources-file', default=None)
  104. parser.add_argument('--append', action='store_true', default=False)
  105. parser.add_argument('--all-resources', action='store_true', default=False)
  106. parser.add_argument('--resolve-kotlin', action='store_true', default=False)
  107. parser.add_argument('--resolve-groovy', action='store_true', default=False)
  108. parser.add_argument('--include-patterns', nargs='*', default=[])
  109. parser.add_argument('--exclude-patterns', nargs='*', default=[])
  110. args = parser.parse_args()
  111. do_it(**vars(args))