OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 """Toolbox to manage all the json files in this directory. | |
7 | |
8 It can reformat them in their canonical format or ensures they are well | |
9 formatted. | |
10 """ | |
11 | |
12 import argparse | |
13 import glob | |
14 import json | |
15 import os | |
16 import sys | |
17 | |
18 | |
19 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | |
20 | |
21 | |
22 # These are not 'builders'. | |
23 SKIP = { | |
24 'compile_targets', 'gtest_tests', 'filter_compile_builders', | |
25 'non_filter_builders', 'non_filter_tests_builders', | |
26 } | |
27 | |
28 | |
29 def upgrade_test(test): | |
30 """Converts from old style string to new style dict.""" | |
31 if isinstance(test, basestring): | |
32 return {'test': test} | |
33 assert isinstance(test, dict) | |
34 return test | |
35 | |
36 | |
37 def main(): | |
38 parser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__) | |
39 group = parser.add_mutually_exclusive_group(required=True) | |
40 group.add_argument( | |
41 '-c', '--check', action='store_true', help='Only check the files') | |
42 group.add_argument( | |
43 '-w', '--write', action='store_true', help='Rewrite the files') | |
44 args = parser.parse_args() | |
45 | |
46 result = 0 | |
47 for filepath in glob.glob(os.path.join(THIS_DIR, '*.json')): | |
48 filename = os.path.basename(filepath) | |
49 with open(filepath) as f: | |
50 content = f.read() | |
51 config = json.loads(content) | |
52 for builder, data in sorted(config.iteritems()): | |
53 if builder in SKIP: | |
54 # Oddities. | |
55 continue | |
56 | |
57 if not isinstance(data, dict): | |
58 print('%s: %s is broken: %s' % (filename, builder, data)) | |
59 continue | |
60 | |
61 if 'gtest_tests' in data: | |
62 config[builder]['gtest_tests'] = sorted( | |
63 (upgrade_test(l) for l in data['gtest_tests']), | |
64 key=lambda x: x['test']) | |
65 | |
66 expected = json.dumps( | |
67 config, sort_keys=True, indent=2, separators=(',', ': ')) + '\n' | |
68 if content != expected: | |
69 result = 1 | |
70 if args.write: | |
71 with open(filepath, 'wb') as f: | |
72 f.write(expected) | |
73 print('Updated %s' % filename) | |
74 else: | |
75 print('%s is not in canonical format' % filename) | |
Nico
2015/05/29 04:38:08
This is a pretty useless output. Maybe it could pr
| |
76 return result | |
77 | |
78 | |
79 if __name__ == "__main__": | |
80 sys.exit(main()) | |
OLD | NEW |