OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 | 6 |
7 """Run the given command through LLVM's coverage tools.""" | 7 """Run the given command through LLVM's coverage tools.""" |
8 | 8 |
9 | 9 |
10 import argparse | 10 import argparse |
11 import json | 11 import json |
12 import os | 12 import os |
| 13 import re |
13 import shlex | 14 import shlex |
14 import subprocess | 15 import subprocess |
15 import sys | 16 import sys |
16 | 17 |
17 | 18 |
18 BUILDTYPE = 'Coverage' | 19 BUILDTYPE = 'Coverage' |
19 OUT_DIR = os.path.realpath(os.path.join('out', BUILDTYPE)) | 20 OUT_DIR = os.path.realpath(os.path.join('out', BUILDTYPE)) |
20 PROFILE_DATA = 'default.profraw' | 21 PROFILE_DATA = 'default.profraw' |
21 PROFILE_DATA_MERGED = 'prof_merged' | 22 PROFILE_DATA_MERGED = 'prof_merged' |
22 | 23 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 results = [] | 90 results = [] |
90 for line in report.splitlines()[2:-2]: | 91 for line in report.splitlines()[2:-2]: |
91 filename, _, _, cover, _, _ = shlex.split(line) | 92 filename, _, _, cover, _, _ = shlex.split(line) |
92 percent = float(cover.split('%')[0]) | 93 percent = float(cover.split('%')[0]) |
93 results.append((percent, filename)) | 94 results.append((percent, filename)) |
94 results = _filter_results(results) | 95 results = _filter_results(results) |
95 results.sort() | 96 results.sort() |
96 return results | 97 return results |
97 | 98 |
98 | 99 |
| 100 def _testname(filename): |
| 101 """Transform the file name into an ingestible test name.""" |
| 102 return re.sub(r'[^a-zA-Z0-9]', '_', filename) |
| 103 |
| 104 |
| 105 def _nanobench_json(results, properties, key): |
| 106 """Return the results in JSON format like that produced by nanobench.""" |
| 107 rv = {} |
| 108 # Copy over the properties first, then set the 'key' and 'results' keys, |
| 109 # in order to avoid bad formatting in case the user passes in a properties |
| 110 # dict containing those keys. |
| 111 rv.update(properties) |
| 112 rv['key'] = key |
| 113 rv['results'] = { |
| 114 _testname(f): { |
| 115 'coverage': { |
| 116 'percent': percent, |
| 117 'options': { |
| 118 'fullname': f, |
| 119 'dir': os.path.dirname(f), |
| 120 }, |
| 121 }, |
| 122 } for percent, f in results |
| 123 } |
| 124 return rv |
| 125 |
| 126 |
| 127 def _parse_key_value(kv_list): |
| 128 """Return a dict whose key/value pairs are derived from the given list. |
| 129 |
| 130 For example: |
| 131 |
| 132 ['k1', 'v1', 'k2', 'v2'] |
| 133 becomes: |
| 134 |
| 135 {'k1': 'v1', |
| 136 'k2': 'v2'} |
| 137 """ |
| 138 if len(kv_list) % 2 != 0: |
| 139 raise Exception('Invalid key/value pairs: %s' % kv_list) |
| 140 |
| 141 rv = {} |
| 142 for i in xrange(len(kv_list) / 2): |
| 143 rv[kv_list[i*2]] = kv_list[i*2+1] |
| 144 return rv |
| 145 |
| 146 |
99 def main(): | 147 def main(): |
100 res = run_coverage(sys.argv[1:]) | 148 """Run coverage and generate a report.""" |
101 print '% Covered\tFilename' | 149 # Parse args. |
102 for percent, f in res: | 150 parser = argparse.ArgumentParser() |
103 print '%f\t%s' % (percent, f) | 151 parser.add_argument('--outResultsFile') |
| 152 parser.add_argument( |
| 153 '--key', metavar='key_or_value', nargs='+', |
| 154 help='key/value pairs identifying this bot.') |
| 155 parser.add_argument( |
| 156 '--properties', metavar='key_or_value', nargs='+', |
| 157 help='key/value pairs representing properties of this build.') |
| 158 args, cmd = parser.parse_known_args() |
| 159 key = _parse_key_value(args.key) |
| 160 properties = _parse_key_value(args.properties) |
| 161 |
| 162 # Run coverage. |
| 163 results = run_coverage(cmd) |
| 164 |
| 165 # Write results. |
| 166 format_results = _nanobench_json(results, properties, key) |
| 167 if args.outResultsFile: |
| 168 with open(args.outResultsFile, 'w') as f: |
| 169 json.dump(format_results, f) |
| 170 else: |
| 171 print json.dumps(format_results, indent=4, sort_keys=True) |
104 | 172 |
105 | 173 |
106 if __name__ == '__main__': | 174 if __name__ == '__main__': |
107 main() | 175 main() |
OLD | NEW |