From 4344914bb3da582d4ed7d73d08c8a274033b5430 Mon Sep 17 00:00:00 2001 From: Joe McDonnell Date: Wed, 13 Feb 2019 12:06:24 -0800 Subject: [PATCH] IMPALA-8193: Fix python 2.6 issue in junit_prune_notrun.py Python 2.6's ElementTree.write() does not have an xml_declaration argument, so junitxml_prune_notrun.py fails on python 2.6. This fixes junitxml_prune_notrun.py by using minidom to write the output. This mirrors how bin/generate_junitxml.py outputs XML. Verified that tests now pass on python 2.6 and python 2.7 does not change. Change-Id: I9ef8fb77b1ac8c51e3dfb6b04690ae9ccc490d62 Reviewed-on: http://gerrit.cloudera.org:8080/12479 Reviewed-by: Impala Public Jenkins Tested-by: Impala Public Jenkins --- .../impala_py_lib/jenkins/junitxml_prune_notrun.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/python/impala_py_lib/jenkins/junitxml_prune_notrun.py b/lib/python/impala_py_lib/jenkins/junitxml_prune_notrun.py index 9d9d92107..98131a7fa 100755 --- a/lib/python/impala_py_lib/jenkins/junitxml_prune_notrun.py +++ b/lib/python/impala_py_lib/jenkins/junitxml_prune_notrun.py @@ -22,11 +22,12 @@ Some tests that produce JUnitXML include tests that did not run (i.e. status="no This script walks through the JUnitXML and removes these elements. """ from optparse import OptionParser -from xml.etree.ElementTree import ElementTree +from xml.etree import ElementTree as ET +from xml.dom import minidom def junitxml_prune_notrun(junitxml_filename): - tree = ElementTree() + tree = ET.ElementTree() root = tree.parse(junitxml_filename) for testsuite in root.findall("testsuite"): @@ -52,7 +53,12 @@ def junitxml_prune_notrun(junitxml_filename): # Fixup the total number of tests testsuite.attrib["tests"] = str(num_tests - len(notrun_testcases)) # Write out the pruned JUnitXML - tree.write(junitxml_filename, encoding="utf-8", xml_declaration=True) + # An XML declaration is optional, but it is nice to have. ElementTree.write() does + # not support an XML declaration on Python 2.6, so use minidom to write the XML. + root_node_minidom = minidom.parseString(ET.tostring(root)) + junitxml_string = root_node_minidom.toxml(encoding="utf-8") + with open(junitxml_filename, "w") as f: + f.write(junitxml_string) def main():