java_pack_to_file.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. import re
  3. import optparse
  4. JAVA_PACKAGE_REGEX = re.compile(r'^\s*package\s+(.*?);', flags=re.MULTILINE | re.DOTALL)
  5. KOTLIN_PACKAGE_REGEX = re.compile(r'^\s*package\s+(.*?)^', flags=re.MULTILINE | re.DOTALL)
  6. def parse_args():
  7. parser = optparse.OptionParser()
  8. parser.add_option('-o', '--output')
  9. parser.add_option('-a', '--source-root', dest='source_root')
  10. return parser.parse_args()
  11. def get_package_name(filename):
  12. with open(filename) as afile:
  13. content = afile.read()
  14. if filename.endswith(".kt"):
  15. match = KOTLIN_PACKAGE_REGEX.search(content)
  16. if match:
  17. return match.group(1).strip().replace('.', '/')
  18. else:
  19. match = JAVA_PACKAGE_REGEX.search(content)
  20. if match:
  21. return match.group(1).replace('\n\t ', '').replace('.', '/')
  22. return ''
  23. def write_coverage_sources(output, srcroot, files):
  24. with open(output, 'w') as afile:
  25. for filename in files:
  26. pname = get_package_name(os.path.join(srcroot, filename))
  27. afile.write(os.path.join(pname, os.path.basename(filename)) + ':' + filename + '\n')
  28. def main():
  29. opts, files = parse_args()
  30. write_coverage_sources(opts.output, opts.source_root, files)
  31. if __name__ == '__main__':
  32. exit(main())