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