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