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 """ |
11 | 11 |
12 import argparse | 12 import argparse |
13 import collections | |
13 import glob | 14 import glob |
14 import json | 15 import json |
15 import os | 16 import os |
16 import sys | 17 import sys |
17 | 18 |
18 | 19 |
19 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | 20 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
21 sys.path.insert( | |
22 0, os.path.join(THIS_DIR, '..', '..', 'third_party', 'colorama', 'src')) | |
M-A Ruel
2015/04/01 13:06:25
I hadn't realized there was a src directory undern
| |
20 | 23 |
24 import colorama | |
21 | 25 |
22 # These are not 'builders'. | 26 # These are not 'builders'. |
23 SKIP = { | 27 SKIP = { |
24 'compile_targets', 'gtest_tests', 'filter_compile_builders', | 28 'compile_targets', 'gtest_tests', 'filter_compile_builders', |
25 'non_filter_builders', 'non_filter_tests_builders', | 29 'non_filter_builders', 'non_filter_tests_builders', |
26 } | 30 } |
27 | 31 |
28 | 32 |
29 def upgrade_test(test): | 33 def upgrade_test(test): |
30 """Converts from old style string to new style dict.""" | 34 """Converts from old style string to new style dict.""" |
31 if isinstance(test, basestring): | 35 if isinstance(test, basestring): |
32 return {'test': test} | 36 return {'test': test} |
33 assert isinstance(test, dict) | 37 assert isinstance(test, dict) |
34 return test | 38 return test |
35 | 39 |
36 | 40 |
37 def main(): | 41 def main(): |
42 colorama.init() | |
38 parser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__) | 43 parser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__) |
39 group = parser.add_mutually_exclusive_group(required=True) | 44 group = parser.add_mutually_exclusive_group(required=True) |
40 group.add_argument( | 45 group.add_argument( |
41 '-c', '--check', action='store_true', help='Only check the files') | 46 '-c', '--check', action='store_true', help='Only check the files') |
42 group.add_argument( | 47 group.add_argument( |
48 '--remaining', action='store_true', | |
49 help='Count the number of tests not yet running on Swarming') | |
50 group.add_argument( | |
43 '-w', '--write', action='store_true', help='Rewrite the files') | 51 '-w', '--write', action='store_true', help='Rewrite the files') |
52 parser.add_argument( | |
53 'test_name', nargs='?', | |
54 help='The test name to print which configs to update; only to be used ' | |
55 'with --remaining') | |
44 args = parser.parse_args() | 56 args = parser.parse_args() |
45 | 57 |
58 # Stats when running in --remaining mode; | |
59 tests_location = collections.defaultdict( | |
60 lambda: { | |
61 'count_run_local': 0, 'count_run_on_swarming': 0, 'local_configs': {} | |
62 }) | |
63 | |
46 result = 0 | 64 result = 0 |
47 for filepath in glob.glob(os.path.join(THIS_DIR, '*.json')): | 65 for filepath in glob.glob(os.path.join(THIS_DIR, '*.json')): |
48 filename = os.path.basename(filepath) | 66 filename = os.path.basename(filepath) |
49 with open(filepath) as f: | 67 with open(filepath) as f: |
50 content = f.read() | 68 content = f.read() |
51 config = json.loads(content) | 69 config = json.loads(content) |
52 for builder, data in sorted(config.iteritems()): | 70 for builder, data in sorted(config.iteritems()): |
53 if builder in SKIP: | 71 if builder in SKIP: |
54 # Oddities. | 72 # Oddities. |
55 continue | 73 continue |
56 | 74 |
57 if not isinstance(data, dict): | 75 if not isinstance(data, dict): |
58 print('%s: %s is broken: %s' % (filename, builder, data)) | 76 print('%s: %s is broken: %s' % (filename, builder, data)) |
59 continue | 77 continue |
60 | 78 |
61 if 'gtest_tests' in data: | 79 if 'gtest_tests' in data: |
62 config[builder]['gtest_tests'] = sorted( | 80 config[builder]['gtest_tests'] = sorted( |
63 (upgrade_test(l) for l in data['gtest_tests']), | 81 (upgrade_test(l) for l in data['gtest_tests']), |
64 key=lambda x: x['test']) | 82 key=lambda x: x['test']) |
65 | 83 |
84 if args.remaining: | |
85 for test in data['gtest_tests']: | |
86 name = test['test'] | |
87 if test.get('swarming', {}).get('can_use_on_swarming_builders'): | |
88 tests_location[name]['count_run_on_swarming'] += 1 | |
89 else: | |
90 tests_location[name]['count_run_local'] += 1 | |
91 tests_location[name]['local_configs'].setdefault( | |
92 filename, []).append(builder) | |
93 | |
66 expected = json.dumps( | 94 expected = json.dumps( |
67 config, sort_keys=True, indent=2, separators=(',', ': ')) + '\n' | 95 config, sort_keys=True, indent=2, separators=(',', ': ')) + '\n' |
68 if content != expected: | 96 if content != expected: |
69 result = 1 | 97 result = 1 |
70 if args.write: | 98 if args.write: |
71 with open(filepath, 'wb') as f: | 99 with open(filepath, 'wb') as f: |
72 f.write(expected) | 100 f.write(expected) |
73 print('Updated %s' % filename) | 101 print('Updated %s' % filename) |
74 else: | 102 else: |
75 print('%s is not in canonical format' % filename) | 103 print('%s is not in canonical format' % filename) |
104 | |
105 if args.remaining: | |
106 if args.test_name: | |
107 if args.test_name not in tests_location: | |
108 print('Unknown test %s' % args.test_name) | |
109 return 1 | |
110 for config, builders in sorted( | |
111 tests_location[args.test_name]['local_configs'].iteritems()): | |
112 print('%s:' % config) | |
113 for builder in sorted(builders): | |
114 print(' %s' % builder) | |
115 else: | |
116 l = max(map(len, tests_location)) | |
117 print('%-*s%sLocal %sSwarming' % | |
118 (l, 'Test', colorama.Fore.RED, colorama.Fore.GREEN)) | |
119 total_local = 0 | |
120 total_swarming = 0 | |
121 for name, location in sorted(tests_location.iteritems()): | |
122 if not location['count_run_on_swarming']: | |
123 c = colorama.Fore.RED | |
124 elif location['count_run_local']: | |
125 c = colorama.Fore.YELLOW | |
126 else: | |
127 c = colorama.Fore.GREEN | |
128 total_local += location['count_run_local'] | |
129 total_swarming += location['count_run_on_swarming'] | |
130 print('%s%-*s %4d %4d' % | |
131 (c, l, name, location['count_run_local'], | |
132 location['count_run_on_swarming'])) | |
133 total = total_local + total_swarming | |
134 p_local = 100. * total_local / total | |
135 p_swarming = 100. * total_swarming / total | |
136 print('%s%-*s %4d (%4.1f%%) %4d (%4.1f%%)' % | |
137 (colorama.Fore.WHITE, l, 'Total:', total_local, p_local, | |
138 total_swarming, p_swarming)) | |
139 print('%-*s %4d' % (l, 'Total executions:', total)) | |
76 return result | 140 return result |
77 | 141 |
78 | 142 |
79 if __name__ == "__main__": | 143 if __name__ == "__main__": |
80 sys.exit(main()) | 144 sys.exit(main()) |
OLD | NEW |