-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_xmls.py
More file actions
executable file
·133 lines (111 loc) · 5.7 KB
/
process_xmls.py
File metadata and controls
executable file
·133 lines (111 loc) · 5.7 KB
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
import xml.etree.ElementTree as ET
import os
import sys
def find_files_entries(xml_content):
"""Parse XML content and find entries for Conversion.java and SystemProperties.java"""
try:
root = ET.fromstring(xml_content)
except ET.ParseError as e:
print(f"Error parsing XML: {e}")
return {}
file_data = {
'sum_covered_elements': root.get('sum_covered_elements'),
'sum_elements': root.get('sum_elements'),
"coverage": float(root.get('sum_covered_elements'))/float(root.get('sum_elements'))
}
return file_data
def find_specific_entries(xml_content, target_files=["Conversion.java", "SystemProperties.java"]):
"""Parse XML content and find entries for Conversion.java and SystemProperties.java"""
try:
root = ET.fromstring(xml_content)
except ET.ParseError as e:
print(f"Error parsing XML: {e}")
return {}
result = {}
for file_elem in root.findall('file'):
path = file_elem.get('path')
if path: # Ensure path exists
# More reliable way to get filename
filename = os.path.basename(path)
if filename in target_files:
file_data = {
'LOC': file_elem.get('LOC'),
'complexity': file_elem.get('complexity'),
'coveredElements': file_elem.get('coveredElements'),
'elements': file_elem.get('elements'),
"coverage": float(file_elem.get('coveredElements'))/float(file_elem.get('elements')),
'path': path
}
result[filename] = file_data
return result
def process_xml_files(directory='.', target_files=["Conversion.java", "SystemProperties.java"]):
"""
Find and process coverage metrics XML files in directory and subdirectories.
This function extracts coverage information for whitelisted files from XML coverage reports.
It processes both overall project coverage and specific file-level coverage metrics.
Args:
directory (str): Root directory to search for XML files
target_files (list): List of whitelisted Java files to extract coverage for
Returns:
tuple: (project_results, file_results) where:
- project_results: Overall project coverage metrics
- file_results: Coverage metrics for specific whitelisted files
"""
results = {}
project_results = {}
for root_dir, _, files in os.walk(directory):
for file in files:
if file.endswith('.xml'):
file_path = os.path.join(root_dir, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
xml_content = f.read()
files_result = find_files_entries(xml_content)
file_result = find_specific_entries(
xml_content, target_files)
if files_result: # Only add if we found something
project_results[file_path] = files_result
if file_result:
results[file_path] = file_result
except Exception as e:
print(f"Error processing {file_path}: {e}")
return project_results, results
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description='Extract coverage information for whitelisted files from XML coverage reports')
parser.add_argument(
'directory', help='Root directory to search for XML files')
parser.add_argument('-o', '--output', default='out.txt',
help='Output file (default: out.txt)')
args = parser.parse_args()
orig_stdout = sys.stdout
f = open(args.output, 'w')
sys.stdout = f
# Process all XML files in current directory and subdirectories
# Whitelisted files for coverage analysis (key files from each project)
target_files = ["UTF8StreamJsonParser.java", "Styler.java,AxesChartStyler.java", "PlotContent_Category_Bar.java", "PlotContent_Pie.java", "Axis.java", "Legend_HeatMap.java", "PlotContent_Dial.java", "OHLCChart.java", "AxisPair.java", "PlotContent_XY.java", "AbstractBaseTheme.java", "AxisTickCalculator_.java", "PlotContent_OHLC.java", "XChartPanel.java", "DialStyler.java", "ToolTips.java",
"Legend_.java", "PlotContent_Radar.java", "AxisTickCalculator_Date.java", "XYChart.java", "PlotContent_HeatMap.java", "Chart.java", "Utils.java", "Cursor.java", "PieStyler.java", "JacksonAnnotationIntrospector.java", "POJOPropertyBuilder.java", "ObjectMapper.java", "FTP.java", "FTPClient.java", "NNTPClient.java", "Conversion.java", "SystemProperties.java"]
project_results, all_results = process_xml_files(
directory=args.directory, target_files=target_files)
for file_path, entries in all_results.items():
print(f"\nResults from {file_path}:")
coveredElements = 0
elements = 0
lines_of_code = 0
for filename, data in entries.items():
print(f" {filename}:")
for key, value in data.items():
if key == "coveredElements":
coveredElements += int(value)
if key == "LOC":
lines_of_code += int(value)
if key == "elements":
elements += int(value)
print(f" {key}: {value}")
print(f"Coverage: {coveredElements/elements}")
print(f"LOC_sum: {lines_of_code}")
# Alternatively, just print the raw dictionary
# print(all_results)
sys.stdout = orig_stdout
f.close()