summaryrefslogtreecommitdiff
path: root/buildscripts/aggregate_tracefiles.py
blob: 065ec608425a8c1850aff761fcbc029d2e1644f1 (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
"""Aggregate_tracefiles module.

This script aggregates several tracefiles into one tracefile.
All but the last argument are input tracefiles or .txt files which list tracefiles.
The last argument is the tracefile to which the output will be written.
"""

import subprocess
import os
import sys
from optparse import OptionParser


def aggregate(inputs, output):
    """Aggregate the tracefiles given in inputs to a tracefile given by output."""
    args = ['lcov']

    for name in inputs:
        args += ['-a', name]

    args += ['-o', output]

    print(' '.join(args))

    return subprocess.call(args)


def getfilesize(path):
    """Return file size of 'path'."""
    if not os.path.isfile(path):
        return 0
    return os.path.getsize(path)


def main():
    """Execute Main entry."""
    inputs = []

    usage = "usage: %prog input1.info input2.info ... output.info"
    parser = OptionParser(usage=usage)

    (_, args) = parser.parse_args()
    if len(args) < 2:
        return "must supply input files"

    for path in args[:-1]:
        _, ext = os.path.splitext(path)

        if ext == '.info':
            if getfilesize(path) > 0:
                inputs.append(path)

        elif ext == '.txt':
            inputs += [line.strip() for line in open(path) if getfilesize(line.strip()) > 0]
        else:
            return "unrecognized file type"

    return aggregate(inputs, args[-1])


if __name__ == '__main__':
    sys.exit(main())