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