]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/coverage/parse_gcov_output.py
5 from optparse
import OptionParser
7 # the gcov report follows certain pattern. Each file will have two lines
8 # of report, from which we can extract the file name, total lines and coverage
10 def parse_gcov_report(gcov_input
):
11 per_file_coverage
= {}
14 for line
in sys
.stdin
:
17 # --First line of the coverage report (with file name in it)?
18 match_obj
= re
.match("^File '(.*)'$", line
)
20 # fetch the file name from the first line of the report.
21 current_file
= match_obj
.group(1)
24 # -- Second line of the file report (with coverage percentage)
25 match_obj
= re
.match("^Lines executed:(.*)% of (.*)", line
)
28 coverage
= float(match_obj
.group(1))
29 lines
= int(match_obj
.group(2))
31 if current_file
is not None:
32 per_file_coverage
[current_file
] = (coverage
, lines
)
35 # If current_file is not set, we reach the last line of report,
36 # which contains the summarized coverage percentage.
37 total_coverage
= (coverage
, lines
)
40 # If the line's pattern doesn't fall into the above categories. We
41 # can simply ignore them since they're either empty line or doesn't
42 # find executable lines of the given file.
45 return per_file_coverage
, total_coverage
47 def get_option_parser():
48 usage
= "Parse the gcov output and generate more human-readable code " +\
50 parser
= OptionParser(usage
)
53 "--interested-files", "-i",
55 help="Comma separated files names. if specified, we will display " +
56 "the coverage report only for interested source files. " +
57 "Otherwise we will display the coverage report for all " +
62 def display_file_coverage(per_file_coverage
, total_coverage
):
63 # To print out auto-adjustable column, we need to know the longest
64 # length of file names.
65 max_file_name_length
= max(
66 len(fname
) for fname
in per_file_coverage
.keys()
70 # size of separator is determined by 3 column sizes:
71 # file name, coverage percentage and lines.
73 "%" + str(max_file_name_length
) + "s\t%s\t%s"
74 separator
= "-" * (max_file_name_length
+ 10 + 20)
75 print header_template
% ("Filename", "Coverage", "Lines")
79 # template for printing coverage report for each file.
80 record_template
= "%" + str(max_file_name_length
) + "s\t%5.2f%%\t%10d"
82 for fname
, coverage_info
in per_file_coverage
.items():
83 coverage
, lines
= coverage_info
84 print record_template
% (fname
, coverage
, lines
)
89 print record_template
% ("Total", total_coverage
[0], total_coverage
[1])
91 def report_coverage():
92 parser
= get_option_parser()
93 (options
, args
) = parser
.parse_args()
95 interested_files
= set()
96 if options
.filenames
is not None:
97 interested_files
= set(f
.strip() for f
in options
.filenames
.split(','))
99 # To make things simple, right now we only read gcov report from the input
100 per_file_coverage
, total_coverage
= parse_gcov_report(sys
.stdin
)
102 # Check if we need to display coverage info for interested files.
103 if len(interested_files
):
104 per_file_coverage
= dict(
105 (fname
, per_file_coverage
[fname
]) for fname
in interested_files
106 if fname
in per_file_coverage
108 # If we only interested in several files, it makes no sense to report
110 total_coverage
= None
112 if not len(per_file_coverage
):
113 print >> sys
.stderr
, "Cannot find coverage info for the given files."
115 display_file_coverage(per_file_coverage
, total_coverage
)
117 if __name__
== "__main__":