2 # Copyright 2017 The Chromium Authors
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 from __future__ import print_function
17 def StandardIsolatedScriptMerge(output_json, summary_json, jsons_to_merge):
18 """Merge the contents of one or more results JSONs into a single JSON.
21 output_json: A path to a JSON file to which the merged results should be
23 jsons_to_merge: A list of paths to JSON files that should be merged.
25 # summary.json is produced by swarming client itself. We are mostly interested
26 # in the number of shards.
28 with open(summary_json) as f:
29 summary = json.load(f)
30 except (IOError, ValueError):
32 'summary.json is missing or can not be read',
33 'Something is seriously wrong with swarming client or the bot.'),
38 shard_results_list = []
39 for index, result in enumerate(summary['shards']):
42 output_path = find_shard_output_path(index, result.get('task_id'),
45 missing_shards.append(index)
48 with open(output_path) as f:
50 json_contents = json.load(f)
51 except ValueError as e:
52 six.raise_from(ValueError(
53 'Failed to parse JSON from %s' % output_path), e)
54 shard_results_list.append(json_contents)
56 merged_results = results_merger.merge_test_results(shard_results_list)
58 merged_results['missing_shards'] = missing_shards
59 if 'global_tags' not in merged_results:
60 merged_results['global_tags'] = []
61 merged_results['global_tags'].append('UNRELIABLE_RESULTS')
63 with open(output_json, 'w') as f:
64 json.dump(merged_results, f)
69 def find_shard_output_path(index, task_id, jsons_to_merge):
70 """Finds the shard matching the index/task-id.
73 index: The index of the shard to load data for, this is for old api.
74 task_id: The directory of the shard to load data for, this is for new api.
75 jsons_to_merge: A container of file paths for shards that emitted output.
78 * The matching path, or None
80 # 'output.json' is set in swarming/api.py, gtest_task method.
81 matching_json_files = [
82 j for j in jsons_to_merge
83 if (os.path.basename(j) == 'output.json' and
84 (os.path.basename(os.path.dirname(j)) == str(index) or
85 os.path.basename(os.path.dirname(j)) == task_id))]
87 if not matching_json_files:
88 print('shard %s test output missing' % index, file=sys.stderr)
90 if len(matching_json_files) > 1:
91 print('duplicate test output for shard %s' % index, file=sys.stderr)
94 return matching_json_files[0]
98 parser = merge_api.ArgumentParser()
99 args = parser.parse_args(raw_args)
100 return StandardIsolatedScriptMerge(
101 args.output_json, args.summary_json, args.jsons_to_merge)
104 if __name__ == '__main__':
105 sys.exit(main(sys.argv[1:]))