-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCodeCoverage.py
173 lines (147 loc) · 7.34 KB
/
CodeCoverage.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
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
#=============================================================================
# Notices:
#
# Copyright 2025 United States Government as represented by the Administrator
# of the National Aeronautics and Space Administration. All Rights Reserved.
#
#
# Disclaimers:
#
# No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF
# ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED
# TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY
# IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
# FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR
# FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE
# SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN
# ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT OF ANY RESULTS,
# RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS
# RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, GOVERNMENT AGENCY
# DISCLAIMS ALL WARRANTIES AND LIABILITIES REGARDING THIRD-PARTY SOFTWARE,
# IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT "AS IS."
#
# Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST THE
# UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY
# PRIOR RECIPIENT. IF RECIPIENT'S USE OF THE SUBJECT SOFTWARE RESULTS IN ANY
# LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM SUCH USE,
# INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S
# USE OF THE SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS THE
# UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY
# PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR
# ANY SUCH MATTER SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS
# AGREEMENT.
#
#=============================================================================
class CodeCoverage:
def __init__(coverage_format):
self.coverage_format = coverage_format
self.command_string = '/gcovr-3.3/gcovr -e "(.*[/]*io_src/|.*[/]*swig/|.*[/]*verif/|.*[/]*test/|.*[/]*object_Linux_.*/)" -r '
self.find_string = ' -type f -iname "*.o" -not -path "*/verif/*" -not -path "*/io_src/*" -not -path "*/swig/*" -not -path "*/test/*" 2>/dev/null | sort | uniq'
self.xml_string = " --xml-pretty -o coverage.xml"
self.html_string = " --html --html-details --html-absolute-paths -o coverage.html"
def execute(model_dir):
test_path = model_dir + ("/code_coverage")
shutil.rmtree(test_path, True)
os.mkdir(test_path)
tprint(" build coverage file list ...")
gc_dic = buildGcDic(model_dir)
tprint(" copy coverage files ...")
cp_gc_files(gc_dic)
with changeDirectory(test_path):
gcovr_cmd = scriptDir + command_string + model_dir
#generage xml output
if 1==self.coverage_format%2:
tprint(" generate xml report ...")
cov_cmd = gcovr_cmd + " --xml-pretty -o coverage.xml"
sys_cmd(cov_cmd)
#generage html output
if 2<= self.coverage_format%4:
tprint(" generate html report ...")
cov_cmd = gcovr_cmd + " --html --html-details --html-absolute-paths -o coverage.html"
sys_cmd(cov_cmd)
tprint(" clean up ...")
clean_gc_files(gc_dic)
if 1==self.coverage_format%2:
tprint(" count the not-used source files ...")
count_not_built_in_files(model_dir)
#build the dictionary of .gcxx file location and its source code location
def buildGcDic(model_dir):
gc_dic = {}
find_cmd = 'find ' + model_dir + self.find_string
find_res = sys_cmd(find_cmd, False)
obj_list = find_res.splitlines()
for obj in obj_list:
gcno_file = obj[:-2] + ".gcno"
if (not os.path.isfile(gcno_file)) or (not os.path.exists(gcno_file)):
continue
src_loc = get_src_loc(obj, model_dir)
if src_loc is None:
continue
if (not os.path.isdir(src_loc)) or (not os.path.exists(src_loc)):
tprint("WARNING: The source code directory " + src_loc + " dosen't exist!")
continue
gc_dic[obj[:-2] + ".gc*"] = src_loc
return gc_dic
#parse the source code file from the given object file
def get_src_loc(obj_file, model_dir):
dump_cmd = 'objdump -W ' + obj_file + ' 2>/dev/null | grep "^.*DW_AT_name.*$" | grep "\\.\\(cpp\\|cc\\|c\\|\\f\\)" | grep -v "_GLOBAL__I_" | awk \'{print $NF;}\''
dump_res = sys_cmd(dump_cmd, False)
src_path_list = dump_res.splitlines()
if len(src_path_list) == 0:
return None
src_path = os.path.split(src_path_list[0].strip())[0].strip()
if len(src_path)==0 or src_path[0]!='/':
dump_cmd = 'objdump -W ' + obj_file + ' 2>/dev/null | grep "^.*DW_AT_comp_dir.*[ \\t][ \\t]*' + model_dir + '"' + " | awk '{print $NF;}'"
dump_res = sys_cmd(dump_cmd, False)
comp_path_list = dump_res.splitlines()
if len(comp_path_list) == 0:
return None
comp_path = comp_path_list[0].strip()
if len(comp_path)==0 or comp_path[0]!='/':
return None
src_path = os.path.abspath(os.path.join(comp_path, src_path))
obj_path = os.path.split(obj_file)[0].strip()
if obj_path == src_path:
return None
if src_path.find(model_dir) == -1:
return None
if re.search("(/verif/|/io_src/|/swig/|/test/)", src_path) != None:
return None
return src_path
#Copy the coverage files into source code location
def cp_gc_files(gc_dic):
for src, tgt in gc_dic.items():
for file in glob.glob(src):
shutil.copy(file, tgt)
return 0
#clean the coverage files from source code locaton
def clean_gc_files(gc_dic):
code_path_set = set([])
for src, tgt in gc_dic.items():
tgt_file = tgt + "/" + os.path.split(src)[1].strip()
for file in glob.glob(tgt_file):
os.remove(file)
#count the no-built-in files in the given model_dir
def count_not_built_in_files(model_dir):
built_in_set = set([])
cmd_txt = 'grep -o \'filename=".*"\' ' + model_dir + '/code_coverage/coverage.xml | awk -F\'"\' \'{print $2;}\' | sort | uniq'
cmd_res = sys_cmd(cmd_txt)
built_in_files = cmd_res.splitlines()
for item in built_in_files:
file_path = os.path.abspath(os.path.join(model_dir, item))
built_in_set.add(file_path)
cmd_txt = 'find ' + model_dir + ' -type f -iname "*.c" -o -iname "*.cc" -o -iname "*.cpp" -o -iname "*.f90" | grep -v "\\(/io_src/\\|/swig/\\|/verif/\\|/test/\\)" | sort | uniq'
cmd_res = sys_cmd(cmd_txt)
model_files = cmd_res.splitlines()
log_file = open(os.path.abspath(os.path.join(model_dir, 'code_coverage/not_used_files.txt')), 'w')
cnt = 0
for item in model_files:
if item not in built_in_set:
log_file.write(item)
log_file.write("\n")
cnt += 1
log_file.close()
cnt_file = open(os.path.abspath(os.path.join(model_dir, 'code_coverage/count_file.txt')), 'w')
cnt_file.write('Total Source Files:'+str(len(model_files))+'\n')
cnt_file.write('Not_Used_Source_files:'+str(cnt)+'\n')
cnt_file.close()