blob: fef2b535398d349f07146a70a75741d321b023ee [file] [log] [blame]
Dan Albert74cf8ec2016-04-25 16:29:11 -07001#!/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#
17import argparse
18import glob
19import logging
20import os
21import shutil
22import subprocess
Dan Albert2e1bb712021-03-18 16:49:39 -070023from tempfile import TemporaryDirectory
Dan Albert74cf8ec2016-04-25 16:29:11 -070024import textwrap
25
26
27THIS_DIR = os.path.realpath(os.path.dirname(__file__))
28
29
30def logger():
31 return logging.getLogger(__name__)
32
33
34def check_call(cmd):
35 logger().debug('Running `%s`', ' '.join(cmd))
36 subprocess.check_call(cmd)
37
38
Dan Albert260ec6d2017-12-11 11:41:18 -080039def remove(path):
40 logger().debug('remove `%s`', path)
41 os.remove(path)
42
43
Dan Albert74cf8ec2016-04-25 16:29:11 -070044def 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
51def api_str(api_level):
52 return 'android-{}'.format(api_level)
53
54
55def 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
61def 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 Albert2e1bb712021-03-18 16:49:39 -070072def 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 Albert74cf8ec2016-04-25 16:29:11 -070097def install_new_release(branch, build, install_dir):
98 os.makedirs(install_dir)
99
Dan Albert2e1bb712021-03-18 16:49:39 -0700100 artifact_pattern = 'android-ndk-*.zip'
Dan Albert74cf8ec2016-04-25 16:29:11 -0700101 logger().info('Fetching %s from %s (artifacts matching %s)', build, branch,
102 artifact_pattern)
103 fetch_artifact(branch, build, artifact_pattern)
Dan Albert2e1bb712021-03-18 16:49:39 -0700104 artifacts = glob.glob('android-ndk-*.zip')
Dan Albert74cf8ec2016-04-25 16:29:11 -0700105 try:
106 assert len(artifacts) == 1
107 artifact = artifacts[0]
108
109 logger().info('Extracting release')
Dan Albert2e1bb712021-03-18 16:49:39 -0700110 unzip_single_directory(artifact, install_dir)
Dan Albert74cf8ec2016-04-25 16:29:11 -0700111 finally:
112 for artifact in artifacts:
113 os.unlink(artifact)
114
115
Dan Albert74cf8ec2016-04-25 16:29:11 -0700116def 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 Albertf3fe50d2020-07-15 16:46:27 -0700122 Taken from branch {branch}.
123
124 Bug: None
125 Test: treehugger
126 """).format(branch=branch, build=build)
Dan Albert74cf8ec2016-04-25 16:29:11 -0700127 check_call(['git', 'commit', '-m', message])
128
129
130def 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 Albert7c75aef2017-09-18 14:38:38 -0700142 '-v', '--verbose', action='count', default=0,
143 help='Increase output verbosity.')
Dan Albert74cf8ec2016-04-25 16:29:11 -0700144 return parser.parse_args()
145
146
147def 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 Albert74cf8ec2016-04-25 16:29:11 -0700163 commit(args.branch, args.build, install_dir)
164
165
166if __name__ == '__main__':
167 main()