blob: 9d1d7330b3600ef74e7e9e72a995db5497b2b9b2 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001#!/usr/bin/env python
2
3# Copyright 2013 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Aggregates EMMA coverage files to produce html output."""
8
9import fnmatch
10import json
11import optparse
12import os
13import sys
14
15import devil_chromium
16from devil.utils import cmd_helper
17from pylib import constants
18from pylib.constants import host_paths
19
20
21def _GetFilesWithExt(root_dir, ext):
22 """Gets all files with a given extension.
23
24 Args:
25 root_dir: Directory in which to search for files.
26 ext: Extension to look for (including dot)
27
28 Returns:
29 A list of absolute paths to files that match.
30 """
31 files = []
32 for root, _, filenames in os.walk(root_dir):
33 basenames = fnmatch.filter(filenames, '*.' + ext)
34 files.extend([os.path.join(root, basename)
35 for basename in basenames])
36
37 return files
38
39
40def main():
41 option_parser = optparse.OptionParser()
42 option_parser.add_option('--output', help='HTML output filename.')
43 option_parser.add_option('--coverage-dir', default=None,
44 help=('Root of the directory in which to search for '
45 'coverage data (.ec) files.'))
46 option_parser.add_option('--metadata-dir', default=None,
47 help=('Root of the directory in which to search for '
48 'coverage metadata (.em) files.'))
49 option_parser.add_option('--cleanup', action='store_true',
50 help=('If set, removes coverage files generated at '
51 'runtime.'))
52 options, _ = option_parser.parse_args()
53
54 devil_chromium.Initialize()
55
56 if not (options.coverage_dir and options.metadata_dir and options.output):
57 option_parser.error('One or more mandatory options are missing.')
58
59 coverage_files = _GetFilesWithExt(options.coverage_dir, 'ec')
60 metadata_files = _GetFilesWithExt(options.metadata_dir, 'em')
61 # Filter out zero-length files. These are created by emma_instr.py when a
62 # target has no classes matching the coverage filter.
63 metadata_files = [f for f in metadata_files if os.path.getsize(f)]
64 print 'Found coverage files: %s' % str(coverage_files)
65 print 'Found metadata files: %s' % str(metadata_files)
66
67 sources = []
68 for f in metadata_files:
69 sources_file = os.path.splitext(f)[0] + '_sources.txt'
70 with open(sources_file, 'r') as sf:
71 sources.extend(json.load(sf))
72 sources = [os.path.join(host_paths.DIR_SOURCE_ROOT, s) for s in sources]
73 print 'Sources: %s' % sources
74
75 input_args = []
76 for f in coverage_files + metadata_files:
77 input_args.append('-in')
78 input_args.append(f)
79
80 output_args = ['-Dreport.html.out.file', options.output]
81 source_args = ['-sp', ','.join(sources)]
82
83 exit_code = cmd_helper.RunCmd(
84 ['java', '-cp',
85 os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'lib', 'emma.jar'),
86 'emma', 'report', '-r', 'html']
87 + input_args + output_args + source_args)
88
89 if options.cleanup:
90 for f in coverage_files:
91 os.remove(f)
92
93 # Command tends to exit with status 0 when it actually failed.
94 if not exit_code and not os.path.exists(options.output):
95 exit_code = 1
96
97 return exit_code
98
99
100if __name__ == '__main__':
101 sys.exit(main())