#! /usr/bin/env python # # This file is part of DisOrder. # Copyright (C) 2007 Richard Kettlewell # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # import re,sys,os,string def fatal(msg): sys.stderr.write("%s\n" % msg) sys.exit(1) def sgmlquotechar(c): if c == '&' or c == '<' or ord(c) < 32 or ord(c) > 126: return "&#%d;" % ord(c) else: return c def sgmlquote(s): return string.join(map(sgmlquotechar, s),'') def line_count(f): return len(open(f, "r").readlines()) missing = {} percent = {} total_lines = 0 covered_lines = 0 args = sys.argv[1:] htmldir = None while len(args) > 0 and re.match("^--", args[0]): if args[0] == "--html": htmldir = args[1] args = args[2:] else: fatal("unknown option '%s'" % args[0]) for s in args: missing[s] = True name = None for line in sys.stdin: line = line[:-1] r = re.match("File ['`](?:.*/)?([^/]+.c)'", line) if r: name = r.group(1) if name in missing: del missing[name] r = re.match("Lines executed:([0-9\\.]+)% of ([0-9]+)", line) if r: if name: this_pc = float(r.group(1)) this_lines = int(r.group(2)) percent[name] = this_pc total_lines += this_lines covered_lines += this_lines * this_pc / 100.0 name = None for m in missing: percent[m] = 0 total_lines += line_count(m) def cmp(a,b): if percent[a] < percent[b]: return -1 elif percent[a] > percent[b]: return 1 else: return 0 keys = percent.keys() keys.sort(cmp) if len(keys): for k in keys: print "%20s: %d%%" % (k, percent[k]) print "Total coverage: %d%%" % (100 * (covered_lines / total_lines)) if htmldir is not None and len(keys): index = open(os.path.join(htmldir, "index.html"), "w") index.write("gcov report\n") index.write("\n"); index.write("

gcov report

\n") index.write("\n") for k in keys: index.write("\n") if k in missing: index.write("\n" % int(percent[k])) index.write("
FileCoverage
%s\n" % sgmlquote(k)) else: index.write("%s\n" % (sgmlquote(k), sgmlquote(k))) index.write("%d%%\n" % percent[k]) index.write("
\n") index.write("

Total coverage: %d%%

\n" % (100 * (covered_lines / total_lines))) for k in keys: if k in missing: continue html = open(os.path.join(htmldir, "%s.html" % k), "w") html.write("%s\n" % sgmlquote(k)) html.write("

%s

\n" % sgmlquote(k)) html.write("
")
    r = re.compile("^ *#####:.*")
    for line in open("%s.gcov" % k, "r"):
      if len(line) > 0 and line[-1] == '\n':
        line = line[:-1]
      if r.match(line):
        html.write("%s\n" % sgmlquote(line))
      else:
        html.write("%s\n" % sgmlquote(line))
    html.write("
\n")