blob: bb80208320ad2e44bd9e54f29ca3eed1af146fb2 [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'],
Alan Viverette95f6d362017-04-06 09:40:50 -040031 'exifinterface': ['android-support-exifinterface', 'exifinterface'],
32 'gridlayout-v7': ['android-support-v7-gridlayout', 'v7/gridlayout'],
33 'leanback-v17': ['android-support-v17-leanback', 'v17/leanback'],
34 'mediarouter-v7': ['android-support-v7-mediarouter', 'v7/mediarouter'],
Alan Viverette95f6d362017-04-06 09:40:50 -040035 'palette-v7': ['android-support-v7-palette', 'v7/palette'],
36 'percent': ['android-support-percent', 'percent'],
37 'preference-leanback-v17': ['android-support-v17-preference-leanback', 'v17/preference-leanback'],
38 'preference-v14': ['android-support-v14-preference', 'v14/preference'],
39 'preference-v7': ['android-support-v7-preference', 'v7/preference'],
40 'recommendation': ['android-support-recommendation', 'recommendation'],
41 'recyclerview-v7': ['android-support-v7-recyclerview', 'v7/recyclerview'],
Alan Viverette129555e2018-01-30 09:57:57 -050042 'support-annotations': ['android-support-annotations', 'annotations', 'jar'],
Alan Viverette95f6d362017-04-06 09:40:50 -040043 'support-compat': ['android-support-compat', 'compat'],
44 'support-core-ui': ['android-support-core-ui', 'core-ui'],
45 'support-core-utils': ['android-support-core-utils', 'core-utils'],
46 'support-dynamic-animation': ['android-support-dynamic-animation', 'dynamic-animation'],
47 'support-emoji': ['android-support-emoji', 'emoji'],
48 'support-emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
49 'support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
50 'support-fragment': ['android-support-fragment', 'fragment'],
51 'support-media-compat': ['android-support-media-compat', 'media-compat'],
52 'support-tv-provider': ['android-support-tv-provider', 'tv-provider'],
Alan Viveretted8ce7222017-12-11 17:24:43 -050053 'support-v4': ['android-support-v4', 'v4'],
54 'support-v13': ['android-support-v13', 'v13'],
Alan Viverette95f6d362017-04-06 09:40:50 -040055 'support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'],
56 'transition': ['android-support-transition', 'transition'],
Alan Viverette3e57a4a2017-08-11 15:49:47 -040057 'wear': ['android-support-wear', 'wear'],
Alan Viverette19ecd502018-01-05 13:52:16 -050058 # Slices
59 'slices-core': ['android-slices-core', 'slices-core'],
60 'slices-view': ['android-slices-view', 'slices-view'],
61 'slices-builders': ['android-slices-builders', 'slices-builders'],
62
63 # Multidex
64 'multidex': ['android-support-multidex', 'multidex/library'],
65 'multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'],
66
67 # Constraint Layout
68 'constraint-layout': ['android-support-constraint-layout', 'constraint-layout'],
69 'constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver'],
70
71 # App Arch Core
72 'android.arch.core:runtime': ['android-arch-core-runtime', 'arch-core/runtime'],
73 'android.arch.core:common': ['android-arch-core-common', 'arch-core/common'],
74 # Paging
75 'android.arch.paging:common': ['android-arch-paging-common', 'arch-paging/common'],
76 'android.arch.paging:runtime': ['android-arch-paging-runtime', 'arch-paging/runtime'],
77 # Lifecycle
Ian Lake7afc6372018-01-24 13:11:03 -080078 'android.arch.lifecycle:livedata-core': ['android-arch-lifecycle-livedata-core', 'arch-lifecycle/livedata-core'],
79 'android.arch.lifecycle:livedata': ['android-arch-lifecycle-livedata', 'arch-lifecycle/livedata'],
80 'android.arch.lifecycle:viewmodel': ['android-arch-lifecycle-viewmodel', 'arch-lifecycle/viewmodel'],
81 'android.arch.lifecycle:extensions': ['android-arch-lifecycle-extensions', 'arch-lifecycle/extensions'],
82 'android.arch.lifecycle:runtime': ['android-arch-lifecycle-runtime', 'arch-lifecycle/runtime'],
83 'android.arch.lifecycle:common': ['android-arch-lifecycle-common', 'arch-lifecycle/common'],
84 'android.arch.lifecycle:common-java8': ['android-arch-lifecycle-common-java8', 'arch-lifecycle/common-java8'],
Alan Viverette19ecd502018-01-05 13:52:16 -050085 # Persistence
86 'android.arch.persistence:db': ['android-arch-persistence-db', 'arch-persistence/db'],
87 'android.arch.persistence:db-framework': ['android-arch-persistence-db-framework', 'arch-persistence/db-framework'],
88 # Room
89 'android.arch.persistence.room:common': ['android-arch-room-common', 'arch-room/common'],
90 'android.arch.persistence.room:runtime': ['android-arch-room-runtime', 'arch-room/runtime'],
91 'android.arch.persistence.room:migration': ['android-arch-room-migration', 'arch-room/migration'],
92 'android.arch.persistence.room:testing': ['android-arch-room-testing', 'arch-room/testing'],
Alan Viverette129555e2018-01-30 09:57:57 -050093 # Material Design Components
94 'com.google.android:flexbox': ['flexbox', 'flexbox'],
95 'design': ['android-support-design', 'design'],
96 'design-animation': ['android-support-design-animation', 'design-animation'],
97 'design-bottomnavigation': ['android-support-design-bottomnavigation', 'design-bottomnavigation'],
98 'design-bottomsheet': ['android-support-design-bottomsheet', 'design-bottomsheet'],
99 'design-button': ['android-support-design-button', 'design-button'],
100 'design-canvas': ['android-support-design-canvas', 'design-canvas'],
101 'design-card': ['android-support-design-card', 'design-card'],
102 'design-chip': ['android-support-design-chip', 'design-chip'],
103 'design-circularreveal': ['android-support-design-circularreveal', 'design-circularreveal'],
104 'design-circularreveal-cardview': ['android-support-design-circularreveal-cardview', 'design-circularreveal-cardview'],
105 'design-circularreveal-coordinatorlayout': ['android-support-design-circularreveal-coordinatorlayout', 'design-circularreveal-coordinatorlayout'],
106 'design-color': ['android-support-design-color', 'design-color'],
107 'design-dialog': ['android-support-design-dialog', 'design-dialog'],
108 'design-drawable': ['android-support-design-drawable', 'design-drawable'],
109 'design-expandable': ['android-support-design-expandable', 'design-expandable'],
110 'design-floatingactionbutton': ['android-support-design-floatingactionbutton', 'design-floatingactionbutton'],
111 'design-internal': ['android-support-design-internal', 'design-internal'],
112 'design-math': ['android-support-design-math', 'design-math'],
113 'design-resources': ['android-support-design-resources', 'design-resources'],
114 'design-ripple': ['android-support-design-ripple', 'design-ripple'],
115 'design-snackbar': ['android-support-design-snackbar', 'design-snackbar'],
116 'design-stateful': ['android-support-design-stateful', 'design-stateful'],
117 'design-textfield': ['android-support-design-textfield', 'design-textfield'],
118 'design-theme': ['android-support-design-theme', 'design-theme'],
119 'design-transformation': ['android-support-design-transformation', 'design-transformation'],
120 'design-typography': ['android-support-design-typography', 'design-typography'],
121 'design-widget': ['android-support-design-widget', 'design-widget'],
Alan Viverette95f6d362017-04-06 09:40:50 -0400122}
123
124# Always remove these files.
125blacklist_files = [
126 'annotations.zip',
127 'public.txt',
128 'R.txt',
Alan Viverette44ad4e42017-08-09 17:45:00 -0400129 'AndroidManifest.xml',
Alan Viverettef48d9b42017-08-17 14:45:46 -0400130 os.path.join('libs','noto-emoji-compat-java.jar')
Alan Viverette95f6d362017-04-06 09:40:50 -0400131]
132
Alan Viverette44ad4e42017-08-09 17:45:00 -0400133artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
Alan Viverette95f6d362017-04-06 09:40:50 -0400134
Alan Viverettec960cfb2017-12-04 13:09:22 -0500135
136class MavenLibraryInfo:
137 def __init__(self, key, group_id, artifact_id, version, root, repo_dir, file):
138 self.key = key
139 self.group_id = group_id
140 self.artifact_id = artifact_id
141 self.version = version
142 self.root = root
143 self.repo_dir = repo_dir
144 self.file = file
145
146
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500147def print_e(*args, **kwargs):
148 print(*args, file=sys.stderr, **kwargs)
149
Alan Viverette95f6d362017-04-06 09:40:50 -0400150
151def touch(fname, times=None):
152 with open(fname, 'a'):
153 os.utime(fname, times)
154
155
Alan Viverette45837092017-05-12 14:50:53 -0400156def path(*path_parts):
157 return reduce((lambda x, y: os.path.join(x, y)), path_parts)
158
159
Alan Viverettecd3de262017-08-14 09:51:30 -0400160def flatten(list):
161 return reduce((lambda x, y: "%s %s" % (x, y)), list)
162
163
Alan Viverette45837092017-05-12 14:50:53 -0400164def rm(path):
165 if os.path.isdir(path):
166 rmtree(path)
167 elif os.path.exists(path):
168 os.remove(path)
169
170
171def mv(src_path, dst_path):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400172 if os.path.exists(dst_path):
Alan Viverettecd3de262017-08-14 09:51:30 -0400173 rm(dst_path)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400174 if not os.path.exists(os.path.dirname(dst_path)):
175 os.makedirs(os.path.dirname(dst_path))
Alan Viverette45837092017-05-12 14:50:53 -0400176 os.rename(src_path, dst_path)
177
178
Dan Willemsen814152e2017-11-06 13:22:11 -0800179def detect_artifacts(repo_dirs):
Alan Viveretted4527e62017-05-11 15:03:25 -0400180 maven_lib_info = {}
181
Dan Willemsen814152e2017-11-06 13:22:11 -0800182 # Find the latest revision for each artifact, remove others
183 for repo_dir in repo_dirs:
184 for root, dirs, files in os.walk(repo_dir):
185 for file in files:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500186 if file[-4:] == ".pom":
187 # Read the POM (hack hack hack).
188 group_id = ''
189 artifact_id = ''
190 version = ''
191 file = os.path.join(root, file)
192 with open(file) as pom_file:
193 for line in pom_file:
194 if line[:11] == ' <groupId>':
195 group_id = line[11:-11]
196 elif line[:14] == ' <artifactId>':
197 artifact_id = line[14:-14]
198 elif line[:11] == ' <version>':
199 version = line[11:-11]
200 if group_id == '' or artifact_id == '' or version == '':
201 print_e('Failed to find Maven artifact data in ' + file)
202 continue
Alan Viverette95f6d362017-04-06 09:40:50 -0400203
Alan Viverettec960cfb2017-12-04 13:09:22 -0500204 # Locate the artifact.
205 artifact_file = file[:-4]
206 if os.path.exists(artifact_file + '.jar'):
207 artifact_file = artifact_file + '.jar'
208 elif os.path.exists(artifact_file + '.aar'):
209 artifact_file = artifact_file + '.aar'
210 else:
211 print_e('Failed to find artifact for ' + artifact_file)
212 continue
213
214 # Make relative to root.
215 artifact_file = artifact_file[len(root) + 1:]
216
217 # Find the mapping.
218 group_artifact = group_id + ':' + artifact_id
219 if artifact_id in maven_to_make:
220 key = artifact_id
221 elif group_artifact in maven_to_make:
222 key = group_artifact
223 else:
224 print_e('Failed to find artifact mapping for ' + group_artifact)
225 continue
226
227 # Store the latest version.
228 version = LooseVersion(version)
229 if key not in maven_lib_info \
230 or version > maven_lib_info[key].version:
231 maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version,
232 root, repo_dir, artifact_file)
Alan Viverette95f6d362017-04-06 09:40:50 -0400233
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400234 return maven_lib_info
235
236
Alan Viveretted8ce7222017-12-11 17:24:43 -0500237def transform_maven_repo(repo_dirs, update_dir, extract_res=True):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400238 cwd = os.getcwd()
239
240 # Use a temporary working directory.
241 working_dir = os.path.join(cwd, 'support_tmp')
Dan Willemsen814152e2017-11-06 13:22:11 -0800242 maven_lib_info = detect_artifacts(repo_dirs)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400243
Alan Viverettec960cfb2017-12-04 13:09:22 -0500244 if not maven_lib_info:
245 print_e('Failed to detect artifacts')
246 return False
247
Alan Viveretted4527e62017-05-11 15:03:25 -0400248 for info in maven_lib_info.values():
Alan Viveretted8ce7222017-12-11 17:24:43 -0500249 transform_maven_lib(working_dir, info, extract_res)
Dan Willemsen814152e2017-11-06 13:22:11 -0800250
251 with open(os.path.join(working_dir, 'Android.mk'), 'w') as f:
Alan Viverette129555e2018-01-30 09:57:57 -0500252 args = ["pom2mk", "-static-deps", "-allow-missing", "-sdk-version", "current"]
253 for name in maven_to_make:
254 if len(maven_to_make[name]) == 3:
255 args.extend(["-type=" + maven_to_make[name][0] + "=" + maven_to_make[name][2]])
Dan Willemsen814152e2017-11-06 13:22:11 -0800256 args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name][0] for name in maven_to_make])
257 args.extend(["."])
258 subprocess.check_call(args, stdout=f, cwd=working_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400259
260 # Replace the old directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400261 output_dir = os.path.join(cwd, update_dir)
262 mv(working_dir, output_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500263 return True
Alan Viverette95f6d362017-04-06 09:40:50 -0400264
Alan Viveretted4527e62017-05-11 15:03:25 -0400265
Alan Viveretted8ce7222017-12-11 17:24:43 -0500266def transform_maven_lib(working_dir, artifact_info, extract_res):
Dan Willemsen814152e2017-11-06 13:22:11 -0800267 # Move library into working dir
Alan Viverettec960cfb2017-12-04 13:09:22 -0500268 new_dir = os.path.join(working_dir, os.path.relpath(artifact_info.root, artifact_info.repo_dir))
269 mv(artifact_info.root, new_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800270
271 for dirpath, dirs, files in os.walk(new_dir):
272 for f in files:
273 if '-sources.jar' in f:
274 os.remove(os.path.join(dirpath, f))
275
Alan Viverettec960cfb2017-12-04 13:09:22 -0500276 matcher = artifact_pattern.match(artifact_info.file)
277 maven_lib_name = artifact_info.key
Alan Viveretted4527e62017-05-11 15:03:25 -0400278 maven_lib_vers = matcher.group(2)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500279 maven_lib_type = artifact_info.file[-3:]
Alan Viveretted4527e62017-05-11 15:03:25 -0400280
Alan Viverettec960cfb2017-12-04 13:09:22 -0500281 make_lib_name = maven_to_make[artifact_info.key][0]
282 make_dir_name = maven_to_make[artifact_info.key][1]
Alan Viveretted4527e62017-05-11 15:03:25 -0400283
Alan Viverette129555e2018-01-30 09:57:57 -0500284 artifact_file = os.path.join(new_dir, artifact_info.file)
285
Dan Willemsen814152e2017-11-06 13:22:11 -0800286 if extract_res:
Dan Willemsen814152e2017-11-06 13:22:11 -0800287 target_dir = os.path.join(working_dir, make_dir_name)
288 if not os.path.exists(target_dir):
289 os.makedirs(target_dir)
290
291 if maven_lib_type == "aar":
Alan Viverettec960cfb2017-12-04 13:09:22 -0500292 process_aar(artifact_file, target_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400293
Alan Viveretted8ce7222017-12-11 17:24:43 -0500294 if maven_lib_type == "aar":
Dan Willemsend677b602017-11-08 22:13:17 -0800295 with zipfile.ZipFile(artifact_file) as zip:
Alan Viveretted8ce7222017-12-11 17:24:43 -0500296 manifests_dir = os.path.join(working_dir, "manifests")
297 zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name))
Dan Willemsend677b602017-11-08 22:13:17 -0800298
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500299 print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name)
Alan Viveretted4527e62017-05-11 15:03:25 -0400300
Alan Viverette95f6d362017-04-06 09:40:50 -0400301
Alan Viverettec960cfb2017-12-04 13:09:22 -0500302def process_aar(artifact_file, target_dir):
Alan Viverette95f6d362017-04-06 09:40:50 -0400303 # Extract AAR file to target_dir.
304 with zipfile.ZipFile(artifact_file) as zip:
305 zip.extractall(target_dir)
306
Dan Willemsen814152e2017-11-06 13:22:11 -0800307 # Remove classes.jar
Alan Viverette95f6d362017-04-06 09:40:50 -0400308 classes_jar = os.path.join(target_dir, "classes.jar")
309 if os.path.exists(classes_jar):
Dan Willemsen814152e2017-11-06 13:22:11 -0800310 os.remove(classes_jar)
Alan Viverette95f6d362017-04-06 09:40:50 -0400311
312 # Remove or preserve empty dirs.
313 for root, dirs, files in os.walk(target_dir):
314 for dir in dirs:
315 dir_path = os.path.join(root, dir)
316 if not os.listdir(dir_path):
317 os.rmdir(dir_path)
318
319 # Remove top-level cruft.
320 for file in blacklist_files:
321 file_path = os.path.join(target_dir, file)
322 if os.path.exists(file_path):
323 os.remove(file_path)
324
Alan Viverettef17c9402017-07-19 12:57:40 -0400325
Alan Viverettecd3de262017-08-14 09:51:30 -0400326def fetch_artifact(target, build_id, artifact_path):
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500327 print('Fetching %s from %s...' % (artifact_path, target))
Alan Viverettecd3de262017-08-14 09:51:30 -0400328 fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path]
Alan Viverette45837092017-05-12 14:50:53 -0400329 try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400330 subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
Alan Viverette45837092017-05-12 14:50:53 -0400331 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500332 print_e('FAIL: Unable to retrieve %s artifact for build ID %d' % (artifact_path, build_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500333 print_e('Please make sure you are authenticated for build server access!')
Alan Viverette45837092017-05-12 14:50:53 -0400334 return None
335 return artifact_path
336
337
Alan Viverette129555e2018-01-30 09:57:57 -0500338def extract_artifact(artifact_path):
Alan Viverette45837092017-05-12 14:50:53 -0400339 # Unzip the repo archive into a separate directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400340 repo_dir = os.path.basename(artifact_path)[:-4]
Alan Viverette45837092017-05-12 14:50:53 -0400341 with zipfile.ZipFile(artifact_path) as zipFile:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400342 zipFile.extractall(repo_dir)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400343 return repo_dir
344
345
Alan Viverette129555e2018-01-30 09:57:57 -0500346def fetch_and_extract(target, build_id, file):
347 artifact_path = fetch_artifact(target, build_id, file)
348 if not artifact_path:
349 return None
350 return extract_artifact(artifact_path)
351
352
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400353def update_support(target, build_id):
Alan Viverettecd3de262017-08-14 09:51:30 -0400354 repo_file = 'top-of-tree-m2repository-%s.zip' % build_id
355 repo_dir = fetch_and_extract(target, build_id, repo_file)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400356 if not repo_dir:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500357 print_e('Failed to extract Support Library repository')
Alan Viverettef48d9b42017-08-17 14:45:46 -0400358 return False
Alan Viverette45837092017-05-12 14:50:53 -0400359
360 # Transform the repo archive into a Makefile-compatible format.
Alan Viveretted8ce7222017-12-11 17:24:43 -0500361 return transform_maven_repo([repo_dir], support_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500362
363
364def update_toolkit(target, build_id):
365 repo_dir = fetch_and_extract(target, build_id, 'top-of-tree-m2repository-%s.zip' % build_id)
366 if not repo_dir:
367 print_e('Failed to extract App Toolkit repository')
368 return False
369
370 # Transform the repo archive into a Makefile-compatible format.
371 return transform_maven_repo([repo_dir], os.path.join(extras_dir, 'app-toolkit'))
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400372
373
374def update_constraint(target, build_id):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500375 layout_dir = fetch_and_extract(target, build_id,
376 'com.android.support.constraint-constraint-layout-%s.zip' % build_id)
377 solver_dir = fetch_and_extract(target, build_id,
378 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400379 if not layout_dir or not solver_dir:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500380 print_e('Failed to extract Constraint Layout repositories')
Alan Viverettecd3de262017-08-14 09:51:30 -0400381 return False
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400382
383 # Passing False here is an inelegant solution, but it means we can replace
384 # the top-level directory without worrying about other child directories.
Alan Viverettec960cfb2017-12-04 13:09:22 -0500385 return transform_maven_repo([layout_dir, solver_dir],
386 os.path.join(extras_dir, 'constraint-layout'), extract_res=False)
Alan Viverette45837092017-05-12 14:50:53 -0400387
388
Alan Viverette129555e2018-01-30 09:57:57 -0500389def update_design(file):
390 design_dir = extract_artifact(file)
391 if not design_dir:
392 print_e('Failed to extract Design Library repositories')
393 return False
394
395 # Don't bother extracting resources -- this should only be used with AAPT2.
396 return transform_maven_repo([design_dir],
397 os.path.join(extras_dir, 'material-design'), extract_res=False)
398
399
Alan Viverette45837092017-05-12 14:50:53 -0400400def extract_to(zip_file, paths, filename, parent_path):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500401 zip_path = next(filter(lambda path: filename in path, paths))
Alan Viverette45837092017-05-12 14:50:53 -0400402 src_path = zip_file.extract(zip_path)
403 dst_path = path(parent_path, filename)
404 mv(src_path, dst_path)
405
406
Alan Viverettecd3de262017-08-14 09:51:30 -0400407def update_sdk_repo(target, build_id):
Alan Viverette45837092017-05-12 14:50:53 -0400408 platform = 'darwin' if 'mac' in target else 'linux'
Alan Viverettecd3de262017-08-14 09:51:30 -0400409 artifact_path = fetch_artifact(
410 target, build_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id))
Alan Viverette45837092017-05-12 14:50:53 -0400411 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400412 return False
Alan Viverette45837092017-05-12 14:50:53 -0400413
414 with zipfile.ZipFile(artifact_path) as zipFile:
415 paths = zipFile.namelist()
416
417 extract_to(zipFile, paths, 'android.jar', current_path)
418 extract_to(zipFile, paths, 'uiautomator.jar', current_path)
419 extract_to(zipFile, paths, 'framework.aidl', current_path)
Paul Duffind891f3c2017-12-06 11:32:56 +0000420 extract_to(zipFile, paths, 'optional/android.test.base.jar', current_path)
Paul Duffin2b1a1462017-07-14 16:21:33 +0100421 extract_to(zipFile, paths, 'optional/android.test.mock.jar', current_path)
422 extract_to(zipFile, paths, 'optional/android.test.runner.jar', current_path)
Alan Viverette45837092017-05-12 14:50:53 -0400423
424 # Unclear if this is actually necessary.
425 extract_to(zipFile, paths, 'framework.aidl', system_path)
Alan Viverettecd3de262017-08-14 09:51:30 -0400426 return True
Alan Viverette45837092017-05-12 14:50:53 -0400427
428
Alan Viverettecd3de262017-08-14 09:51:30 -0400429def update_system(target, build_id):
430 artifact_path = fetch_artifact(target, build_id, 'android_system.jar')
Alan Viverette45837092017-05-12 14:50:53 -0400431 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400432 return False
Alan Viverette45837092017-05-12 14:50:53 -0400433
434 mv(artifact_path, path(system_path, 'android.jar'))
Paul Duffin960e1ee2017-12-21 08:50:14 +0000435
436 artifact_path = fetch_artifact(target, build_id, 'android.test.mock.stubs_system.jar')
437 if not artifact_path:
438 return False
439
440 mv(artifact_path, path(system_path, 'optional/android.test.mock.jar'))
441
Alan Viverettecd3de262017-08-14 09:51:30 -0400442 return True
443
444
Alan Viverettec1c32b62017-12-20 09:40:36 -0500445def update_buildtools(target, arch, build_id):
446 artifact_path = fetch_and_extract(target, build_id,
447 "sdk-repo-%s-build-tools-%s.zip" % (arch, build_id))
448 if not artifact_path:
449 return False
450
451 top_level_dir = os.listdir(artifact_path)[0]
452 src_path = os.path.join(artifact_path, top_level_dir)
453 dst_path = path(buildtools_dir, arch)
454 mv(src_path, dst_path)
455
456 # Move all top-level files to /bin and make them executable
457 bin_path = path(dst_path, 'bin')
458 top_level_files = filter(lambda e: os.path.isfile(path(dst_path, e)), os.listdir(dst_path))
459 for file in top_level_files:
460 src_file = path(dst_path, file)
461 dst_file = path(bin_path, file)
462 mv(src_file, dst_file)
463 os.chmod(dst_file, 0o755)
464
465 # Remove renderscript
466 rm(path(dst_path, 'renderscript'))
467
468 return True
469
470
Alan Viverettecd3de262017-08-14 09:51:30 -0400471def append(text, more_text):
472 if text:
473 return "%s, %s" % (text, more_text)
474 return more_text
Alan Viverette45837092017-05-12 14:50:53 -0400475
Alan Viverette95f6d362017-04-06 09:40:50 -0400476
Jeff Gastonb9b09052018-02-05 14:16:05 -0500477def getBuildId(args):
478 source = args.source
479 if source.isnumeric():
480 return int(args.source)
481 else:
482 raise Exception('Updating this set of prebuilts requires <source> to be a numeric build id, not "' + source + '"')
483
Alan Viveretted4527e62017-05-11 15:03:25 -0400484parser = argparse.ArgumentParser(
Alan Viverette45837092017-05-12 14:50:53 -0400485 description=('Update current prebuilts'))
Alan Viveretted4527e62017-05-11 15:03:25 -0400486parser.add_argument(
Alan Viverette129555e2018-01-30 09:57:57 -0500487 'source',
488 help='Build server build ID or local Maven ZIP file')
489parser.add_argument(
490 '-d', '--design', action="store_true",
491 help='If specified, updates only the Design Library')
Alan Viveretted4527e62017-05-11 15:03:25 -0400492parser.add_argument(
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400493 '-c', '--constraint', action="store_true",
494 help='If specified, updates only Constraint Layout')
495parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400496 '-s', '--support', action="store_true",
497 help='If specified, updates only the Support Library')
Alan Viverette45837092017-05-12 14:50:53 -0400498parser.add_argument(
Alan Viverettec960cfb2017-12-04 13:09:22 -0500499 '-t', '--toolkit', action="store_true",
500 help='If specified, updates only the App Toolkit')
501parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400502 '-p', '--platform', action="store_true",
503 help='If specified, updates only the Android Platform')
Alan Viverettec1c32b62017-12-20 09:40:36 -0500504parser.add_argument(
505 '-b', '--buildtools', action="store_true",
506 help='If specified, updates only the Build Tools')
Alan Viveretted4527e62017-05-11 15:03:25 -0400507args = parser.parse_args()
Alan Viverette129555e2018-01-30 09:57:57 -0500508if not args.source:
509 parser.error("You must specify a build ID or local Maven ZIP file")
Alan Viveretted4527e62017-05-11 15:03:25 -0400510 sys.exit(1)
Alan Viverette129555e2018-01-30 09:57:57 -0500511if not (args.support or args.platform or args.constraint or args.toolkit or args.buildtools \
512 or args.design):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500513 parser.error("You must specify at least one target to update")
Alan Viverettecd3de262017-08-14 09:51:30 -0400514 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500515if (args.support or args.constraint or args.toolkit) and which('pom2mk') is None:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500516 parser.error("Cannot find pom2mk in path; please run lunch to set up build environment")
517 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400518
519try:
520 # Make sure we don't overwrite any pending changes.
521 subprocess.check_call(['git', 'diff', '--quiet', '--', '**'])
522 subprocess.check_call(['git', 'diff', '--quiet', '--cached', '--', '**'])
523except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500524 print_e('FAIL: There are uncommitted changes here; please revert or stash')
Alan Viveretted4527e62017-05-11 15:03:25 -0400525 sys.exit(1)
526
527try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400528 components = None
529 if args.constraint:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500530 if update_constraint('studio', getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400531 components = append(components, 'Constraint Layout')
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500532 print_e('Failed to update Constraint Layout, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400533 else:
534 sys.exit(1)
535 if args.support:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500536 if update_support('support_library', getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400537 components = append(components, 'Support Library')
538 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500539 print_e('Failed to update Support Library, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400540 sys.exit(1)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500541 if args.toolkit:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500542 if update_toolkit('support_library_app_toolkit', getBuildId(args)):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500543 components = append(components, 'App Toolkit')
544 else:
545 print_e('Failed to update App Toolkit, aborting...')
546 sys.exit(1)
Alan Viverettecd3de262017-08-14 09:51:30 -0400547 if args.platform:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500548 if update_sdk_repo('sdk_phone_armv7-sdk_mac', getBuildId(args)) \
549 and update_system('sdk_phone_armv7-sdk_mac', getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400550 components = append(components, 'platform SDK')
551 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500552 print_e('Failed to update platform SDK, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400553 sys.exit(1)
Alan Viverette129555e2018-01-30 09:57:57 -0500554 if args.design:
555 if not args.file:
556 print_e('Design Library must have --file specified')
557 sys.exit(1)
558 elif update_design(args.file):
559 components = append(components, 'Design Library')
560 else:
561 print_e('Failed to update platform SDK, aborting...')
562 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500563 if args.buildtools:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500564 if update_buildtools('sdk_phone_armv7-sdk_mac', 'darwin', getBuildId(args)) \
565 and update_buildtools('sdk_phone_x86_64-sdk', 'linux', getBuildId(args)) \
566 and update_buildtools('sdk_phone_armv7-win_sdk', 'windows', getBuildId(args)):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500567 components = append(components, 'build tools')
568 else:
569 print_e('Failed to update build tools, aborting...')
570 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400571
572 # Commit all changes.
Alan Viverette45837092017-05-12 14:50:53 -0400573 subprocess.check_call(['git', 'add', current_path])
574 subprocess.check_call(['git', 'add', system_path])
Alan Viverettec1c32b62017-12-20 09:40:36 -0500575 subprocess.check_call(['git', 'add', buildtools_dir])
Alan Viverette129555e2018-01-30 09:57:57 -0500576 if args.file:
577 src_msg = "local Maven ZIP"
578 else:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500579 src_msg = "build %s" % (getBuildId(args))
Alan Viverette129555e2018-01-30 09:57:57 -0500580 msg = "Import %s from %s\n\n%s" % (components, src_msg, flatten(sys.argv))
Alan Viveretted4527e62017-05-11 15:03:25 -0400581 subprocess.check_call(['git', 'commit', '-m', msg])
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500582 print('Remember to test this change before uploading it to Gerrit!')
Alan Viveretted4527e62017-05-11 15:03:25 -0400583
584finally:
585 # Revert all stray files, including the downloaded zip.
586 try:
587 with open(os.devnull, 'w') as bitbucket:
588 subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
589 subprocess.check_call(
590 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!'], stdout=bitbucket)
591 subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
592 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500593 print_e('ERROR: Failed cleaning up, manual cleanup required!!!')