1#!/usr/bin/env python2
2#
3# Copyright 2017 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# pylint: disable=cros-logging-import
8
9"""Script to re-format json result to one with branch_name and build_id"""
10from __future__ import print_function
11
12import argparse
13import config
14import json
15import logging
16import os
17import subprocess
18import sys
19
20# Turn the logging level to INFO before importing other autotest
21# code, to avoid having failed import logging messages confuse the
22# test_droid user.
23logging.basicConfig(level=logging.INFO)
24
25
26def _parse_arguments_internal(argv):
27    parser = argparse.ArgumentParser(description='Convert result to JSON'
28                                     'format')
29    parser.add_argument(
30        '-b', '--bench', help='Generate JSON format file for which benchmark.')
31    return parser.parse_args(argv)
32
33def fix_json(bench):
34    # Set environment variable for crosperf
35    os.environ['PYTHONPATH'] = os.path.dirname(config.toolchain_utils)
36
37    logging.info('Generating Crosperf Report...')
38    json_path = os.path.join(config.bench_suite_dir, bench + '_refined')
39    crosperf_cmd = [
40        os.path.join(config.toolchain_utils, 'generate_report.py'), '--json',
41        '-i=' + os.path.join(config.bench_suite_dir, bench + '.json'),
42        '-o=' + json_path, '-f'
43    ]
44
45    # Run crosperf generate_report.py
46    logging.info('Command: %s', crosperf_cmd)
47    subprocess.call(crosperf_cmd)
48
49    json_path += '.json'
50    with open(json_path) as fout:
51        objs = json.load(fout)
52    for obj in objs:
53        obj['branch_name'] = 'aosp/master'
54        obj['build_id'] = 0
55    with open(json_path, 'w') as fout:
56        json.dump(objs, fout)
57
58    logging.info('JSON file fixed successfully!')
59
60def main(argv):
61    arguments = _parse_arguments_internal(argv)
62
63    bench = arguments.bench
64
65    fix_json(bench)
66
67if __name__ == '__main__':
68    main(sys.argv[1:])
69