resolve_java_srcs.py 4.8 KB

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