~unity-team/bamf/x-sru2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/python
from argparse import ArgumentParser
import libxslt
import libxml2
import sys
import os

XSL_TRANSFORM='/usr/share/gtester2xunit/gtester.xsl'

def transform_file(input_filename, output_filename, xsl_file):
    gtester = libxml2.parseFile(xsl_file)
    style = libxslt.parseStylesheetDoc(gtester)
    doc = libxml2.parseFile(input_filename)
    result = style.applyStylesheet(doc, None)
    result.saveFormatFile(filename=output_filename, format=True)


def get_output_filename(input_filename):
    filename, extension = os.path.splitext(input_filename)
    return '{filename}-xunit{extension}'.format(filename=filename,
                                                 extension=extension)

def main():
    parser = ArgumentParser(
            description="Simple utility that converts xml output of " +
                        "gtester to xunit output for jenkins")
    parser.add_argument('-o', '--output',
            help="Write output to specified file instead of default")
    parser.add_argument('-x', '--xsl',
            help="xsl file that should be used for the transformation")
    parser.add_argument('-i', '--in-place',
            help="Write the ouput to the original file (i.e. replace " +
            "the original xml)",
            action='store_true',
            default=False)
    parser.add_argument('files', metavar='FILE', type=str, nargs='+',
            help="file(s) to transform to xunit format")
    args = vars(parser.parse_args())
    for input_filename in args['files']:
        if not args['output']:
            if args['in_place']:
                output_filename = input_filename
            else:
                output_filename = get_output_filename(input_filename)
        else:
            output_filename = args['output']
        if not args['xsl']:
            xsl = XSL_TRANSFORM
        else:
            xsl = args['xsl']
        transform_file(input_filename, output_filename, xsl)

sys.exit(main())
# vim: set syntex=python: