File: coverage-calc

package info (click to toggle)
bro 2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 78,640 kB
  • sloc: ansic: 126,302; cpp: 95,205; yacc: 2,528; lex: 1,819; sh: 793; python: 700; makefile: 134
file content (59 lines) | stat: -rwxr-xr-x 2,313 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env python

# This script aggregates many files containing Bro script coverage information
# into a single file and reports the overall coverage information. Usage:
#
#   coverage-calc <quoted glob of filenames> <output file> <script dir>
#
# The last argument is used to point to a root directory containing all
# the Bro distribution's scripts.  It's used to cull out test scripts
# that are not part of the distribution and which should not count towards
# the coverage calculation.

import os
import sys
import glob

stats = {}
inputglob = sys.argv[1]
outputfile = sys.argv[2]
scriptdir = os.path.abspath(sys.argv[3])

for filename in glob.glob(inputglob):
    with open(filename, 'r') as f:
        for line in f.read().splitlines():
            parts = line.split("\t")
            exec_count = int(parts[0])
            # grab file path and line numbers separately
            filepath, srclines = parts[1].rsplit(",", 1)
            filepath = os.path.normpath(filepath)
            # ignore scripts that don't appear to be part of Bro distribution
            if not filepath.startswith(scriptdir):
                continue
            # keep only the line number (or line number range)
            srclines = srclines.split()[1]
            # For sorting purposes (so that line numbers get sorted correctly),
            # construct a specially-formatted key string.
            sortkey = filepath + ", line " + ("%6s" % srclines.split("-")[0])
            location = filepath + ", line " + srclines
            desc = parts[2]
            # Keying by location + desc may result in duplicate data
            # as some descs change as a result of differing configurations
            # producing record (re)definitions
            key = location
            if key in stats:
                stats[key][0] += exec_count
            else:
                stats[key] = [exec_count, location, desc, sortkey]

with open(outputfile, 'w') as f:
    for k in sorted(stats, key=lambda i: stats[i][3]):
        f.write("%s\t%s\t%s\n" % (stats[k][0], stats[k][1], stats[k][2]))

num_covered = 0
for k in stats:
    if stats[k][0] > 0:
        num_covered += 1

if len(stats) > 0:
    print("%s/%s (%.1f%%) Bro script statements covered." % (num_covered, len(stats), float(num_covered)/len(stats)*100))