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.
6 """Toolbox to manage all the json files in this directory.
8 It can reformat them in their canonical format or ensures they are well
20 THIS_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
22 0, os
.path
.join(THIS_DIR
, '..', '..', 'third_party', 'colorama', 'src'))
26 # These are not 'builders'.
28 'compile_targets', 'gtest_tests', 'filter_compile_builders',
29 'non_filter_builders', 'non_filter_tests_builders',
33 def upgrade_test(test
):
34 """Converts from old style string to new style dict."""
35 if isinstance(test
, basestring
):
37 assert isinstance(test
, dict)
43 parser
= argparse
.ArgumentParser(description
=sys
.modules
[__name__
].__doc
__)
44 group
= parser
.add_mutually_exclusive_group(required
=True)
46 '-c', '--check', action
='store_true', help='Only check the files')
48 '--convert', action
='store_true',
49 help='Convert a test to run on Swarming everywhere')
51 '--remaining', action
='store_true',
52 help='Count the number of tests not yet running on Swarming')
54 '-w', '--write', action
='store_true', help='Rewrite the files')
56 'test_name', nargs
='?',
57 help='The test name to print which configs to update; only to be used '
59 args
= parser
.parse_args()
61 if args
.convert
and not args
.test_name
:
62 parser
.error('A test name is required with --convert')
64 # Stats when running in --remaining mode;
65 tests_location
= collections
.defaultdict(
67 'count_run_local': 0, 'count_run_on_swarming': 0, 'local_configs': {}
71 for filepath
in glob
.glob(os
.path
.join(THIS_DIR
, '*.json')):
72 filename
= os
.path
.basename(filepath
)
73 with
open(filepath
) as f
:
75 config
= json
.loads(content
)
76 for builder
, data
in sorted(config
.iteritems()):
81 if not isinstance(data
, dict):
82 print('%s: %s is broken: %s' % (filename
, builder
, data
))
85 if 'gtest_tests' in data
:
86 config
[builder
]['gtest_tests'] = sorted(
87 (upgrade_test(l
) for l
in data
['gtest_tests']),
88 key
=lambda x
: x
['test'])
91 for test
in data
['gtest_tests']:
93 if test
.get('swarming', {}).get('can_use_on_swarming_builders'):
94 tests_location
[name
]['count_run_on_swarming'] += 1
96 tests_location
[name
]['count_run_local'] += 1
97 tests_location
[name
]['local_configs'].setdefault(
98 filename
, []).append(builder
)
100 for test
in data
['gtest_tests']:
101 if test
['test'] != args
.test_name
:
103 test
.setdefault('swarming', {})['can_use_on_swarming_builders'] = (
106 expected
= json
.dumps(
107 config
, sort_keys
=True, indent
=2, separators
=(',', ': ')) + '\n'
108 if content
!= expected
:
110 if args
.write
or args
.convert
:
111 with
open(filepath
, 'wb') as f
:
113 print('Updated %s' % filename
)
115 print('%s is not in canonical format' % filename
)
119 if args
.test_name
not in tests_location
:
120 print('Unknown test %s' % args
.test_name
)
122 for config
, builders
in sorted(
123 tests_location
[args
.test_name
]['local_configs'].iteritems()):
124 print('%s:' % config
)
125 for builder
in sorted(builders
):
126 print(' %s' % builder
)
128 l
= max(map(len, tests_location
))
129 print('%-*s%sLocal %sSwarming' %
130 (l
, 'Test', colorama
.Fore
.RED
, colorama
.Fore
.GREEN
))
133 for name
, location
in sorted(tests_location
.iteritems()):
134 if not location
['count_run_on_swarming']:
135 c
= colorama
.Fore
.RED
136 elif location
['count_run_local']:
137 c
= colorama
.Fore
.YELLOW
139 c
= colorama
.Fore
.GREEN
140 total_local
+= location
['count_run_local']
141 total_swarming
+= location
['count_run_on_swarming']
142 print('%s%-*s %4d %4d' %
143 (c
, l
, name
, location
['count_run_local'],
144 location
['count_run_on_swarming']))
145 total
= total_local
+ total_swarming
146 p_local
= 100. * total_local
/ total
147 p_swarming
= 100. * total_swarming
/ total
148 print('%s%-*s %4d (%4.1f%%) %4d (%4.1f%%)' %
149 (colorama
.Fore
.WHITE
, l
, 'Total:', total_local
, p_local
,
150 total_swarming
, p_swarming
))
151 print('%-*s %4d' % (l
, 'Total executions:', total
))
155 if __name__
== "__main__":