junit-postprocess.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. import argparse
  3. import glob
  4. import os
  5. import re
  6. import xml.etree.ElementTree as ET
  7. from mute_utils import MuteTestCheck, mute_target, recalc_suite_info
  8. shard_suffix_re = re.compile(r"-\d+$")
  9. def update_testname(fn, testcase):
  10. shardname = os.path.splitext(os.path.basename(fn))[0]
  11. shardname = shard_suffix_re.sub("", shardname)
  12. clsname = testcase.get("classname")
  13. tstname = testcase.get("name")
  14. testcase.set("classname", shardname)
  15. testcase.set("name", f"{clsname}::{tstname}")
  16. testcase.set("id", f"{shardname}_{clsname}_{tstname}")
  17. return f"{shardname}/{clsname}::{tstname}"
  18. def postprocess_yunit(fn, mute_check: MuteTestCheck, dry_run):
  19. try:
  20. tree = ET.parse(fn)
  21. except ET.ParseError as e:
  22. print(f"Unable to parse {fn}: {e}")
  23. return
  24. root = tree.getroot()
  25. for testsuite in root.findall("testsuite"):
  26. need_recalc = False
  27. for testcase in testsuite.findall("testcase"):
  28. new_name = update_testname(fn, testcase)
  29. if mute_check(new_name) and mute_target(testcase):
  30. print(f"mute {new_name}")
  31. need_recalc = True
  32. if need_recalc:
  33. recalc_suite_info(testsuite)
  34. print(f"{'(dry-run) ' if dry_run else ''}save {fn}")
  35. if not dry_run:
  36. tree.write(fn, xml_declaration=True, encoding="UTF-8")
  37. def main():
  38. parser = argparse.ArgumentParser()
  39. parser.add_argument("--filter-file", required=True)
  40. parser.add_argument("--dry-run", action="store_true", default=False)
  41. parser.add_argument("yunit_path")
  42. args = parser.parse_args()
  43. if not os.path.isdir(args.yunit_path):
  44. print(f"{args.yunit_path} is not a directory, exit")
  45. raise SystemExit(-1)
  46. mute_check = MuteTestCheck(args.filter_file)
  47. for fn in glob.glob(os.path.join(args.yunit_path, "*.xml")):
  48. postprocess_yunit(fn, mute_check, args.dry_run)
  49. if __name__ == "__main__":
  50. main()