blob: 93b0b0e59c5b10eafa3b3c0a97e3d9ebd081f434 [file] [log] [blame]
[email protected]803f65a72013-08-20 19:11:301#!/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
15from pylib import cmd_helper
16from pylib import constants
17
18
19def _GetFilesWithExt(root_dir, ext):
20 """Gets all files with a given extension.
21
22 Args:
23 root_dir: Directory in which to search for files.
24 ext: Extension to look for (including dot)
25
26 Returns:
27 A list of absolute paths to files that match.
28 """
29 files = []
30 for root, _, filenames in os.walk(root_dir):
31 basenames = fnmatch.filter(filenames, '*.' + ext)
32 files.extend([os.path.join(root, basename)
33 for basename in basenames])
34
35 return files
36
37
[email protected]7c53a602014-03-24 16:21:4438def main():
[email protected]803f65a72013-08-20 19:11:3039 option_parser = optparse.OptionParser()
[email protected]a1f1abfe2013-08-27 22:02:4340 option_parser.add_option('--output', help='HTML output filename.')
41 option_parser.add_option('--coverage-dir', default=None,
[email protected]803f65a72013-08-20 19:11:3042 help=('Root of the directory in which to search for '
43 'coverage data (.ec) files.'))
[email protected]a1f1abfe2013-08-27 22:02:4344 option_parser.add_option('--metadata-dir', default=None,
[email protected]803f65a72013-08-20 19:11:3045 help=('Root of the directory in which to search for '
46 'coverage metadata (.em) files.'))
[email protected]a1f1abfe2013-08-27 22:02:4347 option_parser.add_option('--cleanup', action='store_true',
[email protected]3bdb55f2013-08-31 01:20:3148 help=('If set, removes coverage files generated at '
49 'runtime.'))
[email protected]7c53a602014-03-24 16:21:4450 options, _ = option_parser.parse_args()
[email protected]803f65a72013-08-20 19:11:3051
52 if not (options.coverage_dir and options.metadata_dir and options.output):
[email protected]a1f1abfe2013-08-27 22:02:4353 option_parser.error('One or more mandatory options are missing.')
[email protected]803f65a72013-08-20 19:11:3054
55 coverage_files = _GetFilesWithExt(options.coverage_dir, 'ec')
56 metadata_files = _GetFilesWithExt(options.metadata_dir, 'em')
57 print 'Found coverage files: %s' % str(coverage_files)
58 print 'Found metadata files: %s' % str(metadata_files)
[email protected]3bdb55f2013-08-31 01:20:3159
60 sources = []
[email protected]803f65a72013-08-20 19:11:3061 for f in metadata_files:
[email protected]485fb232013-08-22 19:56:3362 sources_file = os.path.splitext(f)[0] + '_sources.txt'
[email protected]3bdb55f2013-08-31 01:20:3163 with open(sources_file, 'r') as sf:
64 sources.extend(json.load(sf))
65 sources = [os.path.join(constants.DIR_SOURCE_ROOT, s) for s in sources]
66 print 'Sources: %s' % sources
[email protected]803f65a72013-08-20 19:11:3067
68 input_args = []
[email protected]3bdb55f2013-08-31 01:20:3169 for f in coverage_files + metadata_files:
[email protected]803f65a72013-08-20 19:11:3070 input_args.append('-in')
71 input_args.append(f)
72
73 output_args = ['-Dreport.html.out.file', options.output]
[email protected]3bdb55f2013-08-31 01:20:3174 source_args = ['-sp', ','.join(sources)]
[email protected]803f65a72013-08-20 19:11:3075
[email protected]485fb232013-08-22 19:56:3376 exit_code = cmd_helper.RunCmd(
[email protected]803f65a72013-08-20 19:11:3077 ['java', '-cp',
78 os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'lib', 'emma.jar'),
79 'emma', 'report', '-r', 'html']
80 + input_args + output_args + source_args)
81
[email protected]a1f1abfe2013-08-27 22:02:4382 if options.cleanup:
[email protected]3bdb55f2013-08-31 01:20:3183 for f in coverage_files:
[email protected]a1f1abfe2013-08-27 22:02:4384 os.remove(f)
85
[email protected]3bdb55f2013-08-31 01:20:3186 return exit_code
[email protected]485fb232013-08-22 19:56:3387
[email protected]803f65a72013-08-20 19:11:3088
89if __name__ == '__main__':
[email protected]7c53a602014-03-24 16:21:4490 sys.exit(main())