blob: fe0ae6ca97c1ef33e5f79c5c0a2c4e01d04d403f [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'
Jeff Gaston13e38412018-02-06 14:45:36 -050018jetifier_dir = os.path.join(buildtools_dir, 'jetifier')
Alan Viverette95f6d362017-04-06 09:40:50 -040019
Jeff Gastona68a8d42018-03-23 14:00:13 -040020temp_dir = os.path.join(os.getcwd(), "support_tmp")
21os.chdir(os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))))
22git_dir = os.getcwd()
23
Jeff Gastonf12f3ce2018-03-23 16:41:24 -040024rerun_extract_deps = False
25
Alan Viverettecd3de262017-08-14 09:51:30 -040026# See go/fetch_artifact for details on this script.
Alan Viveretted4527e62017-05-11 15:03:25 -040027FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
28
Alan Viverette95f6d362017-04-06 09:40:50 -040029maven_to_make = {
Alan Viverette7e897e22018-03-09 15:24:10 -050030 'animated-vector-drawable': ['android-support-animatedvectordrawable', 'graphics/drawable'],
31 'appcompat-v7': ['android-support-v7-appcompat', 'v7/appcompat'],
32 'cardview-v7': ['android-support-v7-cardview', 'v7/cardview'],
33 'collections': ['android-support-collections', 'collections'],
34 'customtabs': ['android-support-customtabs', 'customtabs'],
35 'exifinterface': ['android-support-exifinterface', 'exifinterface'],
36 'gridlayout-v7': ['android-support-v7-gridlayout', 'v7/gridlayout'],
37 'leanback-v17': ['android-support-v17-leanback', 'v17/leanback'],
38 'mediarouter-v7': ['android-support-v7-mediarouter', 'v7/mediarouter'],
39 'palette-v7': ['android-support-v7-palette', 'v7/palette'],
40 'percent': ['android-support-percent', 'percent'],
41 'preference-leanback-v17': ['android-support-v17-preference-leanback', 'v17/preference-leanback'],
42 'preference-v14': ['android-support-v14-preference', 'v14/preference'],
43 'preference-v7': ['android-support-v7-preference', 'v7/preference'],
44 'recommendation': ['android-support-recommendation', 'recommendation'],
45 'recyclerview-v7': ['android-support-v7-recyclerview', 'v7/recyclerview'],
46 'support-annotations': ['android-support-annotations', 'annotations', 'jar'],
47 'support-compat': ['android-support-compat', 'compat'],
48 'support-core-ui': ['android-support-core-ui', 'core-ui'],
49 'support-core-utils': ['android-support-core-utils', 'core-utils'],
50 'support-dynamic-animation': ['android-support-dynamic-animation', 'dynamic-animation'],
51 'support-emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
52 'support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
53 'support-emoji': ['android-support-emoji', 'emoji'],
54 'support-fragment': ['android-support-fragment', 'fragment'],
55 'support-media-compat': ['android-support-media-compat', 'media-compat'],
56 'support-tv-provider': ['android-support-tv-provider', 'tv-provider'],
57 'support-v13': ['android-support-v13', 'v13'],
58 'support-v4': ['android-support-v4', 'v4'],
59 'support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'],
60 'transition': ['android-support-transition', 'transition'],
61 'wear': ['android-support-wear', 'wear'],
Alan Viverette7bb86da2018-02-21 18:29:02 -050062
Alan Viverette7e897e22018-03-09 15:24:10 -050063 # AndroidX renamed modules
64 'androidx.annotation:annotation': ['android-support-annotations', 'annotations', 'jar'],
65 'androidx.appcompat:appcompat': ['android-support-v7-appcompat', 'v7/appcompat'],
66 'androidx.browser:browser': ['android-support-customtabs', 'customtabs'],
67 'androidx.cardview:cardview': ['android-support-v7-cardview', 'v7/cardview'],
68 'androidx.collection:collection': ['android-support-collections', 'collections'],
69 'androidx.core:core': ['android-support-compat', 'compat'],
70 'androidx.dynamicanimation:dynamicanimation': ['android-support-dynamic-animation', 'dynamic-animation'],
71 'androidx.emoji:emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
72 'androidx.emoji:emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
73 'androidx.emoji:emoji': ['android-support-emoji', 'emoji'],
74 'androidx.fragment:fragment': ['android-support-fragment', 'fragment'],
75 'androidx.gridlayout:gridlayout': ['android-support-v7-gridlayout', 'v7/gridlayout'],
76 'androidx.leanback:leanback-preference': ['android-support-v17-preference-leanback', 'v17/preference-leanback'],
77 'androidx.leanback:leanback': ['android-support-v17-leanback', 'v17/leanback'],
78 'androidx.legacy:legacy-preference-v14': ['android-support-v14-preference', 'v14/preference'],
79 'androidx.legacy:legacy-support-core-ui': ['android-support-core-ui', 'core-ui'],
80 'androidx.legacy:legacy-support-core-utils': ['android-support-core-utils', 'core-utils'],
81 'androidx.legacy:legacy-support-v13': ['android-support-v13', 'v13'],
82 'androidx.legacy:legacy-support-v4': ['android-support-v4', 'v4'],
83 'androidx.media:media': ['android-support-media-compat', 'media-compat'],
84 'androidx.mediarouter:mediarouter': ['android-support-v7-mediarouter', 'v7/mediarouter'],
85 'androidx.palette:palette': ['android-support-v7-palette', 'v7/palette'],
86 'androidx.percentlayout:percentlayout': ['android-support-percent', 'percent'],
87 'androidx.preference:preference': ['android-support-v7-preference', 'v7/preference'],
88 'androidx.recyclerview:recyclerview': ['android-support-v7-recyclerview', 'v7/recyclerview'],
89 'androidx.tvprovider:tvprovider': ['android-support-tv-provider', 'tv-provider'],
90 'androidx.vectordrawable:vectordrawable-animated': ['android-support-animatedvectordrawable', 'graphics/drawable'],
91 'androidx.vectordrawable:vectordrawable': ['android-support-vectordrawable', 'graphics/drawable'],
92
93 # AndroidX new modules
94 'heifwriter': ['android-support-heifwriter', 'heifwriter'],
95 'webkit': ['android-support-webkit', 'webkit'],
96 'customview': ['android-support-customview', 'customview'],
97 'textclassifier': ['android-support-textclassifier', 'textclassifier'],
98 'swiperefreshlayout': ['android-support-swiperefreshlayout', 'swiperefreshlayout'],
99 'viewpager': ['android-support-viewpager', 'viewpager'],
100 'coordinatorlayout': ['android-support-coordinatorlayout', 'coordinatorlayout'],
101 'asynclayoutinflater': ['android-support-asynclayoutinflater', 'asynclayoutinflater'],
102 'support-content': ['android-support-support-content', 'support-content'],
103 'documentfile': ['android-support-documentfile', 'documentfile'],
104 'drawerlayout': ['android-support-drawerlayout', 'drawerlayout'],
105 'localbroadcastmanager': ['android-support-localbroadcastmanager', 'localbroadcastmanager'],
106 'print': ['android-support-print', 'print'],
107 'slidingpanelayout': ['android-support-slidingpanelayout', 'slidingpanelayout'],
108 'interpolator': ['android-support-interpolator', 'interpolator'],
109 'cursoradapter': ['android-support-cursoradapter', 'cursoradapter'],
110 'loader': ['android-support-loader', 'loader'],
111 'contentpaging': ['android-support-contentpaging', 'contentpaging'],
112 'recyclerview-selection': ['android-support-recyclerview-selection', 'recyclerview-selection'],
Alan Viverette4ec9a172018-02-20 16:19:31 -0500113 'car': ['android-support-car', 'car'],
Alan Viverette7bb86da2018-02-21 18:29:02 -0500114
Alan Viverette19ecd502018-01-05 13:52:16 -0500115 # Slices
116 'slices-core': ['android-slices-core', 'slices-core'],
117 'slices-view': ['android-slices-view', 'slices-view'],
118 'slices-builders': ['android-slices-builders', 'slices-builders'],
119
120 # Multidex
121 'multidex': ['android-support-multidex', 'multidex/library'],
122 'multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'],
123
124 # Constraint Layout
125 'constraint-layout': ['android-support-constraint-layout', 'constraint-layout'],
126 'constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver'],
127
128 # App Arch Core
129 'android.arch.core:runtime': ['android-arch-core-runtime', 'arch-core/runtime'],
130 'android.arch.core:common': ['android-arch-core-common', 'arch-core/common'],
Allenab72f012018-01-29 18:10:23 -0800131 'android.arch.core:testing': ['android-arch-core-testing', 'arch-core/testing'],
132
Alan Viverette19ecd502018-01-05 13:52:16 -0500133 # Paging
134 'android.arch.paging:common': ['android-arch-paging-common', 'arch-paging/common'],
135 'android.arch.paging:runtime': ['android-arch-paging-runtime', 'arch-paging/runtime'],
Allenab72f012018-01-29 18:10:23 -0800136
Alan Viverette19ecd502018-01-05 13:52:16 -0500137 # Lifecycle
Allenab72f012018-01-29 18:10:23 -0800138 'android.arch.lifecycle:livedata-core': ['android-arch-lifecycle-livedata-core', 'arch-lifecycle/livedata-core'],
139 'android.arch.lifecycle:livedata': ['android-arch-lifecycle-livedata', 'arch-lifecycle/livedata'],
140 'android.arch.lifecycle:viewmodel': ['android-arch-lifecycle-viewmodel', 'arch-lifecycle/viewmodel'],
141 'android.arch.lifecycle:extensions': ['android-arch-lifecycle-extensions', 'arch-lifecycle/extensions'],
142 'android.arch.lifecycle:runtime': ['android-arch-lifecycle-runtime', 'arch-lifecycle/runtime'],
143 'android.arch.lifecycle:common': ['android-arch-lifecycle-common', 'arch-lifecycle/common'],
144 'android.arch.lifecycle:common-java8': ['android-arch-lifecycle-common-java8', 'arch-lifecycle/common-java8'],
Alan Viverettec7e88f12018-02-13 15:29:50 -0500145 # Missing dependencies:
146 # - auto-common
147 # - javapoet
148 #'android.arch.lifecycle:compiler': ['android-arch-lifecycle-compiler', 'arch-lifecycle/compiler'],
149 # Missing dependencies:
150 # - reactive-streams
151 #'android.arch.lifecycle:reactivestreams': ['android-arch-lifecycle-reactivestreams','arch-lifecycle/reactivestreams'],
Allenab72f012018-01-29 18:10:23 -0800152
Alan Viverette19ecd502018-01-05 13:52:16 -0500153 # Persistence
154 'android.arch.persistence:db': ['android-arch-persistence-db', 'arch-persistence/db'],
155 'android.arch.persistence:db-framework': ['android-arch-persistence-db-framework', 'arch-persistence/db-framework'],
Allenab72f012018-01-29 18:10:23 -0800156
Alan Viverette19ecd502018-01-05 13:52:16 -0500157 # Room
158 'android.arch.persistence.room:common': ['android-arch-room-common', 'arch-room/common'],
Alan Viverettec7e88f12018-02-13 15:29:50 -0500159 # Missing dependencies:
160 # - auto-common
161 # - javapoet
162 # - antlr4
163 # - kotlin-metadata
164 # - commons-codec
165 #'android.arch.persistence.room:compiler': ['android-arch-room-compiler', 'arch-room/compiler'],
Alan Viverette19ecd502018-01-05 13:52:16 -0500166 'android.arch.persistence.room:runtime': ['android-arch-room-runtime', 'arch-room/runtime'],
167 'android.arch.persistence.room:migration': ['android-arch-room-migration', 'arch-room/migration'],
Alan Viverettec7e88f12018-02-13 15:29:50 -0500168 # Missing dependencies:
169 # - rxjava
170 #'android.arch.persistence.room:rxjava2': ['android-arch-room-rxjava2', 'arch-room/rxjava2'],
Alan Viverette19ecd502018-01-05 13:52:16 -0500171 'android.arch.persistence.room:testing': ['android-arch-room-testing', 'arch-room/testing'],
Allenab72f012018-01-29 18:10:23 -0800172
Alan Viverette129555e2018-01-30 09:57:57 -0500173 # Material Design Components
174 'com.google.android:flexbox': ['flexbox', 'flexbox'],
175 'design': ['android-support-design', 'design'],
176 'design-animation': ['android-support-design-animation', 'design-animation'],
177 'design-bottomnavigation': ['android-support-design-bottomnavigation', 'design-bottomnavigation'],
178 'design-bottomsheet': ['android-support-design-bottomsheet', 'design-bottomsheet'],
179 'design-button': ['android-support-design-button', 'design-button'],
180 'design-canvas': ['android-support-design-canvas', 'design-canvas'],
181 'design-card': ['android-support-design-card', 'design-card'],
182 'design-chip': ['android-support-design-chip', 'design-chip'],
183 'design-circularreveal': ['android-support-design-circularreveal', 'design-circularreveal'],
184 'design-circularreveal-cardview': ['android-support-design-circularreveal-cardview', 'design-circularreveal-cardview'],
185 'design-circularreveal-coordinatorlayout': ['android-support-design-circularreveal-coordinatorlayout', 'design-circularreveal-coordinatorlayout'],
186 'design-color': ['android-support-design-color', 'design-color'],
187 'design-dialog': ['android-support-design-dialog', 'design-dialog'],
188 'design-drawable': ['android-support-design-drawable', 'design-drawable'],
189 'design-expandable': ['android-support-design-expandable', 'design-expandable'],
190 'design-floatingactionbutton': ['android-support-design-floatingactionbutton', 'design-floatingactionbutton'],
191 'design-internal': ['android-support-design-internal', 'design-internal'],
192 'design-math': ['android-support-design-math', 'design-math'],
193 'design-resources': ['android-support-design-resources', 'design-resources'],
194 'design-ripple': ['android-support-design-ripple', 'design-ripple'],
195 'design-snackbar': ['android-support-design-snackbar', 'design-snackbar'],
196 'design-stateful': ['android-support-design-stateful', 'design-stateful'],
197 'design-textfield': ['android-support-design-textfield', 'design-textfield'],
198 'design-theme': ['android-support-design-theme', 'design-theme'],
199 'design-transformation': ['android-support-design-transformation', 'design-transformation'],
200 'design-typography': ['android-support-design-typography', 'design-typography'],
201 'design-widget': ['android-support-design-widget', 'design-widget'],
Alan Viverettea59082b2018-02-22 10:27:46 -0500202 'design-navigation': ['android-support-design-navigation', 'design-navigation'],
203 'design-tabs': ['android-support-design-tabs', 'design-tabs'],
204 'design-bottomappbar': ['android-support-design-bottomappbar', 'design-bottomappbar'],
205 'design-shape': ['android-support-design-shape', 'design-shape'],
206
Alan Viverette95f6d362017-04-06 09:40:50 -0400207}
208
209# Always remove these files.
210blacklist_files = [
211 'annotations.zip',
212 'public.txt',
213 'R.txt',
Alan Viverette44ad4e42017-08-09 17:45:00 -0400214 'AndroidManifest.xml',
Alan Viverettef48d9b42017-08-17 14:45:46 -0400215 os.path.join('libs','noto-emoji-compat-java.jar')
Alan Viverette95f6d362017-04-06 09:40:50 -0400216]
217
Alan Viverette44ad4e42017-08-09 17:45:00 -0400218artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
Alan Viverette95f6d362017-04-06 09:40:50 -0400219
Alan Viverettec960cfb2017-12-04 13:09:22 -0500220
221class MavenLibraryInfo:
Jeff Gastonc302bb92018-03-23 13:53:48 -0400222 def __init__(self, key, group_id, artifact_id, version, dir, repo_dir, file):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500223 self.key = key
224 self.group_id = group_id
225 self.artifact_id = artifact_id
226 self.version = version
Jeff Gastonc302bb92018-03-23 13:53:48 -0400227 self.dir = dir
Alan Viverettec960cfb2017-12-04 13:09:22 -0500228 self.repo_dir = repo_dir
229 self.file = file
230
231
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500232def print_e(*args, **kwargs):
233 print(*args, file=sys.stderr, **kwargs)
234
Alan Viverette95f6d362017-04-06 09:40:50 -0400235
236def touch(fname, times=None):
237 with open(fname, 'a'):
238 os.utime(fname, times)
239
240
Alan Viverette45837092017-05-12 14:50:53 -0400241def path(*path_parts):
242 return reduce((lambda x, y: os.path.join(x, y)), path_parts)
243
244
Alan Viverettecd3de262017-08-14 09:51:30 -0400245def flatten(list):
246 return reduce((lambda x, y: "%s %s" % (x, y)), list)
247
248
Alan Viverette45837092017-05-12 14:50:53 -0400249def rm(path):
250 if os.path.isdir(path):
251 rmtree(path)
252 elif os.path.exists(path):
253 os.remove(path)
254
255
256def mv(src_path, dst_path):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400257 if os.path.exists(dst_path):
Alan Viverettecd3de262017-08-14 09:51:30 -0400258 rm(dst_path)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400259 if not os.path.exists(os.path.dirname(dst_path)):
260 os.makedirs(os.path.dirname(dst_path))
Alan Viverette45837092017-05-12 14:50:53 -0400261 os.rename(src_path, dst_path)
262
263
Jeff Gastonc302bb92018-03-23 13:53:48 -0400264def detect_artifacts(maven_repo_dirs):
Alan Viveretted4527e62017-05-11 15:03:25 -0400265 maven_lib_info = {}
266
Dan Willemsen814152e2017-11-06 13:22:11 -0800267 # Find the latest revision for each artifact, remove others
Jeff Gastonc302bb92018-03-23 13:53:48 -0400268 for repo_dir in maven_repo_dirs:
Dan Willemsen814152e2017-11-06 13:22:11 -0800269 for root, dirs, files in os.walk(repo_dir):
270 for file in files:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500271 if file[-4:] == ".pom":
272 # Read the POM (hack hack hack).
273 group_id = ''
274 artifact_id = ''
275 version = ''
276 file = os.path.join(root, file)
277 with open(file) as pom_file:
278 for line in pom_file:
279 if line[:11] == ' <groupId>':
280 group_id = line[11:-11]
281 elif line[:14] == ' <artifactId>':
282 artifact_id = line[14:-14]
283 elif line[:11] == ' <version>':
284 version = line[11:-11]
285 if group_id == '' or artifact_id == '' or version == '':
286 print_e('Failed to find Maven artifact data in ' + file)
287 continue
Alan Viverette95f6d362017-04-06 09:40:50 -0400288
Alan Viverettec960cfb2017-12-04 13:09:22 -0500289 # Locate the artifact.
290 artifact_file = file[:-4]
291 if os.path.exists(artifact_file + '.jar'):
292 artifact_file = artifact_file + '.jar'
293 elif os.path.exists(artifact_file + '.aar'):
294 artifact_file = artifact_file + '.aar'
295 else:
296 print_e('Failed to find artifact for ' + artifact_file)
297 continue
298
299 # Make relative to root.
300 artifact_file = artifact_file[len(root) + 1:]
301
302 # Find the mapping.
303 group_artifact = group_id + ':' + artifact_id
304 if artifact_id in maven_to_make:
305 key = artifact_id
306 elif group_artifact in maven_to_make:
307 key = group_artifact
308 else:
309 print_e('Failed to find artifact mapping for ' + group_artifact)
310 continue
311
312 # Store the latest version.
313 version = LooseVersion(version)
314 if key not in maven_lib_info \
315 or version > maven_lib_info[key].version:
316 maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version,
317 root, repo_dir, artifact_file)
Alan Viverette95f6d362017-04-06 09:40:50 -0400318
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400319 return maven_lib_info
320
321
Jeff Gastonc302bb92018-03-23 13:53:48 -0400322def transform_maven_repos(maven_repo_dirs, transformed_dir, extract_res=True):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400323 cwd = os.getcwd()
324
325 # Use a temporary working directory.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400326 maven_lib_info = detect_artifacts(maven_repo_dirs)
Jeff Gastona68a8d42018-03-23 14:00:13 -0400327 working_dir = temp_dir
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400328
Alan Viverettec960cfb2017-12-04 13:09:22 -0500329 if not maven_lib_info:
330 print_e('Failed to detect artifacts')
331 return False
332
Jeff Gastonc302bb92018-03-23 13:53:48 -0400333 # extract some files (for example, AndroidManifest.xml) from any relevant artifacts
Alan Viveretted4527e62017-05-11 15:03:25 -0400334 for info in maven_lib_info.values():
Alan Viveretted8ce7222017-12-11 17:24:43 -0500335 transform_maven_lib(working_dir, info, extract_res)
Dan Willemsen814152e2017-11-06 13:22:11 -0800336
Jeff Gastonc302bb92018-03-23 13:53:48 -0400337 # generate a single Android.mk that specifies to use all of the above artifacts
Alan Viverette4ec9a172018-02-20 16:19:31 -0500338 makefile = os.path.join(working_dir, 'Android.mk')
339 with open(makefile, 'w') as f:
Alan Viverette229a53d2018-02-06 16:25:22 -0500340 args = ["pom2mk", "-static-deps", "-sdk-version", "current"]
Dan Willemsen814152e2017-11-06 13:22:11 -0800341 args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name][0] for name in maven_to_make])
342 args.extend(["."])
Alan Viverette4ec9a172018-02-20 16:19:31 -0500343 args.extend(["-extra-deps=android-support-car=prebuilt-android.car-stubs"])
Dan Willemsen814152e2017-11-06 13:22:11 -0800344 subprocess.check_call(args, stdout=f, cwd=working_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400345
Jeff Gastonf12f3ce2018-03-23 16:41:24 -0400346 global rerun_extract_deps
347 rerun_extract_deps = True
Alan Viverette4ec9a172018-02-20 16:19:31 -0500348
Alan Viverette95f6d362017-04-06 09:40:50 -0400349 # Replace the old directory.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400350 output_dir = os.path.join(cwd, transformed_dir)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400351 mv(working_dir, output_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500352 return True
Alan Viverette95f6d362017-04-06 09:40:50 -0400353
Jeff Gastonc302bb92018-03-23 13:53:48 -0400354# moves <artifact_info> (of type MavenLibraryInfo) into the appropriate part of <working_dir> , and possibly unpacks any necessary included files
Alan Viveretted8ce7222017-12-11 17:24:43 -0500355def transform_maven_lib(working_dir, artifact_info, extract_res):
Dan Willemsen814152e2017-11-06 13:22:11 -0800356 # Move library into working dir
Jeff Gastonc302bb92018-03-23 13:53:48 -0400357 new_dir = os.path.join(working_dir, os.path.relpath(artifact_info.dir, artifact_info.repo_dir))
358 mv(artifact_info.dir, new_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800359
360 for dirpath, dirs, files in os.walk(new_dir):
361 for f in files:
362 if '-sources.jar' in f:
363 os.remove(os.path.join(dirpath, f))
364
Alan Viverettec960cfb2017-12-04 13:09:22 -0500365 matcher = artifact_pattern.match(artifact_info.file)
366 maven_lib_name = artifact_info.key
Alan Viveretted4527e62017-05-11 15:03:25 -0400367 maven_lib_vers = matcher.group(2)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500368 maven_lib_type = artifact_info.file[-3:]
Alan Viveretted4527e62017-05-11 15:03:25 -0400369
Alan Viverettec960cfb2017-12-04 13:09:22 -0500370 make_lib_name = maven_to_make[artifact_info.key][0]
371 make_dir_name = maven_to_make[artifact_info.key][1]
Alan Viveretted4527e62017-05-11 15:03:25 -0400372
Alan Viverette129555e2018-01-30 09:57:57 -0500373 artifact_file = os.path.join(new_dir, artifact_info.file)
374
Dan Willemsen814152e2017-11-06 13:22:11 -0800375 if extract_res:
Dan Willemsen814152e2017-11-06 13:22:11 -0800376 target_dir = os.path.join(working_dir, make_dir_name)
377 if not os.path.exists(target_dir):
378 os.makedirs(target_dir)
379
380 if maven_lib_type == "aar":
Alan Viverettec960cfb2017-12-04 13:09:22 -0500381 process_aar(artifact_file, target_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400382
Alan Viveretted8ce7222017-12-11 17:24:43 -0500383 if maven_lib_type == "aar":
Dan Willemsend677b602017-11-08 22:13:17 -0800384 with zipfile.ZipFile(artifact_file) as zip:
Alan Viveretted8ce7222017-12-11 17:24:43 -0500385 manifests_dir = os.path.join(working_dir, "manifests")
386 zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name))
Dan Willemsend677b602017-11-08 22:13:17 -0800387
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500388 print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name)
Alan Viveretted4527e62017-05-11 15:03:25 -0400389
Alan Viverette95f6d362017-04-06 09:40:50 -0400390
Alan Viverettec960cfb2017-12-04 13:09:22 -0500391def process_aar(artifact_file, target_dir):
Alan Viverette95f6d362017-04-06 09:40:50 -0400392 # Extract AAR file to target_dir.
393 with zipfile.ZipFile(artifact_file) as zip:
394 zip.extractall(target_dir)
395
Dan Willemsen814152e2017-11-06 13:22:11 -0800396 # Remove classes.jar
Alan Viverette95f6d362017-04-06 09:40:50 -0400397 classes_jar = os.path.join(target_dir, "classes.jar")
398 if os.path.exists(classes_jar):
Dan Willemsen814152e2017-11-06 13:22:11 -0800399 os.remove(classes_jar)
Alan Viverette95f6d362017-04-06 09:40:50 -0400400
401 # Remove or preserve empty dirs.
402 for root, dirs, files in os.walk(target_dir):
403 for dir in dirs:
404 dir_path = os.path.join(root, dir)
405 if not os.listdir(dir_path):
406 os.rmdir(dir_path)
407
408 # Remove top-level cruft.
409 for file in blacklist_files:
410 file_path = os.path.join(target_dir, file)
411 if os.path.exists(file_path):
412 os.remove(file_path)
413
Alan Viverettef17c9402017-07-19 12:57:40 -0400414
Alan Viverettecd3de262017-08-14 09:51:30 -0400415def fetch_artifact(target, build_id, artifact_path):
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500416 print('Fetching %s from %s...' % (artifact_path, target))
Alan Viverettecd3de262017-08-14 09:51:30 -0400417 fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path]
Alan Viverette45837092017-05-12 14:50:53 -0400418 try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400419 subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
Alan Viverette45837092017-05-12 14:50:53 -0400420 except subprocess.CalledProcessError:
Jeff Gaston13e38412018-02-06 14:45:36 -0500421 print_e('FAIL: Unable to retrieve %s artifact for build ID %s' % (artifact_path, build_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500422 print_e('Please make sure you are authenticated for build server access!')
Alan Viverette45837092017-05-12 14:50:53 -0400423 return None
424 return artifact_path
425
426
Alan Viverette129555e2018-01-30 09:57:57 -0500427def extract_artifact(artifact_path):
Alan Viverette45837092017-05-12 14:50:53 -0400428 # Unzip the repo archive into a separate directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400429 repo_dir = os.path.basename(artifact_path)[:-4]
Alan Viverette45837092017-05-12 14:50:53 -0400430 with zipfile.ZipFile(artifact_path) as zipFile:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400431 zipFile.extractall(repo_dir)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400432 return repo_dir
433
434
Alan Viverette7e897e22018-03-09 15:24:10 -0500435def fetch_and_extract(target, build_id, file, artifact_path=None):
436 if not artifact_path:
437 artifact_path = fetch_artifact(target, build_id, file)
Alan Viverette129555e2018-01-30 09:57:57 -0500438 if not artifact_path:
439 return None
440 return extract_artifact(artifact_path)
441
442
Alan Viverette7e897e22018-03-09 15:24:10 -0500443def update_support(target, build_id, local_file):
444 if build_id:
445 repo_file = 'top-of-tree-m2repository-%s.zip' % build_id.fs_id
446 repo_dir = fetch_and_extract(target, build_id.url_id, repo_file, None)
447 else:
448 repo_dir = fetch_and_extract(target, None, None, local_file)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400449 if not repo_dir:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500450 print_e('Failed to extract Support Library repository')
Alan Viverettef48d9b42017-08-17 14:45:46 -0400451 return False
Alan Viverette45837092017-05-12 14:50:53 -0400452
453 # Transform the repo archive into a Makefile-compatible format.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400454 return transform_maven_repos([repo_dir], support_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500455
Alan Viverette7e897e22018-03-09 15:24:10 -0500456
Jeff Gaston782c3e32018-02-06 14:36:17 -0500457def update_jetifier(target, build_id):
458 repo_file = 'jetifier-standalone.zip'
459 repo_dir = fetch_and_extract(target, build_id.url_id, repo_file)
460 if not repo_dir:
461 print_e('Failed to extract Jetifier')
462 return False
463
464 rm(jetifier_dir)
465 mv(repo_dir, jetifier_dir)
Jeff Gaston6afb1e42018-02-07 18:58:07 -0500466 os.chmod(os.path.join(jetifier_dir, 'jetifier-standalone', 'bin', 'jetifier-standalone'), 0o755)
Jeff Gaston782c3e32018-02-06 14:36:17 -0500467 return True
468
Alan Viverette7e897e22018-03-09 15:24:10 -0500469
Alan Viverettec960cfb2017-12-04 13:09:22 -0500470def update_toolkit(target, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500471 repo_dir = fetch_and_extract(target, build_id.url_id, 'top-of-tree-m2repository-%s.zip' % build_id.fs_id)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500472 if not repo_dir:
473 print_e('Failed to extract App Toolkit repository')
474 return False
475
476 # Transform the repo archive into a Makefile-compatible format.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400477 return transform_maven_repos([repo_dir], os.path.join(extras_dir, 'app-toolkit'))
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400478
479
480def update_constraint(target, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500481 layout_dir = fetch_and_extract(target, build_id.url_id,
482 'com.android.support.constraint-constraint-layout-%s.zip' % build_id.fs_id)
483 solver_dir = fetch_and_extract(target, build_id.url_id,
484 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id.fs_id)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400485 if not layout_dir or not solver_dir:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500486 print_e('Failed to extract Constraint Layout repositories')
Alan Viverettecd3de262017-08-14 09:51:30 -0400487 return False
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400488
489 # Passing False here is an inelegant solution, but it means we can replace
490 # the top-level directory without worrying about other child directories.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400491 return transform_maven_repos([layout_dir, solver_dir],
Alan Viverettec960cfb2017-12-04 13:09:22 -0500492 os.path.join(extras_dir, 'constraint-layout'), extract_res=False)
Alan Viverette45837092017-05-12 14:50:53 -0400493
494
Alan Viverette129555e2018-01-30 09:57:57 -0500495def update_design(file):
496 design_dir = extract_artifact(file)
497 if not design_dir:
498 print_e('Failed to extract Design Library repositories')
499 return False
500
501 # Don't bother extracting resources -- this should only be used with AAPT2.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400502 return transform_maven_repos([design_dir],
Alan Viverette129555e2018-01-30 09:57:57 -0500503 os.path.join(extras_dir, 'material-design'), extract_res=False)
504
505
Alan Viverette45837092017-05-12 14:50:53 -0400506def extract_to(zip_file, paths, filename, parent_path):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500507 zip_path = next(filter(lambda path: filename in path, paths))
Alan Viverette45837092017-05-12 14:50:53 -0400508 src_path = zip_file.extract(zip_path)
509 dst_path = path(parent_path, filename)
510 mv(src_path, dst_path)
511
512
Alan Viverettecd3de262017-08-14 09:51:30 -0400513def update_sdk_repo(target, build_id):
Alan Viverette45837092017-05-12 14:50:53 -0400514 platform = 'darwin' if 'mac' in target else 'linux'
Alan Viverettecd3de262017-08-14 09:51:30 -0400515 artifact_path = fetch_artifact(
Jeff Gaston13e38412018-02-06 14:45:36 -0500516 target, build_id.url_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id.fs_id))
Alan Viverette45837092017-05-12 14:50:53 -0400517 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400518 return False
Alan Viverette45837092017-05-12 14:50:53 -0400519
520 with zipfile.ZipFile(artifact_path) as zipFile:
521 paths = zipFile.namelist()
522
523 extract_to(zipFile, paths, 'android.jar', current_path)
524 extract_to(zipFile, paths, 'uiautomator.jar', current_path)
525 extract_to(zipFile, paths, 'framework.aidl', current_path)
Paul Duffind891f3c2017-12-06 11:32:56 +0000526 extract_to(zipFile, paths, 'optional/android.test.base.jar', current_path)
Paul Duffin2b1a1462017-07-14 16:21:33 +0100527 extract_to(zipFile, paths, 'optional/android.test.mock.jar', current_path)
528 extract_to(zipFile, paths, 'optional/android.test.runner.jar', current_path)
Alan Viverette45837092017-05-12 14:50:53 -0400529
530 # Unclear if this is actually necessary.
531 extract_to(zipFile, paths, 'framework.aidl', system_path)
Jiyong Parkb773d292018-01-30 23:33:19 +0900532
Colin Cross4f4b9062018-02-14 10:03:19 -0800533 artifact_path = fetch_artifact(target, build_id.fs_id, 'core.current.stubs.jar')
Jiyong Parkb773d292018-01-30 23:33:19 +0900534 if not artifact_path:
535 return False
536
537 mv(artifact_path, path(current_path, 'core.jar'))
Alan Viverettecd3de262017-08-14 09:51:30 -0400538 return True
Alan Viverette45837092017-05-12 14:50:53 -0400539
540
Alan Viverettecd3de262017-08-14 09:51:30 -0400541def update_system(target, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500542 artifact_path = fetch_artifact(target, build_id.url_id, 'android_system.jar')
Alan Viverette45837092017-05-12 14:50:53 -0400543 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400544 return False
Alan Viverette45837092017-05-12 14:50:53 -0400545
546 mv(artifact_path, path(system_path, 'android.jar'))
Paul Duffin960e1ee2017-12-21 08:50:14 +0000547
Jeff Gaston13e38412018-02-06 14:45:36 -0500548 artifact_path = fetch_artifact(target, build_id.url_id, 'android.test.mock.stubs_system.jar')
Paul Duffin960e1ee2017-12-21 08:50:14 +0000549 if not artifact_path:
550 return False
551
552 mv(artifact_path, path(system_path, 'optional/android.test.mock.jar'))
553
Alan Viverettecd3de262017-08-14 09:51:30 -0400554 return True
555
556
Alan Viverettec1c32b62017-12-20 09:40:36 -0500557def update_buildtools(target, arch, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500558 artifact_path = fetch_and_extract(target, build_id.url_id,
559 "sdk-repo-%s-build-tools-%s.zip" % (arch, build_id.fs_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500560 if not artifact_path:
561 return False
562
563 top_level_dir = os.listdir(artifact_path)[0]
564 src_path = os.path.join(artifact_path, top_level_dir)
565 dst_path = path(buildtools_dir, arch)
566 mv(src_path, dst_path)
567
568 # Move all top-level files to /bin and make them executable
569 bin_path = path(dst_path, 'bin')
570 top_level_files = filter(lambda e: os.path.isfile(path(dst_path, e)), os.listdir(dst_path))
571 for file in top_level_files:
572 src_file = path(dst_path, file)
573 dst_file = path(bin_path, file)
574 mv(src_file, dst_file)
575 os.chmod(dst_file, 0o755)
576
577 # Remove renderscript
578 rm(path(dst_path, 'renderscript'))
579
580 return True
581
582
Alan Viverettecd3de262017-08-14 09:51:30 -0400583def append(text, more_text):
584 if text:
585 return "%s, %s" % (text, more_text)
586 return more_text
Alan Viverette45837092017-05-12 14:50:53 -0400587
Alan Viverette95f6d362017-04-06 09:40:50 -0400588
Jeff Gaston13e38412018-02-06 14:45:36 -0500589class buildId(object):
590 def __init__(self, url_id, fs_id):
591 # id when used in build server urls
592 self.url_id = url_id
593 # id when used in build commands
594 self.fs_id = fs_id
595
Jeff Gastonb9b09052018-02-05 14:16:05 -0500596def getBuildId(args):
Jeff Gaston13e38412018-02-06 14:45:36 -0500597 # must be in the format 12345 or P12345
Jeff Gastonb9b09052018-02-05 14:16:05 -0500598 source = args.source
Jeff Gaston13e38412018-02-06 14:45:36 -0500599 number_text = source[:]
600 presubmit = False
601 if number_text.startswith("P"):
602 presubmit = True
603 number_text = number_text[1:]
604 if not number_text.isnumeric():
Alan Viverette7e897e22018-03-09 15:24:10 -0500605 return None
Jeff Gaston13e38412018-02-06 14:45:36 -0500606 url_id = source
607 fs_id = url_id
608 if presubmit:
609 fs_id = "0"
610 args.file = False
611 return buildId(url_id, fs_id)
Jeff Gastonb9b09052018-02-05 14:16:05 -0500612
Colin Cross573e1212018-02-12 14:59:52 -0800613def getFile(args):
614 source = args.source
615 if not source.isnumeric():
616 return args.source
Alan Viverette7e897e22018-03-09 15:24:10 -0500617 return None
Colin Cross573e1212018-02-12 14:59:52 -0800618
Alan Viverette4ec9a172018-02-20 16:19:31 -0500619
620def script_relative(rel_path):
Jeff Gastona68a8d42018-03-23 14:00:13 -0400621 return os.path.join(script_dir, rel_path)
Alan Viverette4ec9a172018-02-20 16:19:31 -0500622
623
Jeff Gastoncc296a82018-03-23 14:33:24 -0400624def uncommittedChangesExist():
625 try:
626 # Make sure we don't overwrite any pending changes.
627 diffCommand = "cd " + git_dir + " && git diff --quiet"
628 subprocess.check_call(diffCommand, shell=True)
629 subprocess.check_call(diffCommand + " --cached", shell=True)
630 return False
631 except subprocess.CalledProcessError:
632 return True
633
634
Alan Viveretted4527e62017-05-11 15:03:25 -0400635parser = argparse.ArgumentParser(
Alan Viverette45837092017-05-12 14:50:53 -0400636 description=('Update current prebuilts'))
Alan Viveretted4527e62017-05-11 15:03:25 -0400637parser.add_argument(
Alan Viverette129555e2018-01-30 09:57:57 -0500638 'source',
639 help='Build server build ID or local Maven ZIP file')
640parser.add_argument(
641 '-d', '--design', action="store_true",
642 help='If specified, updates only the Design Library')
Alan Viveretted4527e62017-05-11 15:03:25 -0400643parser.add_argument(
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400644 '-c', '--constraint', action="store_true",
645 help='If specified, updates only Constraint Layout')
646parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400647 '-s', '--support', action="store_true",
648 help='If specified, updates only the Support Library')
Alan Viverette45837092017-05-12 14:50:53 -0400649parser.add_argument(
Jeff Gaston782c3e32018-02-06 14:36:17 -0500650 '-j', '--jetifier', action="store_true",
651 help='If specified, updates only Jetifier')
652parser.add_argument(
Alan Viverettec960cfb2017-12-04 13:09:22 -0500653 '-t', '--toolkit', action="store_true",
654 help='If specified, updates only the App Toolkit')
655parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400656 '-p', '--platform', action="store_true",
657 help='If specified, updates only the Android Platform')
Alan Viverettec1c32b62017-12-20 09:40:36 -0500658parser.add_argument(
659 '-b', '--buildtools', action="store_true",
660 help='If specified, updates only the Build Tools')
Jeff Gastoncc296a82018-03-23 14:33:24 -0400661parser.add_argument(
662 '--commit-first', action="store_true",
663 help='If specified, then if uncommited changes exist, commit before continuing')
Alan Viveretted4527e62017-05-11 15:03:25 -0400664args = parser.parse_args()
Jeff Gaston56fe2762018-02-06 14:54:37 -0500665args.file = True
Alan Viverette129555e2018-01-30 09:57:57 -0500666if not args.source:
667 parser.error("You must specify a build ID or local Maven ZIP file")
Alan Viveretted4527e62017-05-11 15:03:25 -0400668 sys.exit(1)
Alan Viverette129555e2018-01-30 09:57:57 -0500669if not (args.support or args.platform or args.constraint or args.toolkit or args.buildtools \
Jeff Gaston782c3e32018-02-06 14:36:17 -0500670 or args.design or args.jetifier):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500671 parser.error("You must specify at least one target to update")
Alan Viverettecd3de262017-08-14 09:51:30 -0400672 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500673if (args.support or args.constraint or args.toolkit) and which('pom2mk') is None:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500674 parser.error("Cannot find pom2mk in path; please run lunch to set up build environment")
675 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400676
Jeff Gastoncc296a82018-03-23 14:33:24 -0400677if uncommittedChangesExist():
678 if args.commit_first:
679 subprocess.check_call("cd " + git_dir + " && git add -u", shell=True)
680 subprocess.check_call("cd " + git_dir + " && git commit -m 'save working state'", shell=True)
681
682if uncommittedChangesExist():
683 print_e('FAIL: There are uncommitted changes here. Please commit or stash before continuing, because update_current.py will run "git reset --hard" if execution fails')
Alan Viveretted4527e62017-05-11 15:03:25 -0400684 sys.exit(1)
685
686try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400687 components = None
688 if args.constraint:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500689 if update_constraint('studio', getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400690 components = append(components, 'Constraint Layout')
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500691 print_e('Failed to update Constraint Layout, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400692 else:
693 sys.exit(1)
694 if args.support:
Alan Viverette7e897e22018-03-09 15:24:10 -0500695 if update_support('support_library', getBuildId(args), getFile(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400696 components = append(components, 'Support Library')
697 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500698 print_e('Failed to update Support Library, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400699 sys.exit(1)
Jeff Gaston782c3e32018-02-06 14:36:17 -0500700 if args.jetifier:
701 if update_jetifier('support_library', getBuildId(args)):
702 components = append(components, 'Jetifier')
703 else:
704 print_e('Failed to update Jetifier, aborting...')
705 sys.exit(1)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500706 if args.toolkit:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500707 if update_toolkit('support_library_app_toolkit', getBuildId(args)):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500708 components = append(components, 'App Toolkit')
709 else:
710 print_e('Failed to update App Toolkit, aborting...')
711 sys.exit(1)
Alan Viverettecd3de262017-08-14 09:51:30 -0400712 if args.platform:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500713 if update_sdk_repo('sdk_phone_armv7-sdk_mac', getBuildId(args)) \
714 and update_system('sdk_phone_armv7-sdk_mac', getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400715 components = append(components, 'platform SDK')
716 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500717 print_e('Failed to update platform SDK, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400718 sys.exit(1)
Alan Viverette129555e2018-01-30 09:57:57 -0500719 if args.design:
Colin Cross573e1212018-02-12 14:59:52 -0800720 if update_design(getFile(args)):
Alan Viverette129555e2018-01-30 09:57:57 -0500721 components = append(components, 'Design Library')
722 else:
723 print_e('Failed to update platform SDK, aborting...')
724 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500725 if args.buildtools:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500726 if update_buildtools('sdk_phone_armv7-sdk_mac', 'darwin', getBuildId(args)) \
727 and update_buildtools('sdk_phone_x86_64-sdk', 'linux', getBuildId(args)) \
728 and update_buildtools('sdk_phone_armv7-win_sdk', 'windows', getBuildId(args)):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500729 components = append(components, 'build tools')
730 else:
731 print_e('Failed to update build tools, aborting...')
732 sys.exit(1)
Jeff Gastonf12f3ce2018-03-23 16:41:24 -0400733 if rerun_extract_deps:
734 depsfile = 'fix_dependencies.mk'
735 cwd=os.getcwd()
736 with open(depsfile, 'w') as f:
737 print("running " + str(args) + " in " + cwd)
738 subprocess.check_call("./update_current/extract_deps.py current/support/Android.mk current/extras/*/Android.mk", stdout=f, cwd=cwd, shell=True)
739
Alan Viveretted4527e62017-05-11 15:03:25 -0400740
741 # Commit all changes.
Alan Viverette45837092017-05-12 14:50:53 -0400742 subprocess.check_call(['git', 'add', current_path])
743 subprocess.check_call(['git', 'add', system_path])
Alan Viverettec1c32b62017-12-20 09:40:36 -0500744 subprocess.check_call(['git', 'add', buildtools_dir])
Colin Cross573e1212018-02-12 14:59:52 -0800745 if not args.source.isnumeric():
Alan Viverette129555e2018-01-30 09:57:57 -0500746 src_msg = "local Maven ZIP"
747 else:
Jeff Gaston13e38412018-02-06 14:45:36 -0500748 src_msg = "build %s" % (getBuildId(args).url_id)
Alan Viverette129555e2018-01-30 09:57:57 -0500749 msg = "Import %s from %s\n\n%s" % (components, src_msg, flatten(sys.argv))
Alan Viveretted4527e62017-05-11 15:03:25 -0400750 subprocess.check_call(['git', 'commit', '-m', msg])
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500751 print('Remember to test this change before uploading it to Gerrit!')
Alan Viveretted4527e62017-05-11 15:03:25 -0400752
753finally:
754 # Revert all stray files, including the downloaded zip.
755 try:
756 with open(os.devnull, 'w') as bitbucket:
757 subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
758 subprocess.check_call(
759 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!'], stdout=bitbucket)
760 subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
761 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500762 print_e('ERROR: Failed cleaning up, manual cleanup required!!!')