-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobertura.py
94 lines (76 loc) · 2.7 KB
/
cobertura.py
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
import xml.etree.ElementTree as ET
import os
def printET(node, depth, max_depth):
if depth == max_depth:
return
prefix = depth * " "
print("{}{} {}".format(prefix, node.tag, node.attrib))
if node.text is not None:
text = node.text.strip()
if text:
print("{} # {}".format(prefix, text))
if node.tail is not None:
text = node.tail.strip()
if text:
print("{}# {}".format(prefix, text))
depth += 1
for child in node:
printET(child, depth, max_depth)
def diskname_from_xml(sources):
disks = [child.text for child in sources if child.text is not None]
if len(disks) == 1:
return disks[0]
class CoberturaXML:
def __init__(self, path):
self.path = path
def preprocess(self, _):
return {}
def ext(self):
return ".xml"
def stats(self, xml_file):
root = ET.parse(xml_file).getroot()
sources, packages = None, None
for child in root:
if child.tag == "sources":
sources = child
if child.tag == "packages":
packages = child
if packages is None:
return {}
diskname = None
if sources is not None:
diskname = diskname_from_xml(sources)
if diskname is None:
diskname = os.path.splitdrive(os.path.abspath(xml_file))[0]
if len(diskname) and diskname[len(diskname) - 1] not in "\\/":
diskname += os.sep
result = {}
for package in packages:
for classes in package:
for klass in classes:
try:
filename = klass.attrib["filename"]
except KeyError:
continue
filename = os.path.join(diskname, filename)
lines = None
for info in klass:
if info.tag == "lines":
lines = info
if lines is None:
continue
raw_lines = []
for line in lines:
try:
number = int(line.attrib["number"])
hits = int(line.attrib["hits"])
raw_lines.append((number, hits, False))
except KeyError:
continue
except ValueError:
continue
coverage = list(sorted(raw_lines))
if not len(coverage):
continue
result[filename] = ([], coverage)
return result