split-junit.py 1022 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import argparse
  5. from pathlib import Path
  6. from xml.etree import ElementTree as ET
  7. def save_suite(suite, path):
  8. root = ET.Element("testsuites")
  9. root.append(suite)
  10. tree = ET.ElementTree(root)
  11. tree.write(path)
  12. def do_split(fn, out_dir):
  13. try:
  14. tree = ET.parse(fn)
  15. except ET.ParseError as e:
  16. print(f"Unable to parse {fn}: {e}", file=sys.stderr)
  17. sys.exit(1)
  18. root = tree.getroot()
  19. for n, suite in enumerate(root.iter('testsuite')):
  20. part_fn = Path(out_dir).joinpath(f"part_{n}.xml")
  21. print(f"write {part_fn}")
  22. save_suite(suite, part_fn)
  23. def main():
  24. parser = argparse.ArgumentParser()
  25. parser.add_argument('-o', dest='out_dir', required=True)
  26. parser.add_argument("in_file", type=argparse.FileType("r"))
  27. args = parser.parse_args()
  28. if not os.path.isdir(args.out_dir):
  29. os.makedirs(args.out_dir)
  30. do_split(args.in_file, args.out_dir)
  31. if __name__ == '__main__':
  32. main()