collect_java_srcs.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import sys
  3. import contextlib
  4. import tarfile
  5. import zipfile
  6. if __name__ == '__main__':
  7. build_root = sys.argv[1]
  8. root = os.path.normpath(sys.argv[2])
  9. dest = os.path.normpath(sys.argv[3])
  10. srcs = sys.argv[4:]
  11. for src in srcs:
  12. src = os.path.normpath(src)
  13. if src.endswith('.java') or src.endswith('.kt'):
  14. src_rel_path = os.path.relpath(src, root)
  15. if os.path.join(root, src_rel_path) == src:
  16. # Inside root
  17. dst = os.path.join(dest, src_rel_path)
  18. else:
  19. # Outside root
  20. print('External src file "{}" is outside of srcdir {}, ignore'.format(
  21. os.path.relpath(src, build_root),
  22. os.path.relpath(root, build_root),
  23. )
  24. continue
  25. if os.path.exists(dst):
  26. print >> sys.stderr, 'Duplicate external src file {}, choice is undefined'.format(
  27. os.path.relpath(dst, root)
  28. )
  29. else:
  30. destdir = os.path.dirname(dst)
  31. if destdir and not os.path.exists(destdir):
  32. os.makedirs(destdir)
  33. os.rename(src, dst)
  34. elif src.endswith('.jsr'):
  35. with contextlib.closing(tarfile.open(src, 'r')) as tf:
  36. tf.extractall(dst)
  37. elif src.endswith('-sources.jar'):
  38. with zipfile.ZipFile(src) as zf:
  39. zf.extractall(dst)
  40. else:
  41. print >> sys.stderr, 'Unrecognized file type', os.path.relpath(src, build_root)