1#!/usr/bin/env python
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Downloads simpleperf prebuilts from the build server."""
18import argparse
19import logging
20import os
21import shutil
22import stat
23import textwrap
24
25THIS_DIR = os.path.realpath(os.path.dirname(__file__))
26
27
28class InstallEntry(object):
29    def __init__(self, target, name, install_path, need_strip=False):
30        self.target = target
31        self.name = name
32        self.install_path = install_path
33        self.need_strip = need_strip
34
35
36MINGW = 'local:../../../../prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/'
37INSTALL_LIST = [
38    # simpleperf on device.
39    InstallEntry('MODULES-IN-system-extras-simpleperf',
40                 'simpleperf/android/arm64/simpleperf_ndk64',
41                 'android/arm64/simpleperf'),
42    InstallEntry('MODULES-IN-system-extras-simpleperf',
43                 'simpleperf/android/arm/simpleperf_ndk',
44                 'android/arm/simpleperf'),
45    InstallEntry('MODULES-IN-system-extras-simpleperf_x86',
46                 'simpleperf/android/x86_64/simpleperf_ndk64',
47                 'android/x86_64/simpleperf'),
48    InstallEntry('MODULES-IN-system-extras-simpleperf_x86',
49                 'simpleperf/android/x86/simpleperf_ndk',
50                 'android/x86/simpleperf'),
51
52    # simpleperf on host. Linux and macOS are 64-bit only these days.
53    InstallEntry('MODULES-IN-system-extras-simpleperf',
54                 'simpleperf/linux/x86_64/simpleperf_ndk64',
55                 'linux/x86_64/simpleperf', True),
56    InstallEntry('MODULES-IN-system-extras-simpleperf_mac',
57                 'simpleperf/darwin/x86_64/simpleperf_ndk64',
58                 'darwin/x86_64/simpleperf'),
59    InstallEntry('MODULES-IN-system-extras-simpleperf',
60                 'simpleperf/windows/x86_64/simpleperf_ndk64.exe',
61                 'windows/x86_64/simpleperf.exe', True),
62    InstallEntry('MODULES-IN-system-extras-simpleperf',
63                 'simpleperf/windows/x86/simpleperf_ndk.exe',
64                 'windows/x86/simpleperf.exe', True),
65
66    # libsimpleperf_report.so on host
67    InstallEntry('MODULES-IN-system-extras-simpleperf',
68                 'simpleperf/linux/x86_64/libsimpleperf_report.so',
69                 'linux/x86_64/libsimpleperf_report.so', True),
70    InstallEntry('MODULES-IN-system-extras-simpleperf_mac',
71                 'simpleperf/darwin/x86_64/libsimpleperf_report.dylib',
72                 'darwin/x86_64/libsimpleperf_report.dylib'),
73    InstallEntry('MODULES-IN-system-extras-simpleperf',
74                 'simpleperf/windows/x86_64/libsimpleperf_report.dll',
75                 'windows/x86_64/libsimpleperf_report.dll', True),
76    InstallEntry('MODULES-IN-system-extras-simpleperf',
77                 'simpleperf/windows/x86/libsimpleperf_report.dll',
78                 'windows/x86/libsimpleperf_report.dll', True),
79
80    # libwinpthread-1.dll on windows host
81    InstallEntry(MINGW + '/bin/libwinpthread-1.dll', 'libwinpthread-1.dll',
82                 'windows/x86_64/libwinpthread-1.dll', False),
83    InstallEntry(MINGW + '/lib32/libwinpthread-1.dll',
84                 'libwinpthread-1_32.dll',
85                 'windows/x86/libwinpthread-1.dll',
86                 False),
87]
88
89
90def logger():
91    """Returns the main logger for this module."""
92    return logging.getLogger(__name__)
93
94
95def check_call(cmd):
96    """Proxy for subprocess.check_call with logging."""
97    import subprocess
98    logger().debug('check_call `%s`', ' '.join(cmd))
99    subprocess.check_call(cmd)
100
101
102def fetch_artifact(branch, build, target, name):
103    """Fetches and artifact from the build server."""
104    if target.startswith('local:'):
105        shutil.copyfile(target[6:], name)
106        return
107    logger().info('Fetching %s from %s %s (artifacts matching %s)', build,
108                  target, branch, name)
109    fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact'
110    cmd = [fetch_artifact_path, '--branch', branch, '--target', target,
111           '--bid', build, name]
112    check_call(cmd)
113
114
115def start_branch(build):
116    """Creates a new branch in the project."""
117    branch_name = 'update-' + (build or 'latest')
118    logger().info('Creating branch %s', branch_name)
119    check_call(['repo', 'start', branch_name, '.'])
120
121
122def commit(branch, build, add_paths):
123    """Commits the new prebuilts."""
124    logger().info('Making commit')
125    check_call(['git', 'add'] + add_paths)
126    message = textwrap.dedent("""\
127        simpleperf: update simpleperf prebuilts to build {build}.
128
129        Taken from branch {branch}.""").format(branch=branch, build=build)
130    check_call(['git', 'commit', '-m', message])
131
132
133def remove_old_release(install_dir):
134    """Removes the old prebuilts."""
135    if os.path.exists(install_dir):
136        logger().info('Removing old install directory "%s"', install_dir)
137        check_call(['git', 'rm', '-rf', '--ignore-unmatch', install_dir])
138
139    # Need to check again because git won't remove directories if they have
140    # non-git files in them.
141    if os.path.exists(install_dir):
142        shutil.rmtree(install_dir)
143
144
145def install_new_release(branch, build, install_dir):
146    """Installs the new release."""
147    for entry in INSTALL_LIST:
148        install_entry(branch, build, install_dir, entry)
149
150
151def install_entry(branch, build, install_dir, entry):
152    """Installs the device specific components of the release."""
153    target = entry.target
154    name = entry.name
155    install_path = os.path.join(install_dir, entry.install_path)
156    need_strip = entry.need_strip
157
158    fetch_artifact(branch, build, target, name)
159    name = os.path.basename(name)
160    exe_stat = os.stat(name)
161    os.chmod(name, exe_stat.st_mode | stat.S_IEXEC)
162    if need_strip:
163        check_call(['strip', name])
164    dirname = os.path.dirname(install_path)
165    if not os.path.isdir(dirname):
166        os.makedirs(dirname)
167    shutil.move(name, install_path)
168
169
170def get_args():
171    """Parses and returns command line arguments."""
172    parser = argparse.ArgumentParser()
173
174    parser.add_argument(
175        '-b', '--branch', default='aosp-simpleperf-release',
176        help='Branch to pull build from.')
177    parser.add_argument('--build', required=True, help='Build number to pull.')
178    parser.add_argument(
179        '--use-current-branch', action='store_true',
180        help='Perform the update in the current branch. Do not repo start.')
181    parser.add_argument(
182        '-v', '--verbose', action='count', default=0,
183        help='Increase output verbosity.')
184
185    return parser.parse_args()
186
187
188def main():
189    """Program entry point."""
190    os.chdir(THIS_DIR)
191
192    args = get_args()
193    verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG)
194    verbosity = args.verbose
195    if verbosity > 2:
196        verbosity = 2
197    logging.basicConfig(level=verbose_map[verbosity])
198
199    install_dir = 'bin'
200
201    if not args.use_current_branch:
202        start_branch(args.build)
203    remove_old_release(install_dir)
204    install_new_release(args.branch, args.build, install_dir)
205    artifacts = [install_dir]
206    commit(args.branch, args.build, artifacts)
207
208
209if __name__ == '__main__':
210    main()
211