• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
25
26THIS_DIR = os.path.realpath(os.path.dirname(__file__))
27
28
29class InstallEntry(object):
30    def __init__(self, target, name, install_path, need_strip=False):
31        self.target = target
32        self.name = name
33        self.install_path = install_path
34        self.need_strip = need_strip
35
36
37INSTALL_LIST = [
38    # simpleperf on device
39    InstallEntry('sdk_arm64-sdk', 'simpleperf', 'android/arm64/simpleperf'),
40    InstallEntry('sdk_arm64-sdk', 'simpleperf32', 'android/arm/simpleperf'),
41    InstallEntry('sdk_x86_64-sdk', 'simpleperf', 'android/x86_64/simpleperf'),
42    InstallEntry('sdk_x86_64-sdk', 'simpleperf32', 'android/x86/simpleperf'),
43
44    # simpleperf on host
45    InstallEntry('sdk_arm64-sdk', 'simpleperf_host', 'linux/x86_64/simpleperf', True),
46    InstallEntry('sdk_arm64-sdk', 'simpleperf_host32', 'linux/x86/simpleperf', True),
47    InstallEntry('sdk_mac', 'simpleperf_host', 'darwin/x86_64/simpleperf'),
48    InstallEntry('sdk_mac', 'simpleperf_host32', 'darwin/x86/simpleperf'),
49    # simpleperf.exe on x86_64 windows doesn't work, use simpleperf32.exe instead.
50    InstallEntry('sdk', 'simpleperf32.exe', 'windows/x86_64/simpleperf.exe', True),
51    InstallEntry('sdk', 'simpleperf32.exe', 'windows/x86/simpleperf.exe', True),
52
53    # libsimpleperf_report.so on host
54    InstallEntry('sdk_arm64-sdk', 'libsimpleperf_report.so', 'linux/x86_64/libsimpleperf_report.so',
55                 True),
56    InstallEntry('sdk_arm64-sdk', 'libsimpleperf_report32.so', 'linux/x86/libsimpleperf_report.so',
57                 True),
58    InstallEntry('sdk_mac', 'libsimpleperf_report.dylib',
59                 'darwin/x86_64/libsimpleperf_report.dylib'),
60    InstallEntry('sdk_mac', 'libsimpleperf_report32.so', 'darwin/x86/libsimpleperf_report.dylib'),
61    InstallEntry('sdk', 'libsimpleperf_report.dll', 'windows/x86_64/libsimpleperf_report.dll',
62                 True),
63    InstallEntry('sdk', 'libsimpleperf_report32.dll', 'windows/x86/libsimpleperf_report.dll', True),
64
65    # libwinpthread-1.dll on windows host
66    InstallEntry('local:../../../../prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8' +
67                 '/x86_64-w64-mingw32/bin/libwinpthread-1.dll', 'libwinpthread-1.dll',
68                 'windows/x86_64/libwinpthread-1.dll', False),
69    InstallEntry('local:../../../../prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8' +
70                 '/x86_64-w64-mingw32/lib32/libwinpthread-1.dll', 'libwinpthread-1_32.dll',
71                 'windows/x86/libwinpthread-1.dll', False),
72]
73
74
75def logger():
76    """Returns the main logger for this module."""
77    return logging.getLogger(__name__)
78
79
80def check_call(cmd):
81    """Proxy for subprocess.check_call with logging."""
82    import subprocess
83    logger().debug('check_call `%s`', ' '.join(cmd))
84    subprocess.check_call(cmd)
85
86
87def fetch_artifact(branch, build, target, name):
88    """Fetches and artifact from the build server."""
89    if target.startswith('local:'):
90        shutil.copyfile(target[6:], name)
91        return
92    logger().info('Fetching %s from %s %s (artifacts matching %s)', build,
93                  target, branch, name)
94    fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact'
95    cmd = [fetch_artifact_path, '--branch', branch, '--target', target,
96           '--bid', build, name]
97    check_call(cmd)
98
99
100def start_branch(build):
101    """Creates a new branch in the project."""
102    branch_name = 'update-' + (build or 'latest')
103    logger().info('Creating branch %s', branch_name)
104    check_call(['repo', 'start', branch_name, '.'])
105
106
107def commit(branch, build, add_paths):
108    """Commits the new prebuilts."""
109    logger().info('Making commit')
110    check_call(['git', 'add'] + add_paths)
111    message = textwrap.dedent("""\
112        simpleperf: update simpleperf prebuilts to build {build}.
113
114        Taken from branch {branch}.""").format(branch=branch, build=build)
115    check_call(['git', 'commit', '-m', message])
116
117
118def remove_old_release(install_dir):
119    """Removes the old prebuilts."""
120    if os.path.exists(install_dir):
121        logger().info('Removing old install directory "%s"', install_dir)
122        check_call(['git', 'rm', '-rf', '--ignore-unmatch', install_dir])
123
124    # Need to check again because git won't remove directories if they have
125    # non-git files in them.
126    if os.path.exists(install_dir):
127        shutil.rmtree(install_dir)
128
129
130def install_new_release(branch, build, install_dir):
131    """Installs the new release."""
132    for entry in INSTALL_LIST:
133        install_entry(branch, build, install_dir, entry)
134
135
136def install_entry(branch, build, install_dir, entry):
137    """Installs the device specific components of the release."""
138    target = entry.target
139    name = entry.name
140    install_path = os.path.join(install_dir, entry.install_path)
141    need_strip = entry.need_strip
142
143    fetch_artifact(branch, build, target, name)
144    exe_stat = os.stat(name)
145    os.chmod(name, exe_stat.st_mode | stat.S_IEXEC)
146    if need_strip:
147        check_call(['strip', name])
148    dirname = os.path.dirname(install_path)
149    if not os.path.isdir(dirname):
150        os.makedirs(dirname)
151    shutil.move(name, install_path)
152
153
154def get_args():
155    """Parses and returns command line arguments."""
156    parser = argparse.ArgumentParser()
157
158    parser.add_argument(
159        '-b', '--branch', default='aosp-master',
160        help='Branch to pull build from.')
161    parser.add_argument('--build', required=True, help='Build number to pull.')
162    parser.add_argument(
163        '--use-current-branch', action='store_true',
164        help='Perform the update in the current branch. Do not repo start.')
165    parser.add_argument(
166        '-v', '--verbose', action='count', default=0,
167        help='Increase output verbosity.')
168
169    return parser.parse_args()
170
171
172def main():
173    """Program entry point."""
174    os.chdir(THIS_DIR)
175
176    args = get_args()
177    verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG)
178    verbosity = args.verbose
179    if verbosity > 2:
180        verbosity = 2
181    logging.basicConfig(level=verbose_map[verbosity])
182
183    install_dir = 'bin'
184
185    if not args.use_current_branch:
186        start_branch(args.build)
187    remove_old_release(install_dir)
188    install_new_release(args.branch, args.build, install_dir)
189    artifacts = [install_dir]
190    commit(args.branch, args.build, artifacts)
191
192
193if __name__ == '__main__':
194    main()
195