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