summaryrefslogtreecommitdiffstats
path: root/Tools/CodeCoverage/regenerate-coverage-display
blob: c25b41284150e7c921751d4abdba70e95ae1e588 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env python
#
# Copyright (C) 2004, 2005, 2006 Nathaniel Smith
# Copyright (C) 2007 Holger Hans Peter Freyther
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer. 
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution. 
# 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
#     its contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission. 
#
# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# HTML output inspired by the output of lcov as found on the GStreamer
# site. I assume this is not copyrightable.
#


#
# Read all CSV files and
#  Create an overview file
#
#


import sys
import csv
import glob
import time
import os
import os.path
import datetime
import shutil

os.environ["TTFPATH"] = ":".join(["/usr/share/fonts/truetype/" + d
                                  for d in "ttf-bitstream-vera",
                                           "freefont",
                                           "msttcorefonts"])

level_LOW    = 10
level_MEDIUM = 70

def copy_files(dest_dir):
    """
    Copy the CSS and the png's to the destination directory
    """
    images = ["amber.png", "emerald.png", "glass.png", "ruby.png", "snow.png"]
    css    = "gcov.css"
    (base_path, name) = os.path.split(__file__)
    base_path = os.path.abspath(base_path)

    shutil.copyfile(os.path.join(base_path,css), os.path.join(dest_dir,css))
    map(lambda x: shutil.copyfile(os.path.join(base_path,x), os.path.join(dest_dir,x)), images)

def sumcov(cov):
        return "%.2f%% (%s/%s)" % (cov[1] * 100.0 / (cov[0] or 1), cov[1], cov[0])

def create_page(dest_dir, name):
    index = open(os.path.join(dest_dir, name), "w")
    index.write("""<HTML>
    <HEAD>
        <TITLE>WebKit test coverage information</TITLE>
        <link rel="stylesheet" type="text/css" href="gcov.css">
    </HEAD>
    <BODY>
    """)
    return index

def generate_header(file, last_time, total_lines, total_executed, path, image):
    product = "WebKit"
    date = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(last_time))
    covered_lines = sumcov((total_lines, total_executed))

    file.write("""<table width="100%%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">GCOV code coverage report</td></tr>
    <tr><td class="ruler"><img src="glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%%">
        <table cellpadding=1 border=0 width="100%%">
        <tr>
          <td class="headerItem" width="20%%">Current&nbsp;view:</td>
          <td class="headerValue" width="80%%" colspan=4>%(path)s</td>
        </tr>
        <tr>
          <td class="headerItem" width="20%%">Test:</td>
          <td class="headerValue" width="80%%" colspan=4>%(product)s</td>
        </tr>
        <tr>
          <td class="headerItem" width="20%%">Date:</td>
          <td class="headerValue" width="20%%">%(date)s</td>
          <td width="20%%"></td>
          <td class="headerItem" width="20%%">Instrumented&nbsp;lines:</td>
          <td class="headerValue" width="20%%">%(total_lines)s</td>
        </tr>
        <tr>
          <td class="headerItem" width="20%%">Code&nbsp;covered:</td>
          <td class="headerValue" width="20%%">%(covered_lines)s</td>
          <td width="20%%"></td>
          <td class="headerItem" width="20%%">Executed&nbsp;lines:</td>
          <td class="headerValue" width="20%%">%(total_executed)s</td>
        </tr>
        </table>
      </td>
    </tr>
    <tr><td class="ruler"><img src="glass.png" width=3 height=3 alt=""></td></tr>
  </table>""" % vars())
    # disabled for now <tr><td><img src="%(image)s"></td></tr>

def generate_table_item(file, name, total_lines, covered_lines):
    covered_precise = (covered_lines*100.0)/(total_lines or 1.0)
    covered = int(round(covered_precise))
    remainder = 100-covered
    (image,perClass,numClass) = coverage_icon(covered_precise)
    site = "%s.html" % name.replace(os.path.sep,'__')
    file.write("""
        <tr>
      <td class="coverFile"><a href="%(site)s">%(name)s</a></td>
      <td class="coverBar" align="center">
        <table border=0 cellspacing=0 cellpadding=1><tr><td class="coverBarOutline"><img src="%(image)s" width=%(covered)s height=10 alt="%(covered_precise).2f"><img src="snow.png" width=%(remainder)s height=10 alt="%(covered_precise).2f"></td></tr></table>
      </td>
      <td class="%(perClass)s">%(covered_precise).2f&nbsp;%%</td>
      <td class="%(numClass)s">%(covered_lines)s&nbsp;/&nbsp;%(total_lines)s&nbsp;lines</td>
    </tr>
    """ % vars())

def generate_table_header_start(file):
  file.write("""<center>
  <table width="80%%" cellpadding=2 cellspacing=1 border=0>

    <tr>
      <td width="50%%"><br></td>
      <td width="15%%"></td>
      <td width="15%%"></td>
      <td width="20%%"></td>
    </tr>

    <tr>
      <td class="tableHead">Directory&nbsp;name</td>
      <td class="tableHead" colspan=3>Coverage</td>
    </tr>
    """)

def coverage_icon(percent):
    if percent < level_LOW:
        return ("ruby.png", "coverPerLo", "coverNumLo")
    elif percent < level_MEDIUM:
        return ("amber.png", "coverPerMed", "coverNumMed")
    else:
        return ("emerald.png", "coverPerHi", "coverNumHi")

def replace(text, *pairs):
    """
    From pydoc... almost identical at least
    """
    from string import split, join
    while pairs:
        (a,b) = pairs[0]
        text = join(split(text, a), b)
        pairs = pairs[1:]
    return text

def escape(text):
    """
    Escape string to be conform HTML
    """
    return replace(text,
                        ('&', '&amp;'),
                        ('<', '&lt;' ),
                        ('>', '&gt;' ) )

def generate_table_header_end(file):
    file.write("""</table>
    </center>""")

def write_title_page(dest_dir, last_time, last_tot_lines, last_tot_covered, dir_series):
    """
    Write the index.html with a overview of each directory
    """
    index= create_page(dest_dir, "index.html")
    generate_header(index, last_time, last_tot_lines, last_tot_covered, "directory", "images/Total.png")
    # Create the directory overview
    generate_table_header_start(index)
    dirs = dir_series.keys()
    dirs.sort()
    for dir in dirs:
        (dir_files, total_lines, covered_lines,_) = dir_series[dir][-1]
        generate_table_item(index, dir, total_lines, covered_lines)
    generate_table_header_end(index)

    index.write("""</BODY></HTML>""")
    index.close()

def write_directory_site(dest_dir, dir_name, last_time, dir_series, file_series):
    escaped_dir = dir_name.replace(os.path.sep,'__')
    site = create_page(dest_dir, "%s.html" % escaped_dir)
    (_,tot_lines,tot_covered,files) = dir_series[dir_name][-1]
    generate_header(site, last_time, tot_lines, tot_covered, "directory - %s" % dir_name, "images/%s.png" % escaped_dir)

    files.sort()

    generate_table_header_start(site)
    for file in files:
        (lines,covered) = file_series[file][-1]
        generate_table_item(site, file, lines, covered)

    generate_table_header_end(site)
    site.write("""</BODY></HTML>""")
    site.close()

def write_file_site(dest_dir, file_name, last_time, data_dir, last_id, file_series):
    escaped_name = file_name.replace(os.path.sep,'__')
    site = create_page(dest_dir, "%s.html" % escaped_name)
    (tot_lines,tot_covered) = file_series[file_name][-1]
    generate_header(site, last_time, tot_lines, tot_covered, "file - %s" % file_name, "images/%s.png" % escaped_name)

    path = "%s/%s.annotated%s" % (data_dir,last_id,file_name)

    # In contrast to the lcov we want to show files that have been compiled
    # but have not been tested at all. This means we have sourcefiles with 0
    # lines covered in the path but they are not lcov files.
    # To identify them we check the first line now. If we see that we can
    # continue
    #         -:    0:Source:
    try:
        file = open(path, "r")
    except:
        return
    all_lines = file.read().split("\n")

    # Convert the gcov file to HTML if we have a chanche to do so
    # Scan each line and see if it was covered or not and escape the
    # text
    if len(all_lines) == 0 or not "-:    0:Source:" in all_lines[0]:
        site.write("<p>The file was not excercised</p>")
    else:
        site.write("""</br><table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td><pre class="source">
    """)
        for line in all_lines:
            split_line = line.split(':',2)
            # e.g. at the EOF
            if len(split_line) == 1:
                continue
            line_number = split_line[1].strip()
            if line_number == "0":
                continue
            covered = 15*" "
            end = ""
            if "#####" in split_line[0]:
                covered = '<span class="lineNoCov">%15s' % "0"
                end = "</span>"
            elif split_line[0].strip() != "-":
                covered = '<span class="lineCov">%15s' % split_line[0].strip()
                end = "</span>"

            escaped_line = escape(split_line[2])
            str = '<span class="lineNum">%(line_number)10s </span>%(covered)s: %(escaped_line)s%(end)s\n' % vars()
            site.write(str)
        site.write("</pre></td></tr></table>")
    site.write("</BODY></HTML>")
    site.close()

def main(progname, args):
    if len(args) != 2:
        sys.exit("Usage: %s DATADIR OUTDIR" % progname)

    branch = "WebKit from trunk"
    datadir, outdir = args

    # First, load in all data from the data directory.
    data = []
    for datapath in glob.glob(os.path.join(datadir, "*.csv")):
        data.append(read_csv(datapath))
    # Sort by time
    data.sort()

    # Calculate time series for each file.
    times = [sample[0] for sample in data]
    times = [datetime.datetime.utcfromtimestamp(t) for t in times]

    all_files = {}
    all_dirs  = {}
    for sample in data:
        t, i, tot_line, tot_cover, per_file, per_dir = sample
        all_files.update(per_file)
        all_dirs.update(per_dir)
    total_series = []
    file_serieses = dict([[k, [(0, 0)] * len(times)] for k in all_files.keys()])
    dir_serieses  = dict([[k, [(0, 0, 0, [])] * len(times)] for k in all_dirs.keys()])
    data_idx = 0
    for sample in data:
        t, i, tot_line, tot_cover, per_file, per_dir = sample
        total_series.append([tot_line, tot_cover])
        for f, covinfo in per_file.items():
            file_serieses[f][data_idx] = covinfo
        for f, covinfo in per_dir.items():
            dir_serieses[f][data_idx] = covinfo
        data_idx += 1


    # Okay, ready to start outputting.  First make sure our directories
    # exist.
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    rel_imgdir = "images"
    imgdir = os.path.join(outdir, rel_imgdir)
    if not os.path.exists(imgdir):
        os.makedirs(imgdir)


    # And look up the latest revision id, and coverage information
    last_time, last_id, last_tot_lines, last_tot_covered = data[-1][:4]

    # Now start generating our html file
    copy_files(outdir)
    write_title_page(outdir, last_time, last_tot_lines, last_tot_covered, dir_serieses)

    dir_keys = dir_serieses.keys()
    dir_keys.sort()
    for dir_name in dir_keys:
        write_directory_site(outdir, dir_name, last_time, dir_serieses, file_serieses)

    file_keys = file_serieses.keys()
    for file_name in file_keys:
        write_file_site(outdir, file_name, last_time, datadir, last_id, file_serieses)

def read_csv(path):
    r = csv.reader(open(path, "r"))
    # First line is id, time
    for row in r:
        id, time_str = row
        break
    time = int(float(time_str))
    # Rest of lines are path, total_lines, covered_lines
    per_file = {}
    per_dir  = {}
    grand_total_lines, grand_covered_lines = 0, 0
    for row in r:
        path, total_lines_str, covered_lines_str = row
        total_lines = int(total_lines_str)
        covered_lines = int(covered_lines_str)
        grand_total_lines += total_lines
        grand_covered_lines += covered_lines
        per_file[path] = [total_lines, covered_lines]

        # Update dir statistics
        dirname = os.path.dirname(path)
        if not dirname in per_dir:
            per_dir[dirname] = (0,0,0,[])
        (dir_files,dir_total_lines,dir_covered_lines, files) = per_dir[dirname]
        dir_files += 1
        dir_total_lines += total_lines
        dir_covered_lines += covered_lines
        files.append(path)
        per_dir[dirname] = (dir_files,dir_total_lines,dir_covered_lines,files)
    return [time, id, grand_total_lines, grand_covered_lines, per_file, per_dir]

if __name__ == "__main__":
    import sys
    main(sys.argv[0], sys.argv[1:])