Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 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 | import argparse |
| 18 | import glob |
| 19 | import logging |
| 20 | import os |
| 21 | import shutil |
| 22 | import subprocess |
Dan Albert | 2e1bb71 | 2021-03-18 16:49:39 -0700 | [diff] [blame] | 23 | from tempfile import TemporaryDirectory |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 24 | import textwrap |
| 25 | |
| 26 | |
| 27 | THIS_DIR = os.path.realpath(os.path.dirname(__file__)) |
| 28 | |
| 29 | |
| 30 | def logger(): |
| 31 | return logging.getLogger(__name__) |
| 32 | |
| 33 | |
| 34 | def check_call(cmd): |
| 35 | logger().debug('Running `%s`', ' '.join(cmd)) |
| 36 | subprocess.check_call(cmd) |
| 37 | |
| 38 | |
Dan Albert | 260ec6d | 2017-12-11 11:41:18 -0800 | [diff] [blame] | 39 | def remove(path): |
| 40 | logger().debug('remove `%s`', path) |
| 41 | os.remove(path) |
| 42 | |
| 43 | |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 44 | def fetch_artifact(branch, build, pattern): |
| 45 | fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact' |
| 46 | cmd = [fetch_artifact_path, '--branch', branch, '--target=linux', |
| 47 | '--bid', build, pattern] |
| 48 | check_call(cmd) |
| 49 | |
| 50 | |
| 51 | def api_str(api_level): |
| 52 | return 'android-{}'.format(api_level) |
| 53 | |
| 54 | |
| 55 | def start_branch(build): |
| 56 | branch_name = 'update-' + (build or 'latest') |
| 57 | logger().info('Creating branch %s', branch_name) |
| 58 | check_call(['repo', 'start', branch_name, '.']) |
| 59 | |
| 60 | |
| 61 | def remove_old_release(install_dir): |
| 62 | if os.path.exists(os.path.join(install_dir, '.git')): |
| 63 | logger().info('Removing old install directory "%s"', install_dir) |
| 64 | check_call(['git', 'rm', '-rf', install_dir]) |
| 65 | |
| 66 | # Need to check again because git won't remove directories if they have |
| 67 | # non-git files in them. |
| 68 | if os.path.exists(install_dir): |
| 69 | shutil.rmtree(install_dir) |
| 70 | |
| 71 | |
Dan Albert | 2e1bb71 | 2021-03-18 16:49:39 -0700 | [diff] [blame] | 72 | def unzip_single_directory(artifact, destination): |
| 73 | # Use cwd so that we can use rename without having to worry about crossing |
| 74 | # file systems. |
| 75 | with TemporaryDirectory(dir=os.getcwd()) as temp_dir: |
| 76 | cmd = [ |
| 77 | 'unzip', |
| 78 | artifact, |
| 79 | '-d', |
| 80 | temp_dir, |
| 81 | '*/sources/android/cpufeatures/*', |
| 82 | '*/sources/android/native_app_glue/*', |
| 83 | '*/sources/android/support/*', |
| 84 | '*/sources/cxx-stl/*', |
| 85 | '*/source.properties', |
| 86 | ] |
| 87 | check_call(cmd) |
| 88 | |
| 89 | dirs = os.listdir(temp_dir) |
| 90 | assert len(dirs) == 1 |
| 91 | ndk_dir = os.path.join(temp_dir, dirs[0]) |
| 92 | for child in os.listdir(ndk_dir): |
| 93 | os.rename(os.path.join(ndk_dir, child), |
| 94 | os.path.join(destination, child)) |
| 95 | |
| 96 | |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 97 | def install_new_release(branch, build, install_dir): |
| 98 | os.makedirs(install_dir) |
| 99 | |
Dan Albert | 2e1bb71 | 2021-03-18 16:49:39 -0700 | [diff] [blame] | 100 | artifact_pattern = 'android-ndk-*.zip' |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 101 | logger().info('Fetching %s from %s (artifacts matching %s)', build, branch, |
| 102 | artifact_pattern) |
| 103 | fetch_artifact(branch, build, artifact_pattern) |
Dan Albert | 2e1bb71 | 2021-03-18 16:49:39 -0700 | [diff] [blame] | 104 | artifacts = glob.glob('android-ndk-*.zip') |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 105 | try: |
| 106 | assert len(artifacts) == 1 |
| 107 | artifact = artifacts[0] |
| 108 | |
| 109 | logger().info('Extracting release') |
Dan Albert | 2e1bb71 | 2021-03-18 16:49:39 -0700 | [diff] [blame] | 110 | unzip_single_directory(artifact, install_dir) |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 111 | finally: |
| 112 | for artifact in artifacts: |
| 113 | os.unlink(artifact) |
| 114 | |
| 115 | |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 116 | def commit(branch, build, install_dir): |
| 117 | logger().info('Making commit') |
| 118 | check_call(['git', 'add', install_dir]) |
| 119 | message = textwrap.dedent("""\ |
| 120 | Update NDK prebuilts to build {build}. |
| 121 | |
Dan Albert | f3fe50d | 2020-07-15 16:46:27 -0700 | [diff] [blame] | 122 | Taken from branch {branch}. |
| 123 | |
| 124 | Bug: None |
| 125 | Test: treehugger |
| 126 | """).format(branch=branch, build=build) |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 127 | check_call(['git', 'commit', '-m', message]) |
| 128 | |
| 129 | |
| 130 | def get_args(): |
| 131 | parser = argparse.ArgumentParser() |
| 132 | parser.add_argument( |
| 133 | '-b', '--branch', default='master-ndk', |
| 134 | help='Branch to pull build from.') |
| 135 | parser.add_argument( |
| 136 | 'major_release', help='Major release being installed, e.g. "r11".') |
| 137 | parser.add_argument('--build', required=True, help='Build number to pull.') |
| 138 | parser.add_argument( |
| 139 | '--use-current-branch', action='store_true', |
| 140 | help='Perform the update in the current branch. Do not repo start.') |
| 141 | parser.add_argument( |
Dan Albert | 7c75aef | 2017-09-18 14:38:38 -0700 | [diff] [blame] | 142 | '-v', '--verbose', action='count', default=0, |
| 143 | help='Increase output verbosity.') |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 144 | return parser.parse_args() |
| 145 | |
| 146 | |
| 147 | def main(): |
| 148 | os.chdir(THIS_DIR) |
| 149 | |
| 150 | args = get_args() |
| 151 | verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG) |
| 152 | verbosity = args.verbose |
| 153 | if verbosity > 2: |
| 154 | verbosity = 2 |
| 155 | logging.basicConfig(level=verbose_map[verbosity]) |
| 156 | |
| 157 | install_dir = os.path.realpath(args.major_release) |
| 158 | |
| 159 | if not args.use_current_branch: |
| 160 | start_branch(args.build) |
| 161 | remove_old_release(install_dir) |
| 162 | install_new_release(args.branch, args.build, install_dir) |
Dan Albert | 74cf8ec | 2016-04-25 16:29:11 -0700 | [diff] [blame] | 163 | commit(args.branch, args.build, install_dir) |
| 164 | |
| 165 | |
| 166 | if __name__ == '__main__': |
| 167 | main() |