blob: b48b3e27d7823125f26a09b4d584e1e90913caec [file] [log] [blame]
Alan Viverettef5cb0e62017-11-17 15:15:23 -05001#!/usr/bin/python3
Alan Viverette95f6d362017-04-06 09:40:50 -04002
Alan Viverettecd3de262017-08-14 09:51:30 -04003# This script is used to update platform SDK prebuilts, Support Library, and a variety of other
4# prebuilt libraries used by Android's Makefile builds. For details on how to use this script,
5# visit go/update-prebuilts.
Alan Viverette95f6d362017-04-06 09:40:50 -04006import os, sys, getopt, zipfile, re
Alan Viveretted4527e62017-05-11 15:03:25 -04007import argparse
8import subprocess
Alan Viverettef5cb0e62017-11-17 15:15:23 -05009from shutil import copyfile, rmtree, which
Alan Viveretted4527e62017-05-11 15:03:25 -040010from distutils.version import LooseVersion
Alan Viverettef5cb0e62017-11-17 15:15:23 -050011from functools import reduce
Alan Viverette95f6d362017-04-06 09:40:50 -040012
Alan Viverette45837092017-05-12 14:50:53 -040013current_path = 'current'
14system_path = 'system_current'
Alan Viverette3e57a4a2017-08-11 15:49:47 -040015support_dir = os.path.join(current_path, 'support')
16extras_dir = os.path.join(current_path, 'extras')
Alan Viverettec1c32b62017-12-20 09:40:36 -050017buildtools_dir = 'tools'
Alan Viverette95f6d362017-04-06 09:40:50 -040018
Alan Viverettecd3de262017-08-14 09:51:30 -040019# See go/fetch_artifact for details on this script.
Alan Viveretted4527e62017-05-11 15:03:25 -040020FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
21
Alan Viverette95f6d362017-04-06 09:40:50 -040022# Does not import support-v4, which is handled as a separate Android.mk (../support-v4) to
23# statically include dependencies. Similarly, the support-v13 module is imported as
24# support-v13-nodeps and then handled as a separate Android.mk (../support-v13) to statically
25# include dependencies.
26maven_to_make = {
27 'animated-vector-drawable': ['android-support-animatedvectordrawable', 'graphics/drawable'],
Alan Viveretted8ce7222017-12-11 17:24:43 -050028 'appcompat-v7': ['android-support-v7-appcompat', 'v7/appcompat'],
Alan Viverette95f6d362017-04-06 09:40:50 -040029 'cardview-v7': ['android-support-v7-cardview', 'v7/cardview'],
30 'customtabs': ['android-support-customtabs', 'customtabs'],
31 'design': ['android-support-design', 'design'],
32 'exifinterface': ['android-support-exifinterface', 'exifinterface'],
33 'gridlayout-v7': ['android-support-v7-gridlayout', 'v7/gridlayout'],
34 'leanback-v17': ['android-support-v17-leanback', 'v17/leanback'],
35 'mediarouter-v7': ['android-support-v7-mediarouter', 'v7/mediarouter'],
36 'multidex': ['android-support-multidex', 'multidex/library'],
37 'multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'],
38 'palette-v7': ['android-support-v7-palette', 'v7/palette'],
39 'percent': ['android-support-percent', 'percent'],
40 'preference-leanback-v17': ['android-support-v17-preference-leanback', 'v17/preference-leanback'],
41 'preference-v14': ['android-support-v14-preference', 'v14/preference'],
42 'preference-v7': ['android-support-v7-preference', 'v7/preference'],
43 'recommendation': ['android-support-recommendation', 'recommendation'],
44 'recyclerview-v7': ['android-support-v7-recyclerview', 'v7/recyclerview'],
45 'support-annotations': ['android-support-annotations', 'annotations'],
46 'support-compat': ['android-support-compat', 'compat'],
47 'support-core-ui': ['android-support-core-ui', 'core-ui'],
48 'support-core-utils': ['android-support-core-utils', 'core-utils'],
49 'support-dynamic-animation': ['android-support-dynamic-animation', 'dynamic-animation'],
50 'support-emoji': ['android-support-emoji', 'emoji'],
51 'support-emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
52 'support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
53 'support-fragment': ['android-support-fragment', 'fragment'],
54 'support-media-compat': ['android-support-media-compat', 'media-compat'],
55 'support-tv-provider': ['android-support-tv-provider', 'tv-provider'],
Alan Viveretted8ce7222017-12-11 17:24:43 -050056 'support-v4': ['android-support-v4', 'v4'],
57 'support-v13': ['android-support-v13', 'v13'],
Alan Viverette95f6d362017-04-06 09:40:50 -040058 'support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'],
59 'transition': ['android-support-transition', 'transition'],
Alan Viverette3e57a4a2017-08-11 15:49:47 -040060 'wear': ['android-support-wear', 'wear'],
61 'constraint-layout': ['android-support-constraint-layout', 'constraint-layout'],
Alan Viverettec960cfb2017-12-04 13:09:22 -050062 'constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver'],
63 'android.arch.core:runtime': ['android-arch-core-runtime', 'arch-core/runtime'],
64 'android.arch.core:common': ['android-arch-core-common', 'arch-core/common'],
65 'android.arch.lifecycle:runtime': ['android-arch-lifecycle-runtime', 'arch-lifecycle/runtime'],
66 'android.arch.lifecycle:common': ['android-arch-lifecycle-common', 'arch-lifecycle/common']
Alan Viverette95f6d362017-04-06 09:40:50 -040067}
68
69# Always remove these files.
70blacklist_files = [
71 'annotations.zip',
72 'public.txt',
73 'R.txt',
Alan Viverette44ad4e42017-08-09 17:45:00 -040074 'AndroidManifest.xml',
Alan Viverettef48d9b42017-08-17 14:45:46 -040075 os.path.join('libs','noto-emoji-compat-java.jar')
Alan Viverette95f6d362017-04-06 09:40:50 -040076]
77
Alan Viverette44ad4e42017-08-09 17:45:00 -040078artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
Alan Viverette95f6d362017-04-06 09:40:50 -040079
Alan Viverettec960cfb2017-12-04 13:09:22 -050080
81class MavenLibraryInfo:
82 def __init__(self, key, group_id, artifact_id, version, root, repo_dir, file):
83 self.key = key
84 self.group_id = group_id
85 self.artifact_id = artifact_id
86 self.version = version
87 self.root = root
88 self.repo_dir = repo_dir
89 self.file = file
90
91
Alan Viverettef5cb0e62017-11-17 15:15:23 -050092def print_e(*args, **kwargs):
93 print(*args, file=sys.stderr, **kwargs)
94
Alan Viverette95f6d362017-04-06 09:40:50 -040095
96def touch(fname, times=None):
97 with open(fname, 'a'):
98 os.utime(fname, times)
99
100
Alan Viverette45837092017-05-12 14:50:53 -0400101def path(*path_parts):
102 return reduce((lambda x, y: os.path.join(x, y)), path_parts)
103
104
Alan Viverettecd3de262017-08-14 09:51:30 -0400105def flatten(list):
106 return reduce((lambda x, y: "%s %s" % (x, y)), list)
107
108
Alan Viverette45837092017-05-12 14:50:53 -0400109def rm(path):
110 if os.path.isdir(path):
111 rmtree(path)
112 elif os.path.exists(path):
113 os.remove(path)
114
115
116def mv(src_path, dst_path):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400117 if os.path.exists(dst_path):
Alan Viverettecd3de262017-08-14 09:51:30 -0400118 rm(dst_path)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400119 if not os.path.exists(os.path.dirname(dst_path)):
120 os.makedirs(os.path.dirname(dst_path))
Alan Viverette45837092017-05-12 14:50:53 -0400121 os.rename(src_path, dst_path)
122
123
Dan Willemsen814152e2017-11-06 13:22:11 -0800124def detect_artifacts(repo_dirs):
Alan Viveretted4527e62017-05-11 15:03:25 -0400125 maven_lib_info = {}
126
Dan Willemsen814152e2017-11-06 13:22:11 -0800127 # Find the latest revision for each artifact, remove others
128 for repo_dir in repo_dirs:
129 for root, dirs, files in os.walk(repo_dir):
130 for file in files:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500131 if file[-4:] == ".pom":
132 # Read the POM (hack hack hack).
133 group_id = ''
134 artifact_id = ''
135 version = ''
136 file = os.path.join(root, file)
137 with open(file) as pom_file:
138 for line in pom_file:
139 if line[:11] == ' <groupId>':
140 group_id = line[11:-11]
141 elif line[:14] == ' <artifactId>':
142 artifact_id = line[14:-14]
143 elif line[:11] == ' <version>':
144 version = line[11:-11]
145 if group_id == '' or artifact_id == '' or version == '':
146 print_e('Failed to find Maven artifact data in ' + file)
147 continue
Alan Viverette95f6d362017-04-06 09:40:50 -0400148
Alan Viverettec960cfb2017-12-04 13:09:22 -0500149 # Locate the artifact.
150 artifact_file = file[:-4]
151 if os.path.exists(artifact_file + '.jar'):
152 artifact_file = artifact_file + '.jar'
153 elif os.path.exists(artifact_file + '.aar'):
154 artifact_file = artifact_file + '.aar'
155 else:
156 print_e('Failed to find artifact for ' + artifact_file)
157 continue
158
159 # Make relative to root.
160 artifact_file = artifact_file[len(root) + 1:]
161
162 # Find the mapping.
163 group_artifact = group_id + ':' + artifact_id
164 if artifact_id in maven_to_make:
165 key = artifact_id
166 elif group_artifact in maven_to_make:
167 key = group_artifact
168 else:
169 print_e('Failed to find artifact mapping for ' + group_artifact)
170 continue
171
172 # Store the latest version.
173 version = LooseVersion(version)
174 if key not in maven_lib_info \
175 or version > maven_lib_info[key].version:
176 maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version,
177 root, repo_dir, artifact_file)
Alan Viverette95f6d362017-04-06 09:40:50 -0400178
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400179 return maven_lib_info
180
181
Alan Viveretted8ce7222017-12-11 17:24:43 -0500182def transform_maven_repo(repo_dirs, update_dir, extract_res=True):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400183 cwd = os.getcwd()
184
185 # Use a temporary working directory.
186 working_dir = os.path.join(cwd, 'support_tmp')
Dan Willemsen814152e2017-11-06 13:22:11 -0800187 maven_lib_info = detect_artifacts(repo_dirs)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400188
Alan Viverettec960cfb2017-12-04 13:09:22 -0500189 if not maven_lib_info:
190 print_e('Failed to detect artifacts')
191 return False
192
Alan Viveretted4527e62017-05-11 15:03:25 -0400193 for info in maven_lib_info.values():
Alan Viveretted8ce7222017-12-11 17:24:43 -0500194 transform_maven_lib(working_dir, info, extract_res)
Dan Willemsen814152e2017-11-06 13:22:11 -0800195
196 with open(os.path.join(working_dir, 'Android.mk'), 'w') as f:
Alan Viveretted8ce7222017-12-11 17:24:43 -0500197 args = ["pom2mk", "-transitive", "-sdk-version", "current"]
Dan Willemsen814152e2017-11-06 13:22:11 -0800198 args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name][0] for name in maven_to_make])
199 args.extend(["."])
200 subprocess.check_call(args, stdout=f, cwd=working_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400201
202 # Replace the old directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400203 output_dir = os.path.join(cwd, update_dir)
204 mv(working_dir, output_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500205 return True
Alan Viverette95f6d362017-04-06 09:40:50 -0400206
Alan Viveretted4527e62017-05-11 15:03:25 -0400207
Alan Viveretted8ce7222017-12-11 17:24:43 -0500208def transform_maven_lib(working_dir, artifact_info, extract_res):
Dan Willemsen814152e2017-11-06 13:22:11 -0800209 # Move library into working dir
Alan Viverettec960cfb2017-12-04 13:09:22 -0500210 new_dir = os.path.join(working_dir, os.path.relpath(artifact_info.root, artifact_info.repo_dir))
211 mv(artifact_info.root, new_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800212
213 for dirpath, dirs, files in os.walk(new_dir):
214 for f in files:
215 if '-sources.jar' in f:
216 os.remove(os.path.join(dirpath, f))
217
Alan Viverettec960cfb2017-12-04 13:09:22 -0500218 matcher = artifact_pattern.match(artifact_info.file)
219 maven_lib_name = artifact_info.key
Alan Viveretted4527e62017-05-11 15:03:25 -0400220 maven_lib_vers = matcher.group(2)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500221 maven_lib_type = artifact_info.file[-3:]
Alan Viveretted4527e62017-05-11 15:03:25 -0400222
Alan Viverettec960cfb2017-12-04 13:09:22 -0500223 make_lib_name = maven_to_make[artifact_info.key][0]
224 make_dir_name = maven_to_make[artifact_info.key][1]
Alan Viveretted4527e62017-05-11 15:03:25 -0400225
Dan Willemsen814152e2017-11-06 13:22:11 -0800226 if extract_res:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500227 artifact_file = os.path.join(new_dir, artifact_info.file)
Dan Willemsen814152e2017-11-06 13:22:11 -0800228 target_dir = os.path.join(working_dir, make_dir_name)
229 if not os.path.exists(target_dir):
230 os.makedirs(target_dir)
231
232 if maven_lib_type == "aar":
Alan Viverettec960cfb2017-12-04 13:09:22 -0500233 process_aar(artifact_file, target_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400234
Alan Viveretted8ce7222017-12-11 17:24:43 -0500235 if maven_lib_type == "aar":
Dan Willemsend677b602017-11-08 22:13:17 -0800236 with zipfile.ZipFile(artifact_file) as zip:
Alan Viveretted8ce7222017-12-11 17:24:43 -0500237 manifests_dir = os.path.join(working_dir, "manifests")
238 zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name))
Dan Willemsend677b602017-11-08 22:13:17 -0800239
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500240 print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name)
Alan Viveretted4527e62017-05-11 15:03:25 -0400241
Alan Viverette95f6d362017-04-06 09:40:50 -0400242
Alan Viverettec960cfb2017-12-04 13:09:22 -0500243def process_aar(artifact_file, target_dir):
Alan Viverette95f6d362017-04-06 09:40:50 -0400244 # Extract AAR file to target_dir.
245 with zipfile.ZipFile(artifact_file) as zip:
246 zip.extractall(target_dir)
247
Dan Willemsen814152e2017-11-06 13:22:11 -0800248 # Remove classes.jar
Alan Viverette95f6d362017-04-06 09:40:50 -0400249 classes_jar = os.path.join(target_dir, "classes.jar")
250 if os.path.exists(classes_jar):
Dan Willemsen814152e2017-11-06 13:22:11 -0800251 os.remove(classes_jar)
Alan Viverette95f6d362017-04-06 09:40:50 -0400252
253 # Remove or preserve empty dirs.
254 for root, dirs, files in os.walk(target_dir):
255 for dir in dirs:
256 dir_path = os.path.join(root, dir)
257 if not os.listdir(dir_path):
258 os.rmdir(dir_path)
259
260 # Remove top-level cruft.
261 for file in blacklist_files:
262 file_path = os.path.join(target_dir, file)
263 if os.path.exists(file_path):
264 os.remove(file_path)
265
Alan Viverettef17c9402017-07-19 12:57:40 -0400266
Alan Viverettecd3de262017-08-14 09:51:30 -0400267def fetch_artifact(target, build_id, artifact_path):
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500268 print('Fetching %s from %s...' % (artifact_path, target))
Alan Viverettecd3de262017-08-14 09:51:30 -0400269 fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path]
Alan Viverette45837092017-05-12 14:50:53 -0400270 try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400271 subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
Alan Viverette45837092017-05-12 14:50:53 -0400272 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500273 print_e('FAIL: Unable to retrieve %s artifact for build ID %d' % (artifact_path, build_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500274 print_e('Please make sure you are authenticated for build server access!')
Alan Viverette45837092017-05-12 14:50:53 -0400275 return None
276 return artifact_path
277
278
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400279def fetch_and_extract(target, build_id, file):
280 artifact_path = fetch_artifact(target, build_id, file)
Alan Viverette45837092017-05-12 14:50:53 -0400281 if not artifact_path:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400282 return None
Alan Viverette45837092017-05-12 14:50:53 -0400283
284 # Unzip the repo archive into a separate directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400285 repo_dir = os.path.basename(artifact_path)[:-4]
Alan Viverette45837092017-05-12 14:50:53 -0400286 with zipfile.ZipFile(artifact_path) as zipFile:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400287 zipFile.extractall(repo_dir)
288
289 return repo_dir
290
291
292def update_support(target, build_id):
Alan Viverettecd3de262017-08-14 09:51:30 -0400293 repo_file = 'top-of-tree-m2repository-%s.zip' % build_id
294 repo_dir = fetch_and_extract(target, build_id, repo_file)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400295 if not repo_dir:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500296 print_e('Failed to extract Support Library repository')
Alan Viverettef48d9b42017-08-17 14:45:46 -0400297 return False
Alan Viverette45837092017-05-12 14:50:53 -0400298
299 # Transform the repo archive into a Makefile-compatible format.
Alan Viveretted8ce7222017-12-11 17:24:43 -0500300 return transform_maven_repo([repo_dir], support_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500301
302
303def update_toolkit(target, build_id):
304 repo_dir = fetch_and_extract(target, build_id, 'top-of-tree-m2repository-%s.zip' % build_id)
305 if not repo_dir:
306 print_e('Failed to extract App Toolkit repository')
307 return False
308
309 # Transform the repo archive into a Makefile-compatible format.
310 return transform_maven_repo([repo_dir], os.path.join(extras_dir, 'app-toolkit'))
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400311
312
313def update_constraint(target, build_id):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500314 layout_dir = fetch_and_extract(target, build_id,
315 'com.android.support.constraint-constraint-layout-%s.zip' % build_id)
316 solver_dir = fetch_and_extract(target, build_id,
317 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400318 if not layout_dir or not solver_dir:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500319 print_e('Failed to extract Constraint Layout repositories')
Alan Viverettecd3de262017-08-14 09:51:30 -0400320 return False
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400321
322 # Passing False here is an inelegant solution, but it means we can replace
323 # the top-level directory without worrying about other child directories.
Alan Viverettec960cfb2017-12-04 13:09:22 -0500324 return transform_maven_repo([layout_dir, solver_dir],
325 os.path.join(extras_dir, 'constraint-layout'), extract_res=False)
Alan Viverette45837092017-05-12 14:50:53 -0400326
327
328def extract_to(zip_file, paths, filename, parent_path):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500329 zip_path = next(filter(lambda path: filename in path, paths))
Alan Viverette45837092017-05-12 14:50:53 -0400330 src_path = zip_file.extract(zip_path)
331 dst_path = path(parent_path, filename)
332 mv(src_path, dst_path)
333
334
Alan Viverettecd3de262017-08-14 09:51:30 -0400335def update_sdk_repo(target, build_id):
Alan Viverette45837092017-05-12 14:50:53 -0400336 platform = 'darwin' if 'mac' in target else 'linux'
Alan Viverettecd3de262017-08-14 09:51:30 -0400337 artifact_path = fetch_artifact(
338 target, build_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id))
Alan Viverette45837092017-05-12 14:50:53 -0400339 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400340 return False
Alan Viverette45837092017-05-12 14:50:53 -0400341
342 with zipfile.ZipFile(artifact_path) as zipFile:
343 paths = zipFile.namelist()
344
345 extract_to(zipFile, paths, 'android.jar', current_path)
346 extract_to(zipFile, paths, 'uiautomator.jar', current_path)
347 extract_to(zipFile, paths, 'framework.aidl', current_path)
Paul Duffind891f3c2017-12-06 11:32:56 +0000348 extract_to(zipFile, paths, 'optional/android.test.base.jar', current_path)
Paul Duffin2b1a1462017-07-14 16:21:33 +0100349 extract_to(zipFile, paths, 'optional/android.test.mock.jar', current_path)
350 extract_to(zipFile, paths, 'optional/android.test.runner.jar', current_path)
Alan Viverette45837092017-05-12 14:50:53 -0400351
352 # Unclear if this is actually necessary.
353 extract_to(zipFile, paths, 'framework.aidl', system_path)
Alan Viverettecd3de262017-08-14 09:51:30 -0400354 return True
Alan Viverette45837092017-05-12 14:50:53 -0400355
356
Alan Viverettecd3de262017-08-14 09:51:30 -0400357def update_system(target, build_id):
358 artifact_path = fetch_artifact(target, build_id, 'android_system.jar')
Alan Viverette45837092017-05-12 14:50:53 -0400359 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400360 return False
Alan Viverette45837092017-05-12 14:50:53 -0400361
362 mv(artifact_path, path(system_path, 'android.jar'))
Paul Duffin960e1ee2017-12-21 08:50:14 +0000363
364 artifact_path = fetch_artifact(target, build_id, 'android.test.mock.stubs_system.jar')
365 if not artifact_path:
366 return False
367
368 mv(artifact_path, path(system_path, 'optional/android.test.mock.jar'))
369
Alan Viverettecd3de262017-08-14 09:51:30 -0400370 return True
371
372
Alan Viverettec1c32b62017-12-20 09:40:36 -0500373def update_buildtools(target, arch, build_id):
374 artifact_path = fetch_and_extract(target, build_id,
375 "sdk-repo-%s-build-tools-%s.zip" % (arch, build_id))
376 if not artifact_path:
377 return False
378
379 top_level_dir = os.listdir(artifact_path)[0]
380 src_path = os.path.join(artifact_path, top_level_dir)
381 dst_path = path(buildtools_dir, arch)
382 mv(src_path, dst_path)
383
384 # Move all top-level files to /bin and make them executable
385 bin_path = path(dst_path, 'bin')
386 top_level_files = filter(lambda e: os.path.isfile(path(dst_path, e)), os.listdir(dst_path))
387 for file in top_level_files:
388 src_file = path(dst_path, file)
389 dst_file = path(bin_path, file)
390 mv(src_file, dst_file)
391 os.chmod(dst_file, 0o755)
392
393 # Remove renderscript
394 rm(path(dst_path, 'renderscript'))
395
396 return True
397
398
Alan Viverettecd3de262017-08-14 09:51:30 -0400399def append(text, more_text):
400 if text:
401 return "%s, %s" % (text, more_text)
402 return more_text
Alan Viverette45837092017-05-12 14:50:53 -0400403
Alan Viverette95f6d362017-04-06 09:40:50 -0400404
Alan Viveretted4527e62017-05-11 15:03:25 -0400405parser = argparse.ArgumentParser(
Alan Viverette45837092017-05-12 14:50:53 -0400406 description=('Update current prebuilts'))
Alan Viveretted4527e62017-05-11 15:03:25 -0400407parser.add_argument(
408 'buildId',
409 type=int,
Alan Viveretted4527e62017-05-11 15:03:25 -0400410 help='Build server build ID')
411parser.add_argument(
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400412 '-c', '--constraint', action="store_true",
413 help='If specified, updates only Constraint Layout')
414parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400415 '-s', '--support', action="store_true",
416 help='If specified, updates only the Support Library')
Alan Viverette45837092017-05-12 14:50:53 -0400417parser.add_argument(
Alan Viverettec960cfb2017-12-04 13:09:22 -0500418 '-t', '--toolkit', action="store_true",
419 help='If specified, updates only the App Toolkit')
420parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400421 '-p', '--platform', action="store_true",
422 help='If specified, updates only the Android Platform')
Alan Viverettec1c32b62017-12-20 09:40:36 -0500423parser.add_argument(
424 '-b', '--buildtools', action="store_true",
425 help='If specified, updates only the Build Tools')
Alan Viveretted4527e62017-05-11 15:03:25 -0400426args = parser.parse_args()
427if not args.buildId:
428 parser.error("You must specify a build ID")
429 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500430if not (args.support or args.platform or args.constraint or args.toolkit or args.buildtools):
431 parser.error("You must specify at least one target to update")
Alan Viverettecd3de262017-08-14 09:51:30 -0400432 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500433if (args.support or args.constraint or args.toolkit) and which('pom2mk') is None:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500434 parser.error("Cannot find pom2mk in path; please run lunch to set up build environment")
435 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400436
437try:
438 # Make sure we don't overwrite any pending changes.
439 subprocess.check_call(['git', 'diff', '--quiet', '--', '**'])
440 subprocess.check_call(['git', 'diff', '--quiet', '--cached', '--', '**'])
441except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500442 print_e('FAIL: There are uncommitted changes here; please revert or stash')
Alan Viveretted4527e62017-05-11 15:03:25 -0400443 sys.exit(1)
444
445try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400446 components = None
447 if args.constraint:
448 if update_constraint('studio', args.buildId):
449 components = append(components, 'Constraint Layout')
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500450 print_e('Failed to update Constraint Layout, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400451 else:
452 sys.exit(1)
453 if args.support:
454 if update_support('support_library', args.buildId):
455 components = append(components, 'Support Library')
456 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500457 print_e('Failed to update Support Library, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400458 sys.exit(1)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500459 if args.toolkit:
460 if update_toolkit('support_library_app_toolkit', args.buildId):
461 components = append(components, 'App Toolkit')
462 else:
463 print_e('Failed to update App Toolkit, aborting...')
464 sys.exit(1)
Alan Viverettecd3de262017-08-14 09:51:30 -0400465 if args.platform:
466 if update_sdk_repo('sdk_phone_armv7-sdk_mac', args.buildId) \
467 and update_system('sdk_phone_armv7-sdk_mac', args.buildId):
468 components = append(components, 'platform SDK')
469 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500470 print_e('Failed to update platform SDK, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400471 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500472 if args.buildtools:
473 if update_buildtools('sdk_phone_armv7-sdk_mac', 'darwin', args.buildId) \
474 and update_buildtools('sdk_phone_x86_64-sdk', 'linux', args.buildId) \
475 and update_buildtools('sdk_phone_armv7-win_sdk', 'windows', args.buildId):
476 components = append(components, 'build tools')
477 else:
478 print_e('Failed to update build tools, aborting...')
479 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400480
481 # Commit all changes.
Alan Viverette45837092017-05-12 14:50:53 -0400482 subprocess.check_call(['git', 'add', current_path])
483 subprocess.check_call(['git', 'add', system_path])
Alan Viverettec1c32b62017-12-20 09:40:36 -0500484 subprocess.check_call(['git', 'add', buildtools_dir])
Alan Viverettecd3de262017-08-14 09:51:30 -0400485 msg = "Import %s from build %s\n\n%s" % (components, args.buildId, flatten(sys.argv))
Alan Viveretted4527e62017-05-11 15:03:25 -0400486 subprocess.check_call(['git', 'commit', '-m', msg])
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500487 print('Remember to test this change before uploading it to Gerrit!')
Alan Viveretted4527e62017-05-11 15:03:25 -0400488
489finally:
490 # Revert all stray files, including the downloaded zip.
491 try:
492 with open(os.devnull, 'w') as bitbucket:
493 subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
494 subprocess.check_call(
495 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!'], stdout=bitbucket)
496 subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
497 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500498 print_e('ERROR: Failed cleaning up, manual cleanup required!!!')