mongo/buildscripts/parse_test_xml.py
Steve McClure 12c0c32f12 SERVER-111373 parse test.xml more robustly with better diagnostics (#41734)
GitOrigin-RevId: a3cb25fc5dedbc89e817cc4a431d9a6941465a56
2025-09-24 20:26:55 +00:00

26 lines
770 B
Python

import os
import xml.etree.ElementTree as ET
def parse_test_xml(xml_file: str) -> ET.ElementTree:
"""Parse the test.xml file and return the ElementTree object. Throws with diagnostics if unparsable."""
# Check if file exists and has content
if not os.path.exists(xml_file):
raise Exception(f"Failed to parse {xml_file}: file does not exist")
with open(xml_file, "r", encoding="utf-8") as f:
content = f.read().strip()
if not content:
raise Exception(f"Failed to parse {xml_file}: file is empty")
try:
root = ET.fromstring(content)
return ET.ElementTree(root)
except (ET.ParseError, UnicodeDecodeError) as e:
print(f"Failed to parse {xml_file}: {e}")
print(content)
raise