1#!/usr/bin/env python3
2#
3# Copyright (c) 2018 Intel Corporation
4#
5# SPDX-License-Identifier: Apache-2.0
6
7
8"""This script will parse the serial console log file and create the required
9gcda files.
10"""
11
12import argparse
13import os
14import re
15
16
17def retrieve_data(input_file):
18    extracted_coverage_info = {}
19    capture_data = False
20    reached_end = False
21    with open(input_file, 'r') as fp:
22        for line in fp.readlines():
23            if re.search("GCOV_COVERAGE_DUMP_START", line):
24                capture_data = True
25                continue
26            if re.search("GCOV_COVERAGE_DUMP_END", line):
27                reached_end = True
28                break
29            # Loop until the coverage data is found.
30            if not capture_data:
31                continue
32
33            # Remove the leading delimiter "*"
34            file_name = line.split("<")[0][1:]
35            # Remove the trailing new line char
36            hex_dump = line.split("<")[1][:-1]
37            extracted_coverage_info.update({file_name: hex_dump})
38
39    if not reached_end:
40        print("incomplete data captured from %s" % input_file)
41    return extracted_coverage_info
42
43
44def create_gcda_files(extracted_coverage_info):
45    if args.verbose:
46        print("Generating gcda files")
47    for filename, hexdump_val in extracted_coverage_info.items():
48        if args.verbose:
49            print(filename)
50        # if kobject_hash is given for coverage gcovr fails
51        # hence skipping it problem only in gcovr v4.1
52        if "kobject_hash" in filename:
53            filename = filename[:-4] + "gcno"
54            try:
55                os.remove(filename)
56            except Exception:
57                pass
58            continue
59
60        with open(filename, 'wb') as fp:
61            fp.write(bytes.fromhex(hexdump_val))
62
63
64def parse_args():
65    global args
66    parser = argparse.ArgumentParser(
67        description=__doc__,
68        formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False)
69    parser.add_argument("-i", "--input", required=True,
70                        help="Input dump data")
71    parser.add_argument("-v", "--verbose", action="count", default=0,
72                        help="Verbose Output")
73    args = parser.parse_args()
74
75
76def main():
77    parse_args()
78    input_file = args.input
79
80    extracted_coverage_info = retrieve_data(input_file)
81    create_gcda_files(extracted_coverage_info)
82
83
84if __name__ == '__main__':
85    main()
86