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"""Script to patch Android repo with diffs that are needed by the suite.
7
8Run this script before running the suite.
9"""
10from __future__ import print_function
11
12import config
13import os
14import subprocess
15
16# The patches to be added to the android repo.
17# An error may occur if it is already patched, or meets some error.
18# FIXME: Needs to be FIXED in the future.
19def try_patch_autotest():
20    # Patch autotest, which includes all the testcases on device,
21    # setting device, and running the benchmarks
22    autotest_dir = os.path.join(config.android_home, config.autotest_dir)
23    autotest_patch = os.path.join(
24        os.path.dirname(os.path.realpath(__file__)), 'autotest.diff')
25    dex2oat_dir = os.path.join(autotest_dir,
26                               'server/site_tests/android_Dex2oat')
27    panorama_dir = os.path.join(autotest_dir,
28                                'server/site_tests/android_Panorama')
29    # FIXME: A quick hack, need to handle errors and check whether has been
30    # applied in the future.
31    try:
32        subprocess.check_call(['git', '-C', autotest_dir,
33                               'apply', autotest_patch])
34        subprocess.check_call(['cp', '-rf', 'dex2oat_input', dex2oat_dir])
35        subprocess.check_call(['cp', '-rf', 'panorama_input', panorama_dir])
36        print('Autotest patched successfully!')
37    except subprocess.CalledProcessError:
38        print('Autotest patch not applied, error or already patched.')
39
40
41def try_patch_panorama():
42    panorama_dir = os.path.join(config.android_home,
43                                config.bench_dict['Panorama'])
44    panorama_patch = os.path.join(
45        os.path.dirname(os.path.realpath(__file__)), 'panorama.diff')
46    # FIXME: A quick hack, need to handle errors and check whether has been
47    # applied in the future.
48    try:
49        subprocess.check_call(['mkdir', '-p', panorama_dir])
50        subprocess.check_call(['git', '-C', panorama_dir,
51                               'apply', panorama_patch])
52        print('Panorama patched successfully!')
53    except subprocess.CalledProcessError:
54        print('Panorama patch not applied, error or already patched.')
55
56
57def try_patch_synthmark():
58    synthmark_dir = '/tmp/devrel/tools/synthmark'
59    # FIXME: A quick hack, need to handle errors and check whether has been
60    # applied in the future.
61    try:
62        subprocess.check_call([
63            'bash', '-c', 'cd /tmp && '
64            'rm -rf devrel && '
65            'mkdir devrel && '
66            'cd devrel && '
67            'repo init -u sso://devrel/manifest && '
68            'repo sync tools/synthmark'
69        ])
70        synthmark_patch = os.path.join(
71            os.path.dirname(os.path.realpath(__file__)), 'synthmark.diff')
72        subprocess.check_call(['git', '-C', synthmark_dir,
73                               'apply', synthmark_patch])
74
75        subprocess.check_call(['mv', '-f', synthmark_dir, config.android_home])
76        subprocess.check_call(['rm', '-rf', '/tmp/devrel'])
77        print('Synthmark patched successfully!')
78    except subprocess.CalledProcessError:
79        print('Synthmark patch not applied, error or already patched.')
80
81
82def main():
83    try_patch_panorama()
84    try_patch_autotest()
85    try_patch_synthmark()
86
87if __name__ == '__main__':
88    main()
89