Yabin Cui | deb7b5f | 2021-04-15 16:57:01 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 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.""" |
| 18 | import argparse |
| 19 | import logging |
| 20 | import os |
| 21 | import shutil |
| 22 | import stat |
| 23 | import textwrap |
| 24 | |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 25 | THIS_DIR = os.path.realpath(os.path.dirname(__file__)) |
| 26 | |
| 27 | |
| 28 | class 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 | |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 36 | MINGW = 'local:../../../../prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/' |
Yabin Cui | 370c341 | 2018-06-12 17:11:37 -0700 | [diff] [blame] | 37 | INSTALL_LIST = [ |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 38 | # simpleperf on device. |
| 39 | InstallEntry('MODULES-IN-system-extras-simpleperf', |
| 40 | 'simpleperf/android/arm64/simpleperf_ndk64', |
| 41 | 'android/arm64/simpleperf'), |
Yabin Cui | 0d6380a | 2021-08-17 12:09:13 -0700 | [diff] [blame] | 42 | InstallEntry('MODULES-IN-system-extras-simpleperf_arm', |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 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'), |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 51 | |
Yabin Cui | db30352 | 2021-03-17 14:09:52 -0700 | [diff] [blame] | 52 | # simpleperf on host. |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 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'), |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 59 | InstallEntry('MODULES-IN-system-extras-simpleperf', |
Yabin Cui | af31dd8 | 2019-05-29 10:35:46 -0700 | [diff] [blame] | 60 | 'simpleperf/windows/x86_64/simpleperf_ndk64.exe', |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 61 | 'windows/x86_64/simpleperf.exe', True), |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 62 | |
| 63 | # libsimpleperf_report.so on host |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 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', |
Yabin Cui | 370c341 | 2018-06-12 17:11:37 -0700 | [diff] [blame] | 69 | 'darwin/x86_64/libsimpleperf_report.dylib'), |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 70 | InstallEntry('MODULES-IN-system-extras-simpleperf', |
| 71 | 'simpleperf/windows/x86_64/libsimpleperf_report.dll', |
| 72 | 'windows/x86_64/libsimpleperf_report.dll', True), |
Yabin Cui | cbddd38 | 2017-05-03 14:46:55 -0700 | [diff] [blame] | 73 | |
| 74 | # libwinpthread-1.dll on windows host |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 75 | InstallEntry(MINGW + '/bin/libwinpthread-1.dll', 'libwinpthread-1.dll', |
Yabin Cui | 370c341 | 2018-06-12 17:11:37 -0700 | [diff] [blame] | 76 | 'windows/x86_64/libwinpthread-1.dll', False), |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 77 | ] |
| 78 | |
| 79 | |
| 80 | def logger(): |
| 81 | """Returns the main logger for this module.""" |
| 82 | return logging.getLogger(__name__) |
| 83 | |
| 84 | |
| 85 | def 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 | |
Yabin Cui | b0d308d | 2017-05-08 11:29:12 -0700 | [diff] [blame] | 92 | def fetch_artifact(branch, build, target, name): |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 93 | """Fetches and artifact from the build server.""" |
Yabin Cui | b0d308d | 2017-05-08 11:29:12 -0700 | [diff] [blame] | 94 | if target.startswith('local:'): |
| 95 | shutil.copyfile(target[6:], name) |
Yabin Cui | cbddd38 | 2017-05-03 14:46:55 -0700 | [diff] [blame] | 96 | return |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 97 | logger().info('Fetching %s from %s %s (artifacts matching %s)', build, |
Yabin Cui | b0d308d | 2017-05-08 11:29:12 -0700 | [diff] [blame] | 98 | target, branch, name) |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 99 | fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact' |
| 100 | cmd = [fetch_artifact_path, '--branch', branch, '--target', target, |
Yabin Cui | b0d308d | 2017-05-08 11:29:12 -0700 | [diff] [blame] | 101 | '--bid', build, name] |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 102 | check_call(cmd) |
| 103 | |
| 104 | |
| 105 | def 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 | |
| 112 | def 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 | |
| 123 | def 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 | |
| 135 | def install_new_release(branch, build, install_dir): |
| 136 | """Installs the new release.""" |
Yabin Cui | 370c341 | 2018-06-12 17:11:37 -0700 | [diff] [blame] | 137 | for entry in INSTALL_LIST: |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 138 | install_entry(branch, build, install_dir, entry) |
| 139 | |
| 140 | |
| 141 | def 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) |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 149 | name = os.path.basename(name) |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 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]) |
Yabin Cui | 370c341 | 2018-06-12 17:11:37 -0700 | [diff] [blame] | 154 | dirname = os.path.dirname(install_path) |
| 155 | if not os.path.isdir(dirname): |
| 156 | os.makedirs(dirname) |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 157 | shutil.move(name, install_path) |
| 158 | |
| 159 | |
| 160 | def get_args(): |
| 161 | """Parses and returns command line arguments.""" |
| 162 | parser = argparse.ArgumentParser() |
| 163 | |
| 164 | parser.add_argument( |
Elliott Hughes | 5746ce4 | 2019-04-04 14:04:51 -0700 | [diff] [blame] | 165 | '-b', '--branch', default='aosp-simpleperf-release', |
Yabin Cui | b3a66f1 | 2017-01-05 12:01:26 -0800 | [diff] [blame] | 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 | |
| 178 | def 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 | |
| 199 | if __name__ == '__main__': |
Yabin Cui | 370c341 | 2018-06-12 17:11:37 -0700 | [diff] [blame] | 200 | main() |