Created
September 14, 2016 22:53
-
-
Save rberrelleza/724d7eaaaee63da31a9264ca589cfb01 to your computer and use it in GitHub Desktop.
Sort junit test files by the provided attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import sys | |
import xml.etree.ElementTree as ET | |
def getChildValue(val): | |
try: | |
return float(val) | |
except: | |
return val | |
def sortchildrenby(parent, attr): | |
parent[:] = sorted(parent, key=lambda child: getChildValue(child.get(attr))) | |
parser = argparse.ArgumentParser(description='Sort junit files') | |
parser.add_argument('--field', dest='field', default='time', | |
help='sort tests by (default: time)') | |
args = parser.parse_args() | |
lines = sys.stdin.readlines() | |
tree = ET.fromstring("".join(lines)) | |
sortchildrenby(tree, args.field) | |
for child in tree: | |
sortchildrenby(child, args.field) | |
print(ET.tostring(tree, encoding='utf8', method='xml')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment