blob: c5a44794eae00fadbf97cd97d99f647523db8a39 [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 Viverette95f6d362017-04-06 09:40:50 -040017
Alan Viverettecd3de262017-08-14 09:51:30 -040018# See go/fetch_artifact for details on this script.
Alan Viveretted4527e62017-05-11 15:03:25 -040019FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
20
Alan Viverette95f6d362017-04-06 09:40:50 -040021# Does not import support-v4, which is handled as a separate Android.mk (../support-v4) to
22# statically include dependencies. Similarly, the support-v13 module is imported as
23# support-v13-nodeps and then handled as a separate Android.mk (../support-v13) to statically
24# include dependencies.
25maven_to_make = {
26 'animated-vector-drawable': ['android-support-animatedvectordrawable', 'graphics/drawable'],
Alan Viveretted8ce7222017-12-11 17:24:43 -050027 'appcompat-v7': ['android-support-v7-appcompat', 'v7/appcompat'],
Alan Viverette95f6d362017-04-06 09:40:50 -040028 'cardview-v7': ['android-support-v7-cardview', 'v7/cardview'],
29 'customtabs': ['android-support-customtabs', 'customtabs'],
30 'design': ['android-support-design', 'design'],
31 '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'],
35 'multidex': ['android-support-multidex', 'multidex/library'],
36 'multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'],
37 'palette-v7': ['android-support-v7-palette', 'v7/palette'],
38 'percent': ['android-support-percent', 'percent'],
39 'preference-leanback-v17': ['android-support-v17-preference-leanback', 'v17/preference-leanback'],
40 'preference-v14': ['android-support-v14-preference', 'v14/preference'],
41 'preference-v7': ['android-support-v7-preference', 'v7/preference'],
42 'recommendation': ['android-support-recommendation', 'recommendation'],
43 'recyclerview-v7': ['android-support-v7-recyclerview', 'v7/recyclerview'],
44 'support-annotations': ['android-support-annotations', 'annotations'],
45 'support-compat': ['android-support-compat', 'compat'],
46 'support-core-ui': ['android-support-core-ui', 'core-ui'],
47 'support-core-utils': ['android-support-core-utils', 'core-utils'],
48 'support-dynamic-animation': ['android-support-dynamic-animation', 'dynamic-animation'],
49 'support-emoji': ['android-support-emoji', 'emoji'],
50 'support-emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
51 'support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
52 'support-fragment': ['android-support-fragment', 'fragment'],
53 'support-media-compat': ['android-support-media-compat', 'media-compat'],
54 'support-tv-provider': ['android-support-tv-provider', 'tv-provider'],
Alan Viveretted8ce7222017-12-11 17:24:43 -050055 'support-v4': ['android-support-v4', 'v4'],
56 'support-v13': ['android-support-v13', 'v13'],
Alan Viverette95f6d362017-04-06 09:40:50 -040057 'support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'],
58 'transition': ['android-support-transition', 'transition'],
Alan Viverette3e57a4a2017-08-11 15:49:47 -040059 'wear': ['android-support-wear', 'wear'],
60 'constraint-layout': ['android-support-constraint-layout', 'constraint-layout'],
Alan Viverettec960cfb2017-12-04 13:09:22 -050061 'constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver'],
62 'android.arch.core:runtime': ['android-arch-core-runtime', 'arch-core/runtime'],
63 'android.arch.core:common': ['android-arch-core-common', 'arch-core/common'],
64 'android.arch.lifecycle:runtime': ['android-arch-lifecycle-runtime', 'arch-lifecycle/runtime'],
65 'android.arch.lifecycle:common': ['android-arch-lifecycle-common', 'arch-lifecycle/common']
Alan Viverette95f6d362017-04-06 09:40:50 -040066}
67
68# Always remove these files.
69blacklist_files = [
70 'annotations.zip',
71 'public.txt',
72 'R.txt',
Alan Viverette44ad4e42017-08-09 17:45:00 -040073 'AndroidManifest.xml',
Alan Viverettef48d9b42017-08-17 14:45:46 -040074 os.path.join('libs','noto-emoji-compat-java.jar')
Alan Viverette95f6d362017-04-06 09:40:50 -040075]
76
Alan Viverette44ad4e42017-08-09 17:45:00 -040077artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
Alan Viverette95f6d362017-04-06 09:40:50 -040078
Alan Viverettec960cfb2017-12-04 13:09:22 -050079
80class MavenLibraryInfo:
81 def __init__(self, key, group_id, artifact_id, version, root, repo_dir, file):
82 self.key = key
83 self.group_id = group_id
84 self.artifact_id = artifact_id
85 self.version = version
86 self.root = root
87 self.repo_dir = repo_dir
88 self.file = file
89
90
Alan Viverettef5cb0e62017-11-17 15:15:23 -050091def print_e(*args, **kwargs):
92 print(*args, file=sys.stderr, **kwargs)
93
Alan Viverette95f6d362017-04-06 09:40:50 -040094
95def touch(fname, times=None):
96 with open(fname, 'a'):
97 os.utime(fname, times)
98
99
Alan Viverette45837092017-05-12 14:50:53 -0400100def path(*path_parts):
101 return reduce((lambda x, y: os.path.join(x, y)), path_parts)
102
103
Alan Viverettecd3de262017-08-14 09:51:30 -0400104def flatten(list):
105 return reduce((lambda x, y: "%s %s" % (x, y)), list)
106
107
Alan Viverette45837092017-05-12 14:50:53 -0400108def rm(path):
109 if os.path.isdir(path):
110 rmtree(path)
111 elif os.path.exists(path):
112 os.remove(path)
113
114
115def mv(src_path, dst_path):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400116 if os.path.exists(dst_path):
Alan Viverettecd3de262017-08-14 09:51:30 -0400117 rm(dst_path)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400118 if not os.path.exists(os.path.dirname(dst_path)):
119 os.makedirs(os.path.dirname(dst_path))
Alan Viverette45837092017-05-12 14:50:53 -0400120 os.rename(src_path, dst_path)
121
122
Dan Willemsen814152e2017-11-06 13:22:11 -0800123def detect_artifacts(repo_dirs):
Alan Viveretted4527e62017-05-11 15:03:25 -0400124 maven_lib_info = {}
125
Dan Willemsen814152e2017-11-06 13:22:11 -0800126 # Find the latest revision for each artifact, remove others
127 for repo_dir in repo_dirs:
128 for root, dirs, files in os.walk(repo_dir):
129 for file in files:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500130 if file[-4:] == ".pom":
131 # Read the POM (hack hack hack).
132 group_id = ''
133 artifact_id = ''
134 version = ''
135 file = os.path.join(root, file)
136 with open(file) as pom_file:
137 for line in pom_file:
138 if line[:11] == ' <groupId>':
139 group_id = line[11:-11]
140 elif line[:14] == ' <artifactId>':
141 artifact_id = line[14:-14]
142 elif line[:11] == ' <version>':
143 version = line[11:-11]
144 if group_id == '' or artifact_id == '' or version == '':
145 print_e('Failed to find Maven artifact data in ' + file)
146 continue
Alan Viverette95f6d362017-04-06 09:40:50 -0400147
Alan Viverettec960cfb2017-12-04 13:09:22 -0500148 # Locate the artifact.
149 artifact_file = file[:-4]
150 if os.path.exists(artifact_file + '.jar'):
151 artifact_file = artifact_file + '.jar'
152 elif os.path.exists(artifact_file + '.aar'):
153 artifact_file = artifact_file + '.aar'
154 else:
155 print_e('Failed to find artifact for ' + artifact_file)
156 continue
157
158 # Make relative to root.
159 artifact_file = artifact_file[len(root) + 1:]
160
161 # Find the mapping.
162 group_artifact = group_id + ':' + artifact_id
163 if artifact_id in maven_to_make:
164 key = artifact_id
165 elif group_artifact in maven_to_make:
166 key = group_artifact
167 else:
168 print_e('Failed to find artifact mapping for ' + group_artifact)
169 continue
170
171 # Store the latest version.
172 version = LooseVersion(version)
173 if key not in maven_lib_info \
174 or version > maven_lib_info[key].version:
175 maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version,
176 root, repo_dir, artifact_file)
Alan Viverette95f6d362017-04-06 09:40:50 -0400177
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400178 return maven_lib_info
179
180
Alan Viveretted8ce7222017-12-11 17:24:43 -0500181def transform_maven_repo(repo_dirs, update_dir, extract_res=True):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400182 cwd = os.getcwd()
183
184 # Use a temporary working directory.
185 working_dir = os.path.join(cwd, 'support_tmp')
Dan Willemsen814152e2017-11-06 13:22:11 -0800186 maven_lib_info = detect_artifacts(repo_dirs)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400187
Alan Viverettec960cfb2017-12-04 13:09:22 -0500188 if not maven_lib_info:
189 print_e('Failed to detect artifacts')
190 return False
191
Alan Viveretted4527e62017-05-11 15:03:25 -0400192 for info in maven_lib_info.values():
Alan Viveretted8ce7222017-12-11 17:24:43 -0500193 transform_maven_lib(working_dir, info, extract_res)
Dan Willemsen814152e2017-11-06 13:22:11 -0800194
195 with open(os.path.join(working_dir, 'Android.mk'), 'w') as f:
Alan Viveretted8ce7222017-12-11 17:24:43 -0500196 args = ["pom2mk", "-transitive", "-sdk-version", "current"]
Dan Willemsen814152e2017-11-06 13:22:11 -0800197 args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name][0] for name in maven_to_make])
198 args.extend(["."])
199 subprocess.check_call(args, stdout=f, cwd=working_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400200
201 # Replace the old directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400202 output_dir = os.path.join(cwd, update_dir)
203 mv(working_dir, output_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500204 return True
Alan Viverette95f6d362017-04-06 09:40:50 -0400205
Alan Viveretted4527e62017-05-11 15:03:25 -0400206
Alan Viveretted8ce7222017-12-11 17:24:43 -0500207def transform_maven_lib(working_dir, artifact_info, extract_res):
Dan Willemsen814152e2017-11-06 13:22:11 -0800208 # Move library into working dir
Alan Viverettec960cfb2017-12-04 13:09:22 -0500209 new_dir = os.path.join(working_dir, os.path.relpath(artifact_info.root, artifact_info.repo_dir))
210 mv(artifact_info.root, new_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800211
212 for dirpath, dirs, files in os.walk(new_dir):
213 for f in files:
214 if '-sources.jar' in f:
215 os.remove(os.path.join(dirpath, f))
216
Alan Viverettec960cfb2017-12-04 13:09:22 -0500217 matcher = artifact_pattern.match(artifact_info.file)
218 maven_lib_name = artifact_info.key
Alan Viveretted4527e62017-05-11 15:03:25 -0400219 maven_lib_vers = matcher.group(2)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500220 maven_lib_type = artifact_info.file[-3:]
Alan Viveretted4527e62017-05-11 15:03:25 -0400221
Alan Viverettec960cfb2017-12-04 13:09:22 -0500222 make_lib_name = maven_to_make[artifact_info.key][0]
223 make_dir_name = maven_to_make[artifact_info.key][1]
Alan Viveretted4527e62017-05-11 15:03:25 -0400224
Dan Willemsen814152e2017-11-06 13:22:11 -0800225 if extract_res:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500226 artifact_file = os.path.join(new_dir, artifact_info.file)
Dan Willemsen814152e2017-11-06 13:22:11 -0800227 target_dir = os.path.join(working_dir, make_dir_name)
228 if not os.path.exists(target_dir):
229 os.makedirs(target_dir)
230
231 if maven_lib_type == "aar":
Alan Viverettec960cfb2017-12-04 13:09:22 -0500232 process_aar(artifact_file, target_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400233
Alan Viveretted8ce7222017-12-11 17:24:43 -0500234 if maven_lib_type == "aar":
Dan Willemsend677b602017-11-08 22:13:17 -0800235 with zipfile.ZipFile(artifact_file) as zip:
Alan Viveretted8ce7222017-12-11 17:24:43 -0500236 manifests_dir = os.path.join(working_dir, "manifests")
237 zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name))
Dan Willemsend677b602017-11-08 22:13:17 -0800238
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500239 print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name)
Alan Viveretted4527e62017-05-11 15:03:25 -0400240
Alan Viverette95f6d362017-04-06 09:40:50 -0400241
Alan Viverettec960cfb2017-12-04 13:09:22 -0500242def process_aar(artifact_file, target_dir):
Alan Viverette95f6d362017-04-06 09:40:50 -0400243 # Extract AAR file to target_dir.
244 with zipfile.ZipFile(artifact_file) as zip:
245 zip.extractall(target_dir)
246
Dan Willemsen814152e2017-11-06 13:22:11 -0800247 # Remove classes.jar
Alan Viverette95f6d362017-04-06 09:40:50 -0400248 classes_jar = os.path.join(target_dir, "classes.jar")
249 if os.path.exists(classes_jar):
Dan Willemsen814152e2017-11-06 13:22:11 -0800250 os.remove(classes_jar)
Alan Viverette95f6d362017-04-06 09:40:50 -0400251
252 # Remove or preserve empty dirs.
253 for root, dirs, files in os.walk(target_dir):
254 for dir in dirs:
255 dir_path = os.path.join(root, dir)
256 if not os.listdir(dir_path):
257 os.rmdir(dir_path)
258
259 # Remove top-level cruft.
260 for file in blacklist_files:
261 file_path = os.path.join(target_dir, file)
262 if os.path.exists(file_path):
263 os.remove(file_path)
264
Alan Viverettef17c9402017-07-19 12:57:40 -0400265
Alan Viverettecd3de262017-08-14 09:51:30 -0400266def fetch_artifact(target, build_id, artifact_path):
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500267 print('Fetching %s from %s...' % (artifact_path, target))
Alan Viverettecd3de262017-08-14 09:51:30 -0400268 fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path]
Alan Viverette45837092017-05-12 14:50:53 -0400269 try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400270 subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
Alan Viverette45837092017-05-12 14:50:53 -0400271 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500272 print_e('FAIL: Unable to retrieve %s artifact for build ID %d' % (artifact_path, build_id))
Alan Viverette45837092017-05-12 14:50:53 -0400273 return None
274 return artifact_path
275
276
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400277def fetch_and_extract(target, build_id, file):
278 artifact_path = fetch_artifact(target, build_id, file)
Alan Viverette45837092017-05-12 14:50:53 -0400279 if not artifact_path:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400280 return None
Alan Viverette45837092017-05-12 14:50:53 -0400281
282 # Unzip the repo archive into a separate directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400283 repo_dir = os.path.basename(artifact_path)[:-4]
Alan Viverette45837092017-05-12 14:50:53 -0400284 with zipfile.ZipFile(artifact_path) as zipFile:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400285 zipFile.extractall(repo_dir)
286
287 return repo_dir
288
289
290def update_support(target, build_id):
Alan Viverettecd3de262017-08-14 09:51:30 -0400291 repo_file = 'top-of-tree-m2repository-%s.zip' % build_id
292 repo_dir = fetch_and_extract(target, build_id, repo_file)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400293 if not repo_dir:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500294 print_e('Failed to extract Support Library repository')
Alan Viverettef48d9b42017-08-17 14:45:46 -0400295 return False
Alan Viverette45837092017-05-12 14:50:53 -0400296
297 # Transform the repo archive into a Makefile-compatible format.
Alan Viveretted8ce7222017-12-11 17:24:43 -0500298 return transform_maven_repo([repo_dir], support_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500299
300
301def update_toolkit(target, build_id):
302 repo_dir = fetch_and_extract(target, build_id, 'top-of-tree-m2repository-%s.zip' % build_id)
303 if not repo_dir:
304 print_e('Failed to extract App Toolkit repository')
305 return False
306
307 # Transform the repo archive into a Makefile-compatible format.
308 return transform_maven_repo([repo_dir], os.path.join(extras_dir, 'app-toolkit'))
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400309
310
311def update_constraint(target, build_id):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500312 layout_dir = fetch_and_extract(target, build_id,
313 'com.android.support.constraint-constraint-layout-%s.zip' % build_id)
314 solver_dir = fetch_and_extract(target, build_id,
315 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400316 if not layout_dir or not solver_dir:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500317 print_e('Failed to extract Constraint Layout repositories')
Alan Viverettecd3de262017-08-14 09:51:30 -0400318 return False
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400319
320 # Passing False here is an inelegant solution, but it means we can replace
321 # the top-level directory without worrying about other child directories.
Alan Viverettec960cfb2017-12-04 13:09:22 -0500322 return transform_maven_repo([layout_dir, solver_dir],
323 os.path.join(extras_dir, 'constraint-layout'), extract_res=False)
Alan Viverette45837092017-05-12 14:50:53 -0400324
325
326def extract_to(zip_file, paths, filename, parent_path):
327 zip_path = filter(lambda path: filename in path, paths)[0]
328 src_path = zip_file.extract(zip_path)
329 dst_path = path(parent_path, filename)
330 mv(src_path, dst_path)
331
332
Alan Viverettecd3de262017-08-14 09:51:30 -0400333def update_sdk_repo(target, build_id):
Alan Viverette45837092017-05-12 14:50:53 -0400334 platform = 'darwin' if 'mac' in target else 'linux'
Alan Viverettecd3de262017-08-14 09:51:30 -0400335 artifact_path = fetch_artifact(
336 target, build_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id))
Alan Viverette45837092017-05-12 14:50:53 -0400337 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400338 return False
Alan Viverette45837092017-05-12 14:50:53 -0400339
340 with zipfile.ZipFile(artifact_path) as zipFile:
341 paths = zipFile.namelist()
342
343 extract_to(zipFile, paths, 'android.jar', current_path)
344 extract_to(zipFile, paths, 'uiautomator.jar', current_path)
345 extract_to(zipFile, paths, 'framework.aidl', current_path)
Paul Duffind891f3c2017-12-06 11:32:56 +0000346 extract_to(zipFile, paths, 'optional/android.test.base.jar', current_path)
Paul Duffin2b1a1462017-07-14 16:21:33 +0100347 extract_to(zipFile, paths, 'optional/android.test.mock.jar', current_path)
348 extract_to(zipFile, paths, 'optional/android.test.runner.jar', current_path)
Alan Viverette45837092017-05-12 14:50:53 -0400349
350 # Unclear if this is actually necessary.
351 extract_to(zipFile, paths, 'framework.aidl', system_path)
Alan Viverettecd3de262017-08-14 09:51:30 -0400352 return True
Alan Viverette45837092017-05-12 14:50:53 -0400353
354
Alan Viverettecd3de262017-08-14 09:51:30 -0400355def update_system(target, build_id):
356 artifact_path = fetch_artifact(target, build_id, 'android_system.jar')
Alan Viverette45837092017-05-12 14:50:53 -0400357 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400358 return False
Alan Viverette45837092017-05-12 14:50:53 -0400359
360 mv(artifact_path, path(system_path, 'android.jar'))
Alan Viverettecd3de262017-08-14 09:51:30 -0400361 return True
362
363
364def append(text, more_text):
365 if text:
366 return "%s, %s" % (text, more_text)
367 return more_text
Alan Viverette45837092017-05-12 14:50:53 -0400368
Alan Viverette95f6d362017-04-06 09:40:50 -0400369
Alan Viveretted4527e62017-05-11 15:03:25 -0400370parser = argparse.ArgumentParser(
Alan Viverette45837092017-05-12 14:50:53 -0400371 description=('Update current prebuilts'))
Alan Viveretted4527e62017-05-11 15:03:25 -0400372parser.add_argument(
373 'buildId',
374 type=int,
Alan Viveretted4527e62017-05-11 15:03:25 -0400375 help='Build server build ID')
376parser.add_argument(
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400377 '-c', '--constraint', action="store_true",
378 help='If specified, updates only Constraint Layout')
379parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400380 '-s', '--support', action="store_true",
381 help='If specified, updates only the Support Library')
Alan Viverette45837092017-05-12 14:50:53 -0400382parser.add_argument(
Alan Viverettec960cfb2017-12-04 13:09:22 -0500383 '-t', '--toolkit', action="store_true",
384 help='If specified, updates only the App Toolkit')
385parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400386 '-p', '--platform', action="store_true",
387 help='If specified, updates only the Android Platform')
Alan Viveretted4527e62017-05-11 15:03:25 -0400388args = parser.parse_args()
389if not args.buildId:
390 parser.error("You must specify a build ID")
391 sys.exit(1)
Alan Viverettecd3de262017-08-14 09:51:30 -0400392if not (args.support or args.platform or args.constraint):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500393 parser.error("You must specify at least one of --constraint, --support, --toolkit, or --platform")
Alan Viverettecd3de262017-08-14 09:51:30 -0400394 sys.exit(1)
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500395if which('pom2mk') is None:
396 parser.error("Cannot find pom2mk in path; please run lunch to set up build environment")
397 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400398
399try:
400 # Make sure we don't overwrite any pending changes.
401 subprocess.check_call(['git', 'diff', '--quiet', '--', '**'])
402 subprocess.check_call(['git', 'diff', '--quiet', '--cached', '--', '**'])
403except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500404 print_e('FAIL: There are uncommitted changes here; please revert or stash')
Alan Viveretted4527e62017-05-11 15:03:25 -0400405 sys.exit(1)
406
407try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400408 components = None
409 if args.constraint:
410 if update_constraint('studio', args.buildId):
411 components = append(components, 'Constraint Layout')
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500412 print_e('Failed to update Constraint Layout, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400413 else:
414 sys.exit(1)
415 if args.support:
416 if update_support('support_library', args.buildId):
417 components = append(components, 'Support Library')
418 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500419 print_e('Failed to update Support Library, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400420 sys.exit(1)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500421 if args.toolkit:
422 if update_toolkit('support_library_app_toolkit', args.buildId):
423 components = append(components, 'App Toolkit')
424 else:
425 print_e('Failed to update App Toolkit, aborting...')
426 sys.exit(1)
Alan Viverettecd3de262017-08-14 09:51:30 -0400427 if args.platform:
428 if update_sdk_repo('sdk_phone_armv7-sdk_mac', args.buildId) \
429 and update_system('sdk_phone_armv7-sdk_mac', args.buildId):
430 components = append(components, 'platform SDK')
431 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500432 print_e('Failed to update platform SDK, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400433 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400434
435 # Commit all changes.
Alan Viverette45837092017-05-12 14:50:53 -0400436 subprocess.check_call(['git', 'add', current_path])
437 subprocess.check_call(['git', 'add', system_path])
Alan Viverettecd3de262017-08-14 09:51:30 -0400438 msg = "Import %s from build %s\n\n%s" % (components, args.buildId, flatten(sys.argv))
Alan Viveretted4527e62017-05-11 15:03:25 -0400439 subprocess.check_call(['git', 'commit', '-m', msg])
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500440 print('Remember to test this change before uploading it to Gerrit!')
Alan Viveretted4527e62017-05-11 15:03:25 -0400441
442finally:
443 # Revert all stray files, including the downloaded zip.
444 try:
445 with open(os.devnull, 'w') as bitbucket:
446 subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
447 subprocess.check_call(
448 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!'], stdout=bitbucket)
449 subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
450 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500451 print_e('ERROR: Failed cleaning up, manual cleanup required!!!')