summaryrefslogtreecommitdiff
path: root/contrib/coveragehelper/merge-cobertura.py
blob: 979f42b23b0cf748ab741310152d6a07057a1fc0 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
"""
Cobertura XML report merger

Written for merging OpenCppCoverage reports, thus not completely supporting all
Cobertura XML attributes, only line coverage which OpenCppCoverage generates.
Anything else is simply discarded in the process.

Copyright (C) 2017 Michal Cihar <michal@cihar.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
import argparse
import glob
import sys
try:
    from xml.etree.cElementTree import (
        iterparse, Element, SubElement, ElementTree,
    )
except ImportError:
    from xml.etree.ElementTree import (
        iterparse, Element, SubElement, ElementTree,
    )

HEADER = '''<?xml version="1.0"?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
'''


def log(message=''):
    """Log message to strderr"""
    sys.stderr.write(message)
    sys.stderr.write('\n')


def read_files(names):
    """Read coverage data from input files"""
    result = {}
    lines = {}
    outfile = None
    log('Reading files:')

    for filename in names:
        log('* {}'.format(filename))
        for event, elem in iterparse(filename, events=('start', 'end')):
            if elem.tag == 'class' and event == 'start':
                outfile = elem.get('filename')
                lines = result.get(outfile, {})
            elif elem.tag == 'line' and event == 'end':
                line = elem.get('number')
                lines[line] = lines.get(line, 0) + int(elem.get('hits'))
            elif elem.tag == 'class' and event == 'end':
                result[outfile] = lines
    log()

    return result


def get_line_rates(data):
    """Calculate line hit rates from raw coverage data"""
    result = {}
    total_lines = 0
    total_hits = 0
    log('Counting line rates:')
    for item in data:
        lines = len(data[item])
        hits = sum([1 for x in data[item].values() if x])
        result[item] = 1.0 * hits / lines
        log(' * {0} = {1} ({2} / {3})'.format(item, result[item], hits, lines))
        total_lines += lines
        total_hits += hits

    result['_'] = 1.0 * total_hits / total_lines
    result['_hits'] = total_hits
    result['_lines'] = total_lines

    return result


def write_data(data, handle):
    """Write Cobertura XML for coverage data"""
    line_rates = get_line_rates(data)

    log('Generating output...')
    root = Element('coverage')
    root.set('line-rate', str(line_rates['_']))
    root.set('branch-rate', '0')
    root.set('complexity', '0')
    root.set('branches-covered', '0')
    root.set('branches-valid', '0')
    root.set('timestamp', '0')
    root.set('lines-covered', str(line_rates['_hits']))
    root.set('lines-valid', str(line_rates['_lines']))
    root.set('version', '0')

    sources = SubElement(root, 'sources')
    source = SubElement(sources, 'source')
    source.text = 'c:'

    packages = SubElement(root, 'packages')
    package = SubElement(packages, 'package')
    package.set('name', 'gammu.exe')
    package.set('line-rate', str(line_rates['_']))
    package.set('branch-rate', '0')
    package.set('complexity', '0')
    classes = SubElement(package, 'classes')

    for item in data:
        obj = SubElement(classes, 'class')
        obj.set('name', item.rsplit('\\', 1)[1])
        obj.set('filename', item)
        obj.set('line-rate', str(line_rates[item]))
        obj.set('branch-rate', '0')
        obj.set('complexity', '0')
        SubElement(obj, 'methods')
        lines = SubElement(obj, 'lines')
        for line in sorted(data[item], key=lambda x: int(x)):
            obj = SubElement(lines, 'line')
            obj.set('number', line)
            obj.set('hits', str(data[item][line]))

    tree = ElementTree(root)

    handle.write(HEADER)

    tree.write(handle, xml_declaration=False)


def main():
    """Command line interface"""
    parser = argparse.ArgumentParser(
        description=sys.modules[__name__].__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        '-m', '--match',
        help='wildcard to match files to process'
    )
    parser.add_argument(
        '-o', '--output',
        help='output file, stdout used if omitted'
    )
    parser.add_argument(
        'file',
        nargs='*',
        help='files to process'
    )
    args = parser.parse_args()
    if args.match:
        files = glob.glob(args.match)
    else:
        files = args.file

    result = read_files(files)

    if args.output:
        with open(args.output, 'w') as handle:
            write_data(result, handle)
    else:
        write_data(result, sys.stdout)


if __name__ == '__main__':
    main()