8 #File 'accidental-engraver.cc'
9 #Lines executed:87.70% of 252
14 str = open (f).read ()
15 m = re.search ("File '([^']+.cc)'\s*Lines executed:([0-9.]+)% of ([0-9]+)", str)
17 if m and '/usr/lib' in m.group (1):
21 cov = float (m.group (2))
22 lines = int (m.group (3))
23 pain = lines * (100.0 - cov)
25 tup = (pain, locals ().copy())
32 print 'files sorted by number of untested lines (decreasing)'
34 print '%5s (%6s): %s' % ('cov %', 'lines', 'file')
35 print '----------------------------------------------'
37 for (pain, d) in results:
38 print '%(cov)5.2f (%(lines)6d): %(file)s' % d
41 def __init__ (self, range, coverage_count, all_lines, file):
42 assert coverage_count >= 0
43 assert type (range) == type (())
45 self.coverage_count = coverage_count
47 self.all_lines = all_lines
51 return self.range[1] - self.range[0]
54 return ''.join ([l[2] for l in self.lines()])
57 return self.all_lines[self.range[0]:
60 self.range = (min (self.range[0] -1, 0),
63 print 'chunk in', self.file
64 for (c, n, l) in self.lines ():
70 sys.stdout.write ('%8s:%8d:%s' % (cov, n, l))
75 in_lines = [l for l in open (f).readlines ()]
76 (count_len, line_num_len) = tuple (map (len, in_lines[0].split( ':')[:2]))
79 c = l[:count_len].strip ()
81 n = int (l[:line_num_len].strip ())
93 l = l[line_num_len+1:]
99 def get_c_chunks (ls, file):
105 if not (c == last_c or c < 0 and l != '}\n'):
106 if chunk and last_c >= 0:
107 nums = [n-1 for (n, l) in chunk]
108 chunks.append (Chunk ((min (nums), max (nums)+1),
118 def get_scm_chunks (ls, file):
123 nums = [n-1 for (n, l) in chunk]
124 chunks.append (Chunk ((min (nums), max (nums)+1),
131 if l.startswith ('(define'):
136 if not (c == last_c or c < 0):
139 if chunk and last_c >= 0:
147 def widen_chunk (ch, ls):
151 return [(n, l) for (c, n, l) in ls[a:b]]
154 def extract_chunks (file):
156 ls = read_gcov (file)
163 cs = get_scm_chunks (ls, file)
165 cs = get_c_chunks (ls, file)
169 def filter_uncovered (chunks):
171 if c.coverage_count > 0:
175 for stat in ('warning', 'error', 'print', 'scm_gc_mark'):
180 return [c for c in chunks if interesting (c)]
184 p = optparse.OptionParser (usage="usage coverage.py [options] files",
186 p.add_option ("--summary",
191 p.add_option ("--hotspots",
196 p.add_option ("--uncovered",
202 (options, args) = p.parse_args ()
206 summary (['%s.gcov-summary' % s for s in args])
208 if options.uncovered or options.hotspots:
212 if name.endswith ('scm'):
217 chunks += extract_chunks (name)
219 if options.uncovered:
220 chunks = filter_uncovered (chunks)
221 chunks = [(c.length (), c) for c in chunks]
222 elif options.hotspots:
223 chunks = [((c.coverage_count, -c.length()), c) for c in chunks]
228 for (score, c) in chunks:
233 if __name__ == '__main__':