blob: 0af4dc6fa8e140bbccb39165965dfbfa0b424cb3 [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
Alan Viverettecd3de262017-08-14 09:51:30 -040024# See go/fetch_artifact for details on this script.
Alan Viveretted4527e62017-05-11 15:03:25 -040025FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
26
Alan Viverette95f6d362017-04-06 09:40:50 -040027maven_to_make = {
Alan Viverette7e897e22018-03-09 15:24:10 -050028 'animated-vector-drawable': ['android-support-animatedvectordrawable', 'graphics/drawable'],
29 'appcompat-v7': ['android-support-v7-appcompat', 'v7/appcompat'],
30 'cardview-v7': ['android-support-v7-cardview', 'v7/cardview'],
31 'collections': ['android-support-collections', 'collections'],
32 'customtabs': ['android-support-customtabs', 'customtabs'],
33 'exifinterface': ['android-support-exifinterface', 'exifinterface'],
34 'gridlayout-v7': ['android-support-v7-gridlayout', 'v7/gridlayout'],
35 'leanback-v17': ['android-support-v17-leanback', 'v17/leanback'],
36 'mediarouter-v7': ['android-support-v7-mediarouter', 'v7/mediarouter'],
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', 'jar'],
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-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
50 'support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
51 'support-emoji': ['android-support-emoji', 'emoji'],
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'],
55 'support-v13': ['android-support-v13', 'v13'],
56 'support-v4': ['android-support-v4', 'v4'],
57 'support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'],
58 'transition': ['android-support-transition', 'transition'],
59 'wear': ['android-support-wear', 'wear'],
Alan Viverette7bb86da2018-02-21 18:29:02 -050060
Alan Viverette7e897e22018-03-09 15:24:10 -050061 # AndroidX renamed modules
62 'androidx.annotation:annotation': ['android-support-annotations', 'annotations', 'jar'],
63 'androidx.appcompat:appcompat': ['android-support-v7-appcompat', 'v7/appcompat'],
64 'androidx.browser:browser': ['android-support-customtabs', 'customtabs'],
65 'androidx.cardview:cardview': ['android-support-v7-cardview', 'v7/cardview'],
66 'androidx.collection:collection': ['android-support-collections', 'collections'],
67 'androidx.core:core': ['android-support-compat', 'compat'],
68 'androidx.dynamicanimation:dynamicanimation': ['android-support-dynamic-animation', 'dynamic-animation'],
69 'androidx.emoji:emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
70 'androidx.emoji:emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
71 'androidx.emoji:emoji': ['android-support-emoji', 'emoji'],
72 'androidx.fragment:fragment': ['android-support-fragment', 'fragment'],
73 'androidx.gridlayout:gridlayout': ['android-support-v7-gridlayout', 'v7/gridlayout'],
74 'androidx.leanback:leanback-preference': ['android-support-v17-preference-leanback', 'v17/preference-leanback'],
75 'androidx.leanback:leanback': ['android-support-v17-leanback', 'v17/leanback'],
76 'androidx.legacy:legacy-preference-v14': ['android-support-v14-preference', 'v14/preference'],
77 'androidx.legacy:legacy-support-core-ui': ['android-support-core-ui', 'core-ui'],
78 'androidx.legacy:legacy-support-core-utils': ['android-support-core-utils', 'core-utils'],
79 'androidx.legacy:legacy-support-v13': ['android-support-v13', 'v13'],
80 'androidx.legacy:legacy-support-v4': ['android-support-v4', 'v4'],
81 'androidx.media:media': ['android-support-media-compat', 'media-compat'],
82 'androidx.mediarouter:mediarouter': ['android-support-v7-mediarouter', 'v7/mediarouter'],
83 'androidx.palette:palette': ['android-support-v7-palette', 'v7/palette'],
84 'androidx.percentlayout:percentlayout': ['android-support-percent', 'percent'],
85 'androidx.preference:preference': ['android-support-v7-preference', 'v7/preference'],
86 'androidx.recyclerview:recyclerview': ['android-support-v7-recyclerview', 'v7/recyclerview'],
87 'androidx.tvprovider:tvprovider': ['android-support-tv-provider', 'tv-provider'],
88 'androidx.vectordrawable:vectordrawable-animated': ['android-support-animatedvectordrawable', 'graphics/drawable'],
89 'androidx.vectordrawable:vectordrawable': ['android-support-vectordrawable', 'graphics/drawable'],
90
91 # AndroidX new modules
92 'heifwriter': ['android-support-heifwriter', 'heifwriter'],
93 'webkit': ['android-support-webkit', 'webkit'],
94 'customview': ['android-support-customview', 'customview'],
95 'textclassifier': ['android-support-textclassifier', 'textclassifier'],
96 'swiperefreshlayout': ['android-support-swiperefreshlayout', 'swiperefreshlayout'],
97 'viewpager': ['android-support-viewpager', 'viewpager'],
98 'coordinatorlayout': ['android-support-coordinatorlayout', 'coordinatorlayout'],
99 'asynclayoutinflater': ['android-support-asynclayoutinflater', 'asynclayoutinflater'],
100 'support-content': ['android-support-support-content', 'support-content'],
101 'documentfile': ['android-support-documentfile', 'documentfile'],
102 'drawerlayout': ['android-support-drawerlayout', 'drawerlayout'],
103 'localbroadcastmanager': ['android-support-localbroadcastmanager', 'localbroadcastmanager'],
104 'print': ['android-support-print', 'print'],
105 'slidingpanelayout': ['android-support-slidingpanelayout', 'slidingpanelayout'],
106 'interpolator': ['android-support-interpolator', 'interpolator'],
107 'cursoradapter': ['android-support-cursoradapter', 'cursoradapter'],
108 'loader': ['android-support-loader', 'loader'],
109 'contentpaging': ['android-support-contentpaging', 'contentpaging'],
110 'recyclerview-selection': ['android-support-recyclerview-selection', 'recyclerview-selection'],
Alan Viverette4ec9a172018-02-20 16:19:31 -0500111 'car': ['android-support-car', 'car'],
Alan Viverette7bb86da2018-02-21 18:29:02 -0500112
Alan Viverette19ecd502018-01-05 13:52:16 -0500113 # Slices
114 'slices-core': ['android-slices-core', 'slices-core'],
115 'slices-view': ['android-slices-view', 'slices-view'],
116 'slices-builders': ['android-slices-builders', 'slices-builders'],
117
118 # Multidex
119 'multidex': ['android-support-multidex', 'multidex/library'],
120 'multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'],
121
122 # Constraint Layout
123 'constraint-layout': ['android-support-constraint-layout', 'constraint-layout'],
124 'constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver'],
125
126 # App Arch Core
127 'android.arch.core:runtime': ['android-arch-core-runtime', 'arch-core/runtime'],
128 'android.arch.core:common': ['android-arch-core-common', 'arch-core/common'],
Allenab72f012018-01-29 18:10:23 -0800129 'android.arch.core:testing': ['android-arch-core-testing', 'arch-core/testing'],
130
Alan Viverette19ecd502018-01-05 13:52:16 -0500131 # Paging
132 'android.arch.paging:common': ['android-arch-paging-common', 'arch-paging/common'],
133 'android.arch.paging:runtime': ['android-arch-paging-runtime', 'arch-paging/runtime'],
Allenab72f012018-01-29 18:10:23 -0800134
Alan Viverette19ecd502018-01-05 13:52:16 -0500135 # Lifecycle
Allenab72f012018-01-29 18:10:23 -0800136 'android.arch.lifecycle:livedata-core': ['android-arch-lifecycle-livedata-core', 'arch-lifecycle/livedata-core'],
137 'android.arch.lifecycle:livedata': ['android-arch-lifecycle-livedata', 'arch-lifecycle/livedata'],
138 'android.arch.lifecycle:viewmodel': ['android-arch-lifecycle-viewmodel', 'arch-lifecycle/viewmodel'],
139 'android.arch.lifecycle:extensions': ['android-arch-lifecycle-extensions', 'arch-lifecycle/extensions'],
140 'android.arch.lifecycle:runtime': ['android-arch-lifecycle-runtime', 'arch-lifecycle/runtime'],
141 'android.arch.lifecycle:common': ['android-arch-lifecycle-common', 'arch-lifecycle/common'],
142 'android.arch.lifecycle:common-java8': ['android-arch-lifecycle-common-java8', 'arch-lifecycle/common-java8'],
Alan Viverettec7e88f12018-02-13 15:29:50 -0500143 # Missing dependencies:
144 # - auto-common
145 # - javapoet
146 #'android.arch.lifecycle:compiler': ['android-arch-lifecycle-compiler', 'arch-lifecycle/compiler'],
147 # Missing dependencies:
148 # - reactive-streams
149 #'android.arch.lifecycle:reactivestreams': ['android-arch-lifecycle-reactivestreams','arch-lifecycle/reactivestreams'],
Allenab72f012018-01-29 18:10:23 -0800150
Alan Viverette19ecd502018-01-05 13:52:16 -0500151 # Persistence
152 'android.arch.persistence:db': ['android-arch-persistence-db', 'arch-persistence/db'],
153 'android.arch.persistence:db-framework': ['android-arch-persistence-db-framework', 'arch-persistence/db-framework'],
Allenab72f012018-01-29 18:10:23 -0800154
Alan Viverette19ecd502018-01-05 13:52:16 -0500155 # Room
156 'android.arch.persistence.room:common': ['android-arch-room-common', 'arch-room/common'],
Alan Viverettec7e88f12018-02-13 15:29:50 -0500157 # Missing dependencies:
158 # - auto-common
159 # - javapoet
160 # - antlr4
161 # - kotlin-metadata
162 # - commons-codec
163 #'android.arch.persistence.room:compiler': ['android-arch-room-compiler', 'arch-room/compiler'],
Alan Viverette19ecd502018-01-05 13:52:16 -0500164 'android.arch.persistence.room:runtime': ['android-arch-room-runtime', 'arch-room/runtime'],
165 'android.arch.persistence.room:migration': ['android-arch-room-migration', 'arch-room/migration'],
Alan Viverettec7e88f12018-02-13 15:29:50 -0500166 # Missing dependencies:
167 # - rxjava
168 #'android.arch.persistence.room:rxjava2': ['android-arch-room-rxjava2', 'arch-room/rxjava2'],
Alan Viverette19ecd502018-01-05 13:52:16 -0500169 'android.arch.persistence.room:testing': ['android-arch-room-testing', 'arch-room/testing'],
Allenab72f012018-01-29 18:10:23 -0800170
Alan Viverette129555e2018-01-30 09:57:57 -0500171 # Material Design Components
172 'com.google.android:flexbox': ['flexbox', 'flexbox'],
173 'design': ['android-support-design', 'design'],
174 'design-animation': ['android-support-design-animation', 'design-animation'],
175 'design-bottomnavigation': ['android-support-design-bottomnavigation', 'design-bottomnavigation'],
176 'design-bottomsheet': ['android-support-design-bottomsheet', 'design-bottomsheet'],
177 'design-button': ['android-support-design-button', 'design-button'],
178 'design-canvas': ['android-support-design-canvas', 'design-canvas'],
179 'design-card': ['android-support-design-card', 'design-card'],
180 'design-chip': ['android-support-design-chip', 'design-chip'],
181 'design-circularreveal': ['android-support-design-circularreveal', 'design-circularreveal'],
182 'design-circularreveal-cardview': ['android-support-design-circularreveal-cardview', 'design-circularreveal-cardview'],
183 'design-circularreveal-coordinatorlayout': ['android-support-design-circularreveal-coordinatorlayout', 'design-circularreveal-coordinatorlayout'],
184 'design-color': ['android-support-design-color', 'design-color'],
185 'design-dialog': ['android-support-design-dialog', 'design-dialog'],
186 'design-drawable': ['android-support-design-drawable', 'design-drawable'],
187 'design-expandable': ['android-support-design-expandable', 'design-expandable'],
188 'design-floatingactionbutton': ['android-support-design-floatingactionbutton', 'design-floatingactionbutton'],
189 'design-internal': ['android-support-design-internal', 'design-internal'],
190 'design-math': ['android-support-design-math', 'design-math'],
191 'design-resources': ['android-support-design-resources', 'design-resources'],
192 'design-ripple': ['android-support-design-ripple', 'design-ripple'],
193 'design-snackbar': ['android-support-design-snackbar', 'design-snackbar'],
194 'design-stateful': ['android-support-design-stateful', 'design-stateful'],
195 'design-textfield': ['android-support-design-textfield', 'design-textfield'],
196 'design-theme': ['android-support-design-theme', 'design-theme'],
197 'design-transformation': ['android-support-design-transformation', 'design-transformation'],
198 'design-typography': ['android-support-design-typography', 'design-typography'],
199 'design-widget': ['android-support-design-widget', 'design-widget'],
Alan Viverettea59082b2018-02-22 10:27:46 -0500200 'design-navigation': ['android-support-design-navigation', 'design-navigation'],
201 'design-tabs': ['android-support-design-tabs', 'design-tabs'],
202 'design-bottomappbar': ['android-support-design-bottomappbar', 'design-bottomappbar'],
203 'design-shape': ['android-support-design-shape', 'design-shape'],
204
Alan Viverette95f6d362017-04-06 09:40:50 -0400205}
206
207# Always remove these files.
208blacklist_files = [
209 'annotations.zip',
210 'public.txt',
211 'R.txt',
Alan Viverette44ad4e42017-08-09 17:45:00 -0400212 'AndroidManifest.xml',
Alan Viverettef48d9b42017-08-17 14:45:46 -0400213 os.path.join('libs','noto-emoji-compat-java.jar')
Alan Viverette95f6d362017-04-06 09:40:50 -0400214]
215
Alan Viverette44ad4e42017-08-09 17:45:00 -0400216artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
Alan Viverette95f6d362017-04-06 09:40:50 -0400217
Alan Viverettec960cfb2017-12-04 13:09:22 -0500218
219class MavenLibraryInfo:
220 def __init__(self, key, group_id, artifact_id, version, root, repo_dir, file):
221 self.key = key
222 self.group_id = group_id
223 self.artifact_id = artifact_id
224 self.version = version
225 self.root = root
226 self.repo_dir = repo_dir
227 self.file = file
228
229
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500230def print_e(*args, **kwargs):
231 print(*args, file=sys.stderr, **kwargs)
232
Alan Viverette95f6d362017-04-06 09:40:50 -0400233
234def touch(fname, times=None):
235 with open(fname, 'a'):
236 os.utime(fname, times)
237
238
Alan Viverette45837092017-05-12 14:50:53 -0400239def path(*path_parts):
240 return reduce((lambda x, y: os.path.join(x, y)), path_parts)
241
242
Alan Viverettecd3de262017-08-14 09:51:30 -0400243def flatten(list):
244 return reduce((lambda x, y: "%s %s" % (x, y)), list)
245
246
Alan Viverette45837092017-05-12 14:50:53 -0400247def rm(path):
248 if os.path.isdir(path):
249 rmtree(path)
250 elif os.path.exists(path):
251 os.remove(path)
252
253
254def mv(src_path, dst_path):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400255 if os.path.exists(dst_path):
Alan Viverettecd3de262017-08-14 09:51:30 -0400256 rm(dst_path)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400257 if not os.path.exists(os.path.dirname(dst_path)):
258 os.makedirs(os.path.dirname(dst_path))
Alan Viverette45837092017-05-12 14:50:53 -0400259 os.rename(src_path, dst_path)
260
261
Dan Willemsen814152e2017-11-06 13:22:11 -0800262def detect_artifacts(repo_dirs):
Alan Viveretted4527e62017-05-11 15:03:25 -0400263 maven_lib_info = {}
264
Dan Willemsen814152e2017-11-06 13:22:11 -0800265 # Find the latest revision for each artifact, remove others
266 for repo_dir in repo_dirs:
267 for root, dirs, files in os.walk(repo_dir):
268 for file in files:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500269 if file[-4:] == ".pom":
270 # Read the POM (hack hack hack).
271 group_id = ''
272 artifact_id = ''
273 version = ''
274 file = os.path.join(root, file)
275 with open(file) as pom_file:
276 for line in pom_file:
277 if line[:11] == ' <groupId>':
278 group_id = line[11:-11]
279 elif line[:14] == ' <artifactId>':
280 artifact_id = line[14:-14]
281 elif line[:11] == ' <version>':
282 version = line[11:-11]
283 if group_id == '' or artifact_id == '' or version == '':
284 print_e('Failed to find Maven artifact data in ' + file)
285 continue
Alan Viverette95f6d362017-04-06 09:40:50 -0400286
Alan Viverettec960cfb2017-12-04 13:09:22 -0500287 # Locate the artifact.
288 artifact_file = file[:-4]
289 if os.path.exists(artifact_file + '.jar'):
290 artifact_file = artifact_file + '.jar'
291 elif os.path.exists(artifact_file + '.aar'):
292 artifact_file = artifact_file + '.aar'
293 else:
294 print_e('Failed to find artifact for ' + artifact_file)
295 continue
296
297 # Make relative to root.
298 artifact_file = artifact_file[len(root) + 1:]
299
300 # Find the mapping.
301 group_artifact = group_id + ':' + artifact_id
302 if artifact_id in maven_to_make:
303 key = artifact_id
304 elif group_artifact in maven_to_make:
305 key = group_artifact
306 else:
307 print_e('Failed to find artifact mapping for ' + group_artifact)
308 continue
309
310 # Store the latest version.
311 version = LooseVersion(version)
312 if key not in maven_lib_info \
313 or version > maven_lib_info[key].version:
314 maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version,
315 root, repo_dir, artifact_file)
Alan Viverette95f6d362017-04-06 09:40:50 -0400316
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400317 return maven_lib_info
318
319
Alan Viveretted8ce7222017-12-11 17:24:43 -0500320def transform_maven_repo(repo_dirs, update_dir, extract_res=True):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400321 cwd = os.getcwd()
322
323 # Use a temporary working directory.
Dan Willemsen814152e2017-11-06 13:22:11 -0800324 maven_lib_info = detect_artifacts(repo_dirs)
Jeff Gastona68a8d42018-03-23 14:00:13 -0400325 working_dir = temp_dir
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400326
Alan Viverettec960cfb2017-12-04 13:09:22 -0500327 if not maven_lib_info:
328 print_e('Failed to detect artifacts')
329 return False
330
Alan Viveretted4527e62017-05-11 15:03:25 -0400331 for info in maven_lib_info.values():
Alan Viveretted8ce7222017-12-11 17:24:43 -0500332 transform_maven_lib(working_dir, info, extract_res)
Dan Willemsen814152e2017-11-06 13:22:11 -0800333
Alan Viverette4ec9a172018-02-20 16:19:31 -0500334 makefile = os.path.join(working_dir, 'Android.mk')
335 with open(makefile, 'w') as f:
Alan Viverette229a53d2018-02-06 16:25:22 -0500336 args = ["pom2mk", "-static-deps", "-sdk-version", "current"]
Dan Willemsen814152e2017-11-06 13:22:11 -0800337 args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name][0] for name in maven_to_make])
338 args.extend(["."])
Alan Viverette4ec9a172018-02-20 16:19:31 -0500339 args.extend(["-extra-deps=android-support-car=prebuilt-android.car-stubs"])
Dan Willemsen814152e2017-11-06 13:22:11 -0800340 subprocess.check_call(args, stdout=f, cwd=working_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400341
Alan Viverette4ec9a172018-02-20 16:19:31 -0500342 depsfile = os.path.join(working_dir, 'fix_dependencies.mk')
343 with open(depsfile, 'w') as f:
344 args = [script_relative("extract_deps.py"), makefile]
345 subprocess.check_call(args, stdout=f, cwd=cwd)
346
Alan Viverette95f6d362017-04-06 09:40:50 -0400347 # Replace the old directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400348 output_dir = os.path.join(cwd, update_dir)
349 mv(working_dir, output_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500350 return True
Alan Viverette95f6d362017-04-06 09:40:50 -0400351
Alan Viveretted4527e62017-05-11 15:03:25 -0400352
Alan Viveretted8ce7222017-12-11 17:24:43 -0500353def transform_maven_lib(working_dir, artifact_info, extract_res):
Dan Willemsen814152e2017-11-06 13:22:11 -0800354 # Move library into working dir
Alan Viverettec960cfb2017-12-04 13:09:22 -0500355 new_dir = os.path.join(working_dir, os.path.relpath(artifact_info.root, artifact_info.repo_dir))
356 mv(artifact_info.root, new_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800357
358 for dirpath, dirs, files in os.walk(new_dir):
359 for f in files:
360 if '-sources.jar' in f:
361 os.remove(os.path.join(dirpath, f))
362
Alan Viverettec960cfb2017-12-04 13:09:22 -0500363 matcher = artifact_pattern.match(artifact_info.file)
364 maven_lib_name = artifact_info.key
Alan Viveretted4527e62017-05-11 15:03:25 -0400365 maven_lib_vers = matcher.group(2)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500366 maven_lib_type = artifact_info.file[-3:]
Alan Viveretted4527e62017-05-11 15:03:25 -0400367
Alan Viverettec960cfb2017-12-04 13:09:22 -0500368 make_lib_name = maven_to_make[artifact_info.key][0]
369 make_dir_name = maven_to_make[artifact_info.key][1]
Alan Viveretted4527e62017-05-11 15:03:25 -0400370
Alan Viverette129555e2018-01-30 09:57:57 -0500371 artifact_file = os.path.join(new_dir, artifact_info.file)
372
Dan Willemsen814152e2017-11-06 13:22:11 -0800373 if extract_res:
Dan Willemsen814152e2017-11-06 13:22:11 -0800374 target_dir = os.path.join(working_dir, make_dir_name)
375 if not os.path.exists(target_dir):
376 os.makedirs(target_dir)
377
378 if maven_lib_type == "aar":
Alan Viverettec960cfb2017-12-04 13:09:22 -0500379 process_aar(artifact_file, target_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400380
Alan Viveretted8ce7222017-12-11 17:24:43 -0500381 if maven_lib_type == "aar":
Dan Willemsend677b602017-11-08 22:13:17 -0800382 with zipfile.ZipFile(artifact_file) as zip:
Alan Viveretted8ce7222017-12-11 17:24:43 -0500383 manifests_dir = os.path.join(working_dir, "manifests")
384 zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name))
Dan Willemsend677b602017-11-08 22:13:17 -0800385
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500386 print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name)
Alan Viveretted4527e62017-05-11 15:03:25 -0400387
Alan Viverette95f6d362017-04-06 09:40:50 -0400388
Alan Viverettec960cfb2017-12-04 13:09:22 -0500389def process_aar(artifact_file, target_dir):
Alan Viverette95f6d362017-04-06 09:40:50 -0400390 # Extract AAR file to target_dir.
391 with zipfile.ZipFile(artifact_file) as zip:
392 zip.extractall(target_dir)
393
Dan Willemsen814152e2017-11-06 13:22:11 -0800394 # Remove classes.jar
Alan Viverette95f6d362017-04-06 09:40:50 -0400395 classes_jar = os.path.join(target_dir, "classes.jar")
396 if os.path.exists(classes_jar):
Dan Willemsen814152e2017-11-06 13:22:11 -0800397 os.remove(classes_jar)
Alan Viverette95f6d362017-04-06 09:40:50 -0400398
399 # Remove or preserve empty dirs.
400 for root, dirs, files in os.walk(target_dir):
401 for dir in dirs:
402 dir_path = os.path.join(root, dir)
403 if not os.listdir(dir_path):
404 os.rmdir(dir_path)
405
406 # Remove top-level cruft.
407 for file in blacklist_files:
408 file_path = os.path.join(target_dir, file)
409 if os.path.exists(file_path):
410 os.remove(file_path)
411
Alan Viverettef17c9402017-07-19 12:57:40 -0400412
Alan Viverettecd3de262017-08-14 09:51:30 -0400413def fetch_artifact(target, build_id, artifact_path):
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500414 print('Fetching %s from %s...' % (artifact_path, target))
Alan Viverettecd3de262017-08-14 09:51:30 -0400415 fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path]
Alan Viverette45837092017-05-12 14:50:53 -0400416 try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400417 subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
Alan Viverette45837092017-05-12 14:50:53 -0400418 except subprocess.CalledProcessError:
Jeff Gaston13e38412018-02-06 14:45:36 -0500419 print_e('FAIL: Unable to retrieve %s artifact for build ID %s' % (artifact_path, build_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500420 print_e('Please make sure you are authenticated for build server access!')
Alan Viverette45837092017-05-12 14:50:53 -0400421 return None
422 return artifact_path
423
424
Alan Viverette129555e2018-01-30 09:57:57 -0500425def extract_artifact(artifact_path):
Alan Viverette45837092017-05-12 14:50:53 -0400426 # Unzip the repo archive into a separate directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400427 repo_dir = os.path.basename(artifact_path)[:-4]
Alan Viverette45837092017-05-12 14:50:53 -0400428 with zipfile.ZipFile(artifact_path) as zipFile:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400429 zipFile.extractall(repo_dir)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400430 return repo_dir
431
432
Alan Viverette7e897e22018-03-09 15:24:10 -0500433def fetch_and_extract(target, build_id, file, artifact_path=None):
434 if not artifact_path:
435 artifact_path = fetch_artifact(target, build_id, file)
Alan Viverette129555e2018-01-30 09:57:57 -0500436 if not artifact_path:
437 return None
438 return extract_artifact(artifact_path)
439
440
Alan Viverette7e897e22018-03-09 15:24:10 -0500441def update_support(target, build_id, local_file):
442 if build_id:
443 repo_file = 'top-of-tree-m2repository-%s.zip' % build_id.fs_id
444 repo_dir = fetch_and_extract(target, build_id.url_id, repo_file, None)
445 else:
446 repo_dir = fetch_and_extract(target, None, None, local_file)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400447 if not repo_dir:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500448 print_e('Failed to extract Support Library repository')
Alan Viverettef48d9b42017-08-17 14:45:46 -0400449 return False
Alan Viverette45837092017-05-12 14:50:53 -0400450
451 # Transform the repo archive into a Makefile-compatible format.
Alan Viveretted8ce7222017-12-11 17:24:43 -0500452 return transform_maven_repo([repo_dir], support_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500453
Alan Viverette7e897e22018-03-09 15:24:10 -0500454
Jeff Gaston782c3e32018-02-06 14:36:17 -0500455def update_jetifier(target, build_id):
456 repo_file = 'jetifier-standalone.zip'
457 repo_dir = fetch_and_extract(target, build_id.url_id, repo_file)
458 if not repo_dir:
459 print_e('Failed to extract Jetifier')
460 return False
461
462 rm(jetifier_dir)
463 mv(repo_dir, jetifier_dir)
Jeff Gaston6afb1e42018-02-07 18:58:07 -0500464 os.chmod(os.path.join(jetifier_dir, 'jetifier-standalone', 'bin', 'jetifier-standalone'), 0o755)
Jeff Gaston782c3e32018-02-06 14:36:17 -0500465 return True
466
Alan Viverette7e897e22018-03-09 15:24:10 -0500467
Alan Viverettec960cfb2017-12-04 13:09:22 -0500468def update_toolkit(target, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500469 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 -0500470 if not repo_dir:
471 print_e('Failed to extract App Toolkit repository')
472 return False
473
474 # Transform the repo archive into a Makefile-compatible format.
475 return transform_maven_repo([repo_dir], os.path.join(extras_dir, 'app-toolkit'))
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400476
477
478def update_constraint(target, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500479 layout_dir = fetch_and_extract(target, build_id.url_id,
480 'com.android.support.constraint-constraint-layout-%s.zip' % build_id.fs_id)
481 solver_dir = fetch_and_extract(target, build_id.url_id,
482 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id.fs_id)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400483 if not layout_dir or not solver_dir:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500484 print_e('Failed to extract Constraint Layout repositories')
Alan Viverettecd3de262017-08-14 09:51:30 -0400485 return False
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400486
487 # Passing False here is an inelegant solution, but it means we can replace
488 # the top-level directory without worrying about other child directories.
Alan Viverettec960cfb2017-12-04 13:09:22 -0500489 return transform_maven_repo([layout_dir, solver_dir],
490 os.path.join(extras_dir, 'constraint-layout'), extract_res=False)
Alan Viverette45837092017-05-12 14:50:53 -0400491
492
Alan Viverette129555e2018-01-30 09:57:57 -0500493def update_design(file):
494 design_dir = extract_artifact(file)
495 if not design_dir:
496 print_e('Failed to extract Design Library repositories')
497 return False
498
499 # Don't bother extracting resources -- this should only be used with AAPT2.
500 return transform_maven_repo([design_dir],
501 os.path.join(extras_dir, 'material-design'), extract_res=False)
502
503
Alan Viverette45837092017-05-12 14:50:53 -0400504def extract_to(zip_file, paths, filename, parent_path):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500505 zip_path = next(filter(lambda path: filename in path, paths))
Alan Viverette45837092017-05-12 14:50:53 -0400506 src_path = zip_file.extract(zip_path)
507 dst_path = path(parent_path, filename)
508 mv(src_path, dst_path)
509
510
Alan Viverettecd3de262017-08-14 09:51:30 -0400511def update_sdk_repo(target, build_id):
Alan Viverette45837092017-05-12 14:50:53 -0400512 platform = 'darwin' if 'mac' in target else 'linux'
Alan Viverettecd3de262017-08-14 09:51:30 -0400513 artifact_path = fetch_artifact(
Jeff Gaston13e38412018-02-06 14:45:36 -0500514 target, build_id.url_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id.fs_id))
Alan Viverette45837092017-05-12 14:50:53 -0400515 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400516 return False
Alan Viverette45837092017-05-12 14:50:53 -0400517
518 with zipfile.ZipFile(artifact_path) as zipFile:
519 paths = zipFile.namelist()
520
521 extract_to(zipFile, paths, 'android.jar', current_path)
522 extract_to(zipFile, paths, 'uiautomator.jar', current_path)
523 extract_to(zipFile, paths, 'framework.aidl', current_path)
Paul Duffind891f3c2017-12-06 11:32:56 +0000524 extract_to(zipFile, paths, 'optional/android.test.base.jar', current_path)
Paul Duffin2b1a1462017-07-14 16:21:33 +0100525 extract_to(zipFile, paths, 'optional/android.test.mock.jar', current_path)
526 extract_to(zipFile, paths, 'optional/android.test.runner.jar', current_path)
Alan Viverette45837092017-05-12 14:50:53 -0400527
528 # Unclear if this is actually necessary.
529 extract_to(zipFile, paths, 'framework.aidl', system_path)
Jiyong Parkb773d292018-01-30 23:33:19 +0900530
Colin Cross4f4b9062018-02-14 10:03:19 -0800531 artifact_path = fetch_artifact(target, build_id.fs_id, 'core.current.stubs.jar')
Jiyong Parkb773d292018-01-30 23:33:19 +0900532 if not artifact_path:
533 return False
534
535 mv(artifact_path, path(current_path, 'core.jar'))
Alan Viverettecd3de262017-08-14 09:51:30 -0400536 return True
Alan Viverette45837092017-05-12 14:50:53 -0400537
538
Alan Viverettecd3de262017-08-14 09:51:30 -0400539def update_system(target, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500540 artifact_path = fetch_artifact(target, build_id.url_id, 'android_system.jar')
Alan Viverette45837092017-05-12 14:50:53 -0400541 if not artifact_path:
Alan Viverettecd3de262017-08-14 09:51:30 -0400542 return False
Alan Viverette45837092017-05-12 14:50:53 -0400543
544 mv(artifact_path, path(system_path, 'android.jar'))
Paul Duffin960e1ee2017-12-21 08:50:14 +0000545
Jeff Gaston13e38412018-02-06 14:45:36 -0500546 artifact_path = fetch_artifact(target, build_id.url_id, 'android.test.mock.stubs_system.jar')
Paul Duffin960e1ee2017-12-21 08:50:14 +0000547 if not artifact_path:
548 return False
549
550 mv(artifact_path, path(system_path, 'optional/android.test.mock.jar'))
551
Alan Viverettecd3de262017-08-14 09:51:30 -0400552 return True
553
554
Alan Viverettec1c32b62017-12-20 09:40:36 -0500555def update_buildtools(target, arch, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500556 artifact_path = fetch_and_extract(target, build_id.url_id,
557 "sdk-repo-%s-build-tools-%s.zip" % (arch, build_id.fs_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500558 if not artifact_path:
559 return False
560
561 top_level_dir = os.listdir(artifact_path)[0]
562 src_path = os.path.join(artifact_path, top_level_dir)
563 dst_path = path(buildtools_dir, arch)
564 mv(src_path, dst_path)
565
566 # Move all top-level files to /bin and make them executable
567 bin_path = path(dst_path, 'bin')
568 top_level_files = filter(lambda e: os.path.isfile(path(dst_path, e)), os.listdir(dst_path))
569 for file in top_level_files:
570 src_file = path(dst_path, file)
571 dst_file = path(bin_path, file)
572 mv(src_file, dst_file)
573 os.chmod(dst_file, 0o755)
574
575 # Remove renderscript
576 rm(path(dst_path, 'renderscript'))
577
578 return True
579
580
Alan Viverettecd3de262017-08-14 09:51:30 -0400581def append(text, more_text):
582 if text:
583 return "%s, %s" % (text, more_text)
584 return more_text
Alan Viverette45837092017-05-12 14:50:53 -0400585
Alan Viverette95f6d362017-04-06 09:40:50 -0400586
Jeff Gaston13e38412018-02-06 14:45:36 -0500587class buildId(object):
588 def __init__(self, url_id, fs_id):
589 # id when used in build server urls
590 self.url_id = url_id
591 # id when used in build commands
592 self.fs_id = fs_id
593
Jeff Gastonb9b09052018-02-05 14:16:05 -0500594def getBuildId(args):
Jeff Gaston13e38412018-02-06 14:45:36 -0500595 # must be in the format 12345 or P12345
Jeff Gastonb9b09052018-02-05 14:16:05 -0500596 source = args.source
Jeff Gaston13e38412018-02-06 14:45:36 -0500597 number_text = source[:]
598 presubmit = False
599 if number_text.startswith("P"):
600 presubmit = True
601 number_text = number_text[1:]
602 if not number_text.isnumeric():
Alan Viverette7e897e22018-03-09 15:24:10 -0500603 return None
Jeff Gaston13e38412018-02-06 14:45:36 -0500604 url_id = source
605 fs_id = url_id
606 if presubmit:
607 fs_id = "0"
608 args.file = False
609 return buildId(url_id, fs_id)
Jeff Gastonb9b09052018-02-05 14:16:05 -0500610
Colin Cross573e1212018-02-12 14:59:52 -0800611def getFile(args):
612 source = args.source
613 if not source.isnumeric():
614 return args.source
Alan Viverette7e897e22018-03-09 15:24:10 -0500615 return None
Colin Cross573e1212018-02-12 14:59:52 -0800616
Alan Viverette4ec9a172018-02-20 16:19:31 -0500617
618def script_relative(rel_path):
Jeff Gastona68a8d42018-03-23 14:00:13 -0400619 return os.path.join(script_dir, rel_path)
Alan Viverette4ec9a172018-02-20 16:19:31 -0500620
621
Jeff Gastoncc296a82018-03-23 14:33:24 -0400622def uncommittedChangesExist():
623 try:
624 # Make sure we don't overwrite any pending changes.
625 diffCommand = "cd " + git_dir + " && git diff --quiet"
626 subprocess.check_call(diffCommand, shell=True)
627 subprocess.check_call(diffCommand + " --cached", shell=True)
628 return False
629 except subprocess.CalledProcessError:
630 return True
631
632
Alan Viveretted4527e62017-05-11 15:03:25 -0400633parser = argparse.ArgumentParser(
Alan Viverette45837092017-05-12 14:50:53 -0400634 description=('Update current prebuilts'))
Alan Viveretted4527e62017-05-11 15:03:25 -0400635parser.add_argument(
Alan Viverette129555e2018-01-30 09:57:57 -0500636 'source',
637 help='Build server build ID or local Maven ZIP file')
638parser.add_argument(
639 '-d', '--design', action="store_true",
640 help='If specified, updates only the Design Library')
Alan Viveretted4527e62017-05-11 15:03:25 -0400641parser.add_argument(
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400642 '-c', '--constraint', action="store_true",
643 help='If specified, updates only Constraint Layout')
644parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400645 '-s', '--support', action="store_true",
646 help='If specified, updates only the Support Library')
Alan Viverette45837092017-05-12 14:50:53 -0400647parser.add_argument(
Jeff Gaston782c3e32018-02-06 14:36:17 -0500648 '-j', '--jetifier', action="store_true",
649 help='If specified, updates only Jetifier')
650parser.add_argument(
Alan Viverettec960cfb2017-12-04 13:09:22 -0500651 '-t', '--toolkit', action="store_true",
652 help='If specified, updates only the App Toolkit')
653parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400654 '-p', '--platform', action="store_true",
655 help='If specified, updates only the Android Platform')
Alan Viverettec1c32b62017-12-20 09:40:36 -0500656parser.add_argument(
657 '-b', '--buildtools', action="store_true",
658 help='If specified, updates only the Build Tools')
Jeff Gastoncc296a82018-03-23 14:33:24 -0400659parser.add_argument(
660 '--commit-first', action="store_true",
661 help='If specified, then if uncommited changes exist, commit before continuing')
Alan Viveretted4527e62017-05-11 15:03:25 -0400662args = parser.parse_args()
Jeff Gaston56fe2762018-02-06 14:54:37 -0500663args.file = True
Alan Viverette129555e2018-01-30 09:57:57 -0500664if not args.source:
665 parser.error("You must specify a build ID or local Maven ZIP file")
Alan Viveretted4527e62017-05-11 15:03:25 -0400666 sys.exit(1)
Alan Viverette129555e2018-01-30 09:57:57 -0500667if not (args.support or args.platform or args.constraint or args.toolkit or args.buildtools \
Jeff Gaston782c3e32018-02-06 14:36:17 -0500668 or args.design or args.jetifier):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500669 parser.error("You must specify at least one target to update")
Alan Viverettecd3de262017-08-14 09:51:30 -0400670 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500671if (args.support or args.constraint or args.toolkit) and which('pom2mk') is None:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500672 parser.error("Cannot find pom2mk in path; please run lunch to set up build environment")
673 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400674
Jeff Gastoncc296a82018-03-23 14:33:24 -0400675if uncommittedChangesExist():
676 if args.commit_first:
677 subprocess.check_call("cd " + git_dir + " && git add -u", shell=True)
678 subprocess.check_call("cd " + git_dir + " && git commit -m 'save working state'", shell=True)
679
680if uncommittedChangesExist():
681 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 -0400682 sys.exit(1)
683
684try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400685 components = None
686 if args.constraint:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500687 if update_constraint('studio', getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400688 components = append(components, 'Constraint Layout')
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500689 print_e('Failed to update Constraint Layout, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400690 else:
691 sys.exit(1)
692 if args.support:
Alan Viverette7e897e22018-03-09 15:24:10 -0500693 if update_support('support_library', getBuildId(args), getFile(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400694 components = append(components, 'Support Library')
695 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500696 print_e('Failed to update Support Library, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400697 sys.exit(1)
Jeff Gaston782c3e32018-02-06 14:36:17 -0500698 if args.jetifier:
699 if update_jetifier('support_library', getBuildId(args)):
700 components = append(components, 'Jetifier')
701 else:
702 print_e('Failed to update Jetifier, aborting...')
703 sys.exit(1)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500704 if args.toolkit:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500705 if update_toolkit('support_library_app_toolkit', getBuildId(args)):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500706 components = append(components, 'App Toolkit')
707 else:
708 print_e('Failed to update App Toolkit, aborting...')
709 sys.exit(1)
Alan Viverettecd3de262017-08-14 09:51:30 -0400710 if args.platform:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500711 if update_sdk_repo('sdk_phone_armv7-sdk_mac', getBuildId(args)) \
712 and update_system('sdk_phone_armv7-sdk_mac', getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400713 components = append(components, 'platform SDK')
714 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500715 print_e('Failed to update platform SDK, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400716 sys.exit(1)
Alan Viverette129555e2018-01-30 09:57:57 -0500717 if args.design:
Colin Cross573e1212018-02-12 14:59:52 -0800718 if update_design(getFile(args)):
Alan Viverette129555e2018-01-30 09:57:57 -0500719 components = append(components, 'Design Library')
720 else:
721 print_e('Failed to update platform SDK, aborting...')
722 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500723 if args.buildtools:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500724 if update_buildtools('sdk_phone_armv7-sdk_mac', 'darwin', getBuildId(args)) \
725 and update_buildtools('sdk_phone_x86_64-sdk', 'linux', getBuildId(args)) \
726 and update_buildtools('sdk_phone_armv7-win_sdk', 'windows', getBuildId(args)):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500727 components = append(components, 'build tools')
728 else:
729 print_e('Failed to update build tools, aborting...')
730 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400731
732 # Commit all changes.
Alan Viverette45837092017-05-12 14:50:53 -0400733 subprocess.check_call(['git', 'add', current_path])
734 subprocess.check_call(['git', 'add', system_path])
Alan Viverettec1c32b62017-12-20 09:40:36 -0500735 subprocess.check_call(['git', 'add', buildtools_dir])
Colin Cross573e1212018-02-12 14:59:52 -0800736 if not args.source.isnumeric():
Alan Viverette129555e2018-01-30 09:57:57 -0500737 src_msg = "local Maven ZIP"
738 else:
Jeff Gaston13e38412018-02-06 14:45:36 -0500739 src_msg = "build %s" % (getBuildId(args).url_id)
Alan Viverette129555e2018-01-30 09:57:57 -0500740 msg = "Import %s from %s\n\n%s" % (components, src_msg, flatten(sys.argv))
Alan Viveretted4527e62017-05-11 15:03:25 -0400741 subprocess.check_call(['git', 'commit', '-m', msg])
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500742 print('Remember to test this change before uploading it to Gerrit!')
Alan Viveretted4527e62017-05-11 15:03:25 -0400743
744finally:
745 # Revert all stray files, including the downloaded zip.
746 try:
747 with open(os.devnull, 'w') as bitbucket:
748 subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
749 subprocess.check_call(
750 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!'], stdout=bitbucket)
751 subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
752 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500753 print_e('ERROR: Failed cleaning up, manual cleanup required!!!')