OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 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 """Toolbox to manage all the json files in this directory. | 6 """Toolbox to manage all the json files in this directory. |
7 | 7 |
8 It can reformat them in their canonical format or ensures they are well | 8 It can reformat them in their canonical format or ensures they are well |
9 formatted. | 9 formatted. |
10 """ | 10 """ |
(...skipping 18 matching lines...) Expand all Loading... |
29 # These are not 'builders'. | 29 # These are not 'builders'. |
30 'compile_targets', 'gtest_tests', 'filter_compile_builders', | 30 'compile_targets', 'gtest_tests', 'filter_compile_builders', |
31 'non_filter_builders', 'non_filter_tests_builders', | 31 'non_filter_builders', 'non_filter_tests_builders', |
32 | 32 |
33 # These are not supported on Swarming yet. | 33 # These are not supported on Swarming yet. |
34 # http://crbug.com/472205 | 34 # http://crbug.com/472205 |
35 'Chromium Mac 10.10', | 35 'Chromium Mac 10.10', |
36 # http://crbug.com/441429 | 36 # http://crbug.com/441429 |
37 'Linux Trusty (32)', 'Linux Trusty (dbg)(32)', | 37 'Linux Trusty (32)', 'Linux Trusty (dbg)(32)', |
38 | 38 |
| 39 # http://crbug.com/480053 |
| 40 'Linux GN', |
| 41 'linux_chromium_gn_rel', |
| 42 |
| 43 # Unmaintained builders on chromium.fyi |
| 44 'ClangToTMac', |
| 45 'ClangToTMacASan', |
| 46 |
39 # One off builders. Note that Swarming does support ARM. | 47 # One off builders. Note that Swarming does support ARM. |
40 'Linux ARM Cross-Compile', | 48 'Linux ARM Cross-Compile', |
41 'Site Isolation Linux', | 49 'Site Isolation Linux', |
42 'Site Isolation Win', | 50 'Site Isolation Win', |
43 } | 51 } |
44 | 52 |
45 | 53 |
46 def upgrade_test(test): | 54 class Error(Exception): |
47 """Converts from old style string to new style dict.""" | 55 """Processing error.""" |
48 if isinstance(test, basestring): | |
49 return {'test': test} | |
50 assert isinstance(test, dict) | |
51 return test | |
52 | 56 |
53 | 57 |
54 def get_isolates(): | 58 def get_isolates(): |
55 """Returns the list of all isolate files.""" | 59 """Returns the list of all isolate files.""" |
56 files = subprocess.check_output(['git', 'ls-files'], cwd=SRC_DIR).splitlines() | 60 files = subprocess.check_output(['git', 'ls-files'], cwd=SRC_DIR).splitlines() |
57 return [os.path.basename(f) for f in files if f.endswith('.isolate')] | 61 return [os.path.basename(f) for f in files if f.endswith('.isolate')] |
58 | 62 |
59 | 63 |
| 64 def process_builder_convert(data, filename, builder, test_name): |
| 65 """Converts 'test_name' to run on Swarming in 'data'. |
| 66 |
| 67 Returns True if 'test_name' was found. |
| 68 """ |
| 69 result = False |
| 70 for test in data['gtest_tests']: |
| 71 if test['test'] != test_name: |
| 72 continue |
| 73 test.setdefault('swarming', {}) |
| 74 if not test['swarming'].get('can_use_on_swarming_builders'): |
| 75 print('- %s: %s' % (filename, builder)) |
| 76 test['swarming']['can_use_on_swarming_builders'] = True |
| 77 result = True |
| 78 return result |
| 79 |
| 80 |
| 81 def process_builder_remaining(data, filename, builder, tests_location): |
| 82 """Calculates tests_location when mode is --remaining.""" |
| 83 for test in data['gtest_tests']: |
| 84 name = test['test'] |
| 85 if test.get('swarming', {}).get('can_use_on_swarming_builders'): |
| 86 tests_location[name]['count_run_on_swarming'] += 1 |
| 87 else: |
| 88 tests_location[name]['count_run_local'] += 1 |
| 89 tests_location[name]['local_configs'].setdefault( |
| 90 filename, []).append(builder) |
| 91 |
| 92 |
| 93 def process_file(mode, test_name, tests_location, filepath): |
| 94 """Processes a file. |
| 95 |
| 96 The action depends on mode. Updates tests_location. |
| 97 |
| 98 Return False if the process exit code should be 1. |
| 99 """ |
| 100 filename = os.path.basename(filepath) |
| 101 with open(filepath) as f: |
| 102 content = f.read() |
| 103 try: |
| 104 config = json.loads(content) |
| 105 except ValueError as e: |
| 106 raise Error('Exception raised while checking %s: %s' % (filepath, e)) |
| 107 |
| 108 for builder, data in sorted(config.iteritems()): |
| 109 if builder in SKIP: |
| 110 # Oddities. |
| 111 continue |
| 112 if not isinstance(data, dict): |
| 113 raise Error('%s: %s is broken: %s' % (filename, builder, data)) |
| 114 if 'gtest_tests' not in data: |
| 115 continue |
| 116 if not isinstance(data['gtest_tests'], list): |
| 117 raise Error( |
| 118 '%s: %s is broken: %s' % (filename, builder, data['gtest_tests'])) |
| 119 if not all(isinstance(g, dict) for g in data['gtest_tests']): |
| 120 raise Error( |
| 121 '%s: %s is broken: %s' % (filename, builder, data['gtest_tests'])) |
| 122 |
| 123 config[builder]['gtest_tests'] = sorted( |
| 124 data['gtest_tests'], key=lambda x: x['test']) |
| 125 if mode == 'remaining': |
| 126 process_builder_remaining(data, filename, builder, tests_location) |
| 127 elif mode == 'convert': |
| 128 process_builder_convert(data, filename, builder, test_name) |
| 129 |
| 130 expected = json.dumps( |
| 131 config, sort_keys=True, indent=2, separators=(',', ': ')) + '\n' |
| 132 if content != expected: |
| 133 if mode in ('convert', 'write'): |
| 134 with open(filepath, 'wb') as f: |
| 135 f.write(expected) |
| 136 if mode == 'write': |
| 137 print('Updated %s' % filename) |
| 138 else: |
| 139 print('%s is not in canonical format' % filename) |
| 140 print('run `testing/buildbot/manage.py -w` to fix') |
| 141 return mode != 'check' |
| 142 return True |
| 143 |
| 144 |
| 145 def print_remaining(test_name,tests_location): |
| 146 """Prints a visual summary of what tests are yet to be converted to run on |
| 147 Swarming. |
| 148 """ |
| 149 if test_name: |
| 150 if test_name not in tests_location: |
| 151 raise Error('Unknown test %s' % test_name) |
| 152 for config, builders in sorted( |
| 153 tests_location[test_name]['local_configs'].iteritems()): |
| 154 print('%s:' % config) |
| 155 for builder in sorted(builders): |
| 156 print(' %s' % builder) |
| 157 return |
| 158 |
| 159 isolates = get_isolates() |
| 160 l = max(map(len, tests_location)) |
| 161 print('%-*s%sLocal %sSwarming %sMissing isolate' % |
| 162 (l, 'Test', colorama.Fore.RED, colorama.Fore.GREEN, |
| 163 colorama.Fore.MAGENTA)) |
| 164 total_local = 0 |
| 165 total_swarming = 0 |
| 166 for name, location in sorted(tests_location.iteritems()): |
| 167 if not location['count_run_on_swarming']: |
| 168 c = colorama.Fore.RED |
| 169 elif location['count_run_local']: |
| 170 c = colorama.Fore.YELLOW |
| 171 else: |
| 172 c = colorama.Fore.GREEN |
| 173 total_local += location['count_run_local'] |
| 174 total_swarming += location['count_run_on_swarming'] |
| 175 missing_isolate = '' |
| 176 if name + '.isolate' not in isolates: |
| 177 missing_isolate = colorama.Fore.MAGENTA + '*' |
| 178 print('%s%-*s %4d %4d %s' % |
| 179 (c, l, name, location['count_run_local'], |
| 180 location['count_run_on_swarming'], missing_isolate)) |
| 181 |
| 182 total = total_local + total_swarming |
| 183 p_local = 100. * total_local / total |
| 184 p_swarming = 100. * total_swarming / total |
| 185 print('%s%-*s %4d (%4.1f%%) %4d (%4.1f%%)' % |
| 186 (colorama.Fore.WHITE, l, 'Total:', total_local, p_local, |
| 187 total_swarming, p_swarming)) |
| 188 print('%-*s %4d' % (l, 'Total executions:', total)) |
| 189 |
| 190 |
60 def main(): | 191 def main(): |
61 colorama.init() | 192 colorama.init() |
62 parser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__) | 193 parser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__) |
63 group = parser.add_mutually_exclusive_group(required=True) | 194 group = parser.add_mutually_exclusive_group(required=True) |
64 group.add_argument( | 195 group.add_argument( |
65 '-c', '--check', action='store_true', help='Only check the files') | 196 '-c', '--check', dest='mode', action='store_const', const='check', |
| 197 default='check', help='Only check the files') |
66 group.add_argument( | 198 group.add_argument( |
67 '--convert', action='store_true', | 199 '--convert', dest='mode', action='store_const', const='convert', |
68 help='Convert a test to run on Swarming everywhere') | 200 help='Convert a test to run on Swarming everywhere') |
69 group.add_argument( | 201 group.add_argument( |
70 '--remaining', action='store_true', | 202 '--remaining', dest='mode', action='store_const', const='remaining', |
71 help='Count the number of tests not yet running on Swarming') | 203 help='Count the number of tests not yet running on Swarming') |
72 group.add_argument( | 204 group.add_argument( |
73 '-w', '--write', action='store_true', help='Rewrite the files') | 205 '-w', '--write', dest='mode', action='store_const', const='write', |
| 206 help='Rewrite the files') |
74 parser.add_argument( | 207 parser.add_argument( |
75 'test_name', nargs='?', | 208 'test_name', nargs='?', |
76 help='The test name to print which configs to update; only to be used ' | 209 help='The test name to print which configs to update; only to be used ' |
77 'with --remaining') | 210 'with --remaining') |
78 args = parser.parse_args() | 211 args = parser.parse_args() |
79 | 212 |
80 if args.convert or args.remaining: | 213 if args.mode == 'convert': |
81 isolates = get_isolates() | |
82 | |
83 if args.convert: | |
84 if not args.test_name: | 214 if not args.test_name: |
85 parser.error('A test name is required with --convert') | 215 parser.error('A test name is required with --convert') |
86 if args.test_name + '.isolate' not in isolates: | 216 if args.test_name + '.isolate' not in get_isolates(): |
87 parser.error('Create %s.isolate first' % args.test_name) | 217 parser.error('Create %s.isolate first' % args.test_name) |
88 | 218 |
89 # Stats when running in --remaining mode; | 219 # Stats when running in --remaining mode; |
90 tests_location = collections.defaultdict( | 220 tests_location = collections.defaultdict( |
91 lambda: { | 221 lambda: { |
92 'count_run_local': 0, 'count_run_on_swarming': 0, 'local_configs': {} | 222 'count_run_local': 0, 'count_run_on_swarming': 0, 'local_configs': {} |
93 }) | 223 }) |
94 | 224 |
95 result = 0 | 225 try: |
96 for filepath in glob.glob(os.path.join(THIS_DIR, '*.json')): | 226 result = 0 |
97 filename = os.path.basename(filepath) | 227 for filepath in glob.glob(os.path.join(THIS_DIR, '*.json')): |
98 with open(filepath) as f: | 228 if not process_file(args.mode, args.test_name, tests_location, filepath): |
99 content = f.read() | 229 result = 1 |
100 try: | |
101 config = json.loads(content) | |
102 except ValueError as e: | |
103 print "Exception raised while checking %s: %s" % (filepath, e) | |
104 raise | |
105 for builder, data in sorted(config.iteritems()): | |
106 if builder in SKIP: | |
107 # Oddities. | |
108 continue | |
109 | 230 |
110 if not isinstance(data, dict): | 231 if args.mode == 'remaining': |
111 print('%s: %s is broken: %s' % (filename, builder, data)) | 232 print_remaining(args.test_name, tests_location) |
112 continue | 233 return result |
113 | 234 except Error as e: |
114 if 'gtest_tests' in data: | 235 sys.stderr.write('%s\n' % e) |
115 config[builder]['gtest_tests'] = sorted( | 236 return 1 |
116 (upgrade_test(l) for l in data['gtest_tests']), | |
117 key=lambda x: x['test']) | |
118 | |
119 if args.remaining: | |
120 for test in data['gtest_tests']: | |
121 name = test['test'] | |
122 if test.get('swarming', {}).get('can_use_on_swarming_builders'): | |
123 tests_location[name]['count_run_on_swarming'] += 1 | |
124 else: | |
125 tests_location[name]['count_run_local'] += 1 | |
126 tests_location[name]['local_configs'].setdefault( | |
127 filename, []).append(builder) | |
128 elif args.convert: | |
129 for test in data['gtest_tests']: | |
130 if test['test'] != args.test_name: | |
131 continue | |
132 test.setdefault('swarming', {}) | |
133 if not test['swarming'].get('can_use_on_swarming_builders'): | |
134 print('- %s: %s' % (filename, builder)) | |
135 test['swarming']['can_use_on_swarming_builders'] = True | |
136 | |
137 expected = json.dumps( | |
138 config, sort_keys=True, indent=2, separators=(',', ': ')) + '\n' | |
139 if content != expected: | |
140 result = 1 | |
141 if args.write or args.convert: | |
142 with open(filepath, 'wb') as f: | |
143 f.write(expected) | |
144 if args.write: | |
145 print('Updated %s' % filename) | |
146 else: | |
147 print('%s is not in canonical format' % filename) | |
148 | |
149 if args.remaining: | |
150 if args.test_name: | |
151 if args.test_name not in tests_location: | |
152 print('Unknown test %s' % args.test_name) | |
153 return 1 | |
154 for config, builders in sorted( | |
155 tests_location[args.test_name]['local_configs'].iteritems()): | |
156 print('%s:' % config) | |
157 for builder in sorted(builders): | |
158 print(' %s' % builder) | |
159 else: | |
160 l = max(map(len, tests_location)) | |
161 print('%-*s%sLocal %sSwarming %sMissing isolate' % | |
162 (l, 'Test', colorama.Fore.RED, colorama.Fore.GREEN, | |
163 colorama.Fore.MAGENTA)) | |
164 total_local = 0 | |
165 total_swarming = 0 | |
166 for name, location in sorted(tests_location.iteritems()): | |
167 if not location['count_run_on_swarming']: | |
168 c = colorama.Fore.RED | |
169 elif location['count_run_local']: | |
170 c = colorama.Fore.YELLOW | |
171 else: | |
172 c = colorama.Fore.GREEN | |
173 total_local += location['count_run_local'] | |
174 total_swarming += location['count_run_on_swarming'] | |
175 missing_isolate = '' | |
176 if name + '.isolate' not in isolates: | |
177 missing_isolate = colorama.Fore.MAGENTA + '*' | |
178 print('%s%-*s %4d %4d %s' % | |
179 (c, l, name, location['count_run_local'], | |
180 location['count_run_on_swarming'], missing_isolate)) | |
181 | |
182 total = total_local + total_swarming | |
183 p_local = 100. * total_local / total | |
184 p_swarming = 100. * total_swarming / total | |
185 print('%s%-*s %4d (%4.1f%%) %4d (%4.1f%%)' % | |
186 (colorama.Fore.WHITE, l, 'Total:', total_local, p_local, | |
187 total_swarming, p_swarming)) | |
188 print('%-*s %4d' % (l, 'Total executions:', total)) | |
189 return result | |
190 | 237 |
191 | 238 |
192 if __name__ == "__main__": | 239 if __name__ == "__main__": |
193 sys.exit(main()) | 240 sys.exit(main()) |
OLD | NEW |