• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
14import traceback
15
16from pylib import cmd_helper
17from pylib import constants
18
19
20def _GetFilesWithExt(root_dir, ext):
21  """Gets all files with a given extension.
22
23  Args:
24    root_dir: Directory in which to search for files.
25    ext: Extension to look for (including dot)
26
27  Returns:
28    A list of absolute paths to files that match.
29  """
30  files = []
31  for root, _, filenames in os.walk(root_dir):
32    basenames = fnmatch.filter(filenames, '*.' + ext)
33    files.extend([os.path.join(root, basename)
34                  for basename in basenames])
35
36  return files
37
38
39def main(argv):
40  option_parser = optparse.OptionParser()
41  option_parser.add_option('--output', help='HTML output filename.')
42  option_parser.add_option('--coverage-dir', default=None,
43                           help=('Root of the directory in which to search for '
44                                 'coverage data (.ec) files.'))
45  option_parser.add_option('--metadata-dir', default=None,
46                           help=('Root of the directory in which to search for '
47                                 'coverage metadata (.em) files.'))
48  option_parser.add_option('--cleanup', action='store_true',
49                           help=('If set, removes coverage files generated at '
50                                 'runtime.'))
51  options, args = option_parser.parse_args()
52
53  if not (options.coverage_dir and options.metadata_dir and options.output):
54    option_parser.error('One or more mandatory options are missing.')
55
56  coverage_files = _GetFilesWithExt(options.coverage_dir, 'ec')
57  metadata_files = _GetFilesWithExt(options.metadata_dir, 'em')
58  print 'Found coverage files: %s' % str(coverage_files)
59  print 'Found metadata files: %s' % str(metadata_files)
60
61  sources = []
62  for f in metadata_files:
63    sources_file = os.path.splitext(f)[0] + '_sources.txt'
64    with open(sources_file, 'r') as sf:
65      sources.extend(json.load(sf))
66  sources = [os.path.join(constants.DIR_SOURCE_ROOT, s) for s in sources]
67  print 'Sources: %s' % sources
68
69  input_args = []
70  for f in coverage_files + metadata_files:
71    input_args.append('-in')
72    input_args.append(f)
73
74  output_args = ['-Dreport.html.out.file', options.output]
75  source_args = ['-sp', ','.join(sources)]
76
77  exit_code = cmd_helper.RunCmd(
78      ['java', '-cp',
79       os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'lib', 'emma.jar'),
80       'emma', 'report', '-r', 'html']
81      + input_args + output_args + source_args)
82
83  if options.cleanup:
84    for f in coverage_files:
85      os.remove(f)
86
87  return exit_code
88
89
90if __name__ == '__main__':
91  sys.exit(main(sys.argv))
92