blob: 4547330050353da19b291601b81dbb10ba6c1c3f [file] [log] [blame]
Alan Viverette95f6d362017-04-06 09:40:50 -04001#!/usr/bin/python
2
3import os, sys, getopt, zipfile, re
Alan Viveretted4527e62017-05-11 15:03:25 -04004import argparse
5import subprocess
Alan Viverette95f6d362017-04-06 09:40:50 -04006from shutil import copyfile, rmtree
Alan Viveretted4527e62017-05-11 15:03:25 -04007from distutils.version import LooseVersion
Alan Viverette95f6d362017-04-06 09:40:50 -04008
Alan Viverette45837092017-05-12 14:50:53 -04009current_path = 'current'
10system_path = 'system_current'
Alan Viverette3e57a4a2017-08-11 15:49:47 -040011support_dir = os.path.join(current_path, 'support')
12extras_dir = os.path.join(current_path, 'extras')
Alan Viverette95f6d362017-04-06 09:40:50 -040013
Alan Viveretted4527e62017-05-11 15:03:25 -040014# See go/fetch_artifact
15FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
16
Alan Viverette95f6d362017-04-06 09:40:50 -040017# Does not import support-v4, which is handled as a separate Android.mk (../support-v4) to
18# statically include dependencies. Similarly, the support-v13 module is imported as
19# support-v13-nodeps and then handled as a separate Android.mk (../support-v13) to statically
20# include dependencies.
21maven_to_make = {
22 'animated-vector-drawable': ['android-support-animatedvectordrawable', 'graphics/drawable'],
Alan Viverette0aaa6252017-05-12 11:33:52 -040023 'appcompat-v7': ['android-support-v7-appcompat-nodeps', 'v7/appcompat'],
Alan Viverette95f6d362017-04-06 09:40:50 -040024 'cardview-v7': ['android-support-v7-cardview', 'v7/cardview'],
25 'customtabs': ['android-support-customtabs', 'customtabs'],
26 'design': ['android-support-design', 'design'],
27 'exifinterface': ['android-support-exifinterface', 'exifinterface'],
28 'gridlayout-v7': ['android-support-v7-gridlayout', 'v7/gridlayout'],
29 'leanback-v17': ['android-support-v17-leanback', 'v17/leanback'],
30 'mediarouter-v7': ['android-support-v7-mediarouter', 'v7/mediarouter'],
31 'multidex': ['android-support-multidex', 'multidex/library'],
32 'multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'],
33 'palette-v7': ['android-support-v7-palette', 'v7/palette'],
34 'percent': ['android-support-percent', 'percent'],
35 'preference-leanback-v17': ['android-support-v17-preference-leanback', 'v17/preference-leanback'],
36 'preference-v14': ['android-support-v14-preference', 'v14/preference'],
37 'preference-v7': ['android-support-v7-preference', 'v7/preference'],
38 'recommendation': ['android-support-recommendation', 'recommendation'],
39 'recyclerview-v7': ['android-support-v7-recyclerview', 'v7/recyclerview'],
40 'support-annotations': ['android-support-annotations', 'annotations'],
41 'support-compat': ['android-support-compat', 'compat'],
42 'support-core-ui': ['android-support-core-ui', 'core-ui'],
43 'support-core-utils': ['android-support-core-utils', 'core-utils'],
44 'support-dynamic-animation': ['android-support-dynamic-animation', 'dynamic-animation'],
45 'support-emoji': ['android-support-emoji', 'emoji'],
46 'support-emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
47 'support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
48 'support-fragment': ['android-support-fragment', 'fragment'],
49 'support-media-compat': ['android-support-media-compat', 'media-compat'],
50 'support-tv-provider': ['android-support-tv-provider', 'tv-provider'],
Alan Viverette0aaa6252017-05-12 11:33:52 -040051 'support-v13': ['android-support-v13-nodeps', 'v13'],
Alan Viverette95f6d362017-04-06 09:40:50 -040052 'support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'],
53 'transition': ['android-support-transition', 'transition'],
Alan Viverette3e57a4a2017-08-11 15:49:47 -040054 'wear': ['android-support-wear', 'wear'],
55 'constraint-layout': ['android-support-constraint-layout', 'constraint-layout'],
56 'constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver']
Alan Viverette95f6d362017-04-06 09:40:50 -040057}
58
59# Always remove these files.
60blacklist_files = [
61 'annotations.zip',
62 'public.txt',
63 'R.txt',
Alan Viverette44ad4e42017-08-09 17:45:00 -040064 'AndroidManifest.xml',
65 'noto-emoji-compat-java.jar'
Alan Viverette95f6d362017-04-06 09:40:50 -040066]
67
Alan Viverette44ad4e42017-08-09 17:45:00 -040068artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
Alan Viverette95f6d362017-04-06 09:40:50 -040069
70
71def touch(fname, times=None):
72 with open(fname, 'a'):
73 os.utime(fname, times)
74
75
Alan Viverette45837092017-05-12 14:50:53 -040076def path(*path_parts):
77 return reduce((lambda x, y: os.path.join(x, y)), path_parts)
78
79
80def rm(path):
81 if os.path.isdir(path):
82 rmtree(path)
83 elif os.path.exists(path):
84 os.remove(path)
85
86
87def mv(src_path, dst_path):
Alan Viverette3e57a4a2017-08-11 15:49:47 -040088 if os.path.exists(dst_path):
89 rmtree(dst_path)
90 if not os.path.exists(os.path.dirname(dst_path)):
91 os.makedirs(os.path.dirname(dst_path))
Alan Viverette45837092017-05-12 14:50:53 -040092 os.rename(src_path, dst_path)
93
94
Alan Viverette3e57a4a2017-08-11 15:49:47 -040095def detect_artifacts(repo_dir):
Alan Viveretted4527e62017-05-11 15:03:25 -040096 maven_lib_info = {}
97
Alan Viverette5bfe05b2017-06-06 14:21:29 -040098 # Find the latest revision for each artifact.
Alan Viverette3e57a4a2017-08-11 15:49:47 -040099 for root, dirs, files in os.walk(repo_dir):
Alan Viverette95f6d362017-04-06 09:40:50 -0400100 for file in files:
101 matcher = artifact_pattern.match(file)
102 if matcher:
103 maven_lib_name = matcher.group(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400104 maven_lib_vers = LooseVersion(matcher.group(2))
Alan Viverette95f6d362017-04-06 09:40:50 -0400105
106 if maven_lib_name in maven_to_make:
Alan Viveretted4527e62017-05-11 15:03:25 -0400107 if maven_lib_name not in maven_lib_info \
108 or maven_lib_vers > maven_lib_info[maven_lib_name][0]:
109 maven_lib_info[maven_lib_name] = [maven_lib_vers, root, file]
Alan Viverette95f6d362017-04-06 09:40:50 -0400110
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400111 return maven_lib_info
112
113
114def transform_maven_repo(repo_dir, update_dir, use_make_dir=True):
115 maven_lib_info = detect_artifacts(repo_dir)
116
117 cwd = os.getcwd()
118
119 # Use a temporary working directory.
120 working_dir = os.path.join(cwd, 'support_tmp')
121 if os.path.exists(working_dir):
122 rmtree(working_dir)
123 os.mkdir(working_dir)
124
Alan Viveretted4527e62017-05-11 15:03:25 -0400125 for info in maven_lib_info.values():
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400126 transform_maven_lib(working_dir, info[1], info[2], use_make_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400127
128 # Replace the old directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400129 output_dir = os.path.join(cwd, update_dir)
130 mv(working_dir, output_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400131
Alan Viveretted4527e62017-05-11 15:03:25 -0400132
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400133def transform_maven_lib(working_dir, root, file, use_make_dir):
Alan Viveretted4527e62017-05-11 15:03:25 -0400134 matcher = artifact_pattern.match(file)
135 maven_lib_name = matcher.group(1)
136 maven_lib_vers = matcher.group(2)
137 maven_lib_type = matcher.group(3)
138
139 make_lib_name = maven_to_make[maven_lib_name][0]
140 make_dir_name = maven_to_make[maven_lib_name][1]
141 artifact_file = os.path.join(root, file)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400142 target_dir = os.path.join(working_dir, make_dir_name) if use_make_dir else working_dir
Alan Viveretted4527e62017-05-11 15:03:25 -0400143 if not os.path.exists(target_dir):
144 os.makedirs(target_dir)
145
146 if maven_lib_type == "aar":
147 process_aar(artifact_file, target_dir, make_lib_name)
148 else:
149 target_file = os.path.join(target_dir, make_lib_name + ".jar")
150 os.rename(artifact_file, target_file)
151
152 print maven_lib_vers, ":", maven_lib_name, "->", make_lib_name
153
Alan Viverette95f6d362017-04-06 09:40:50 -0400154
155def process_aar(artifact_file, target_dir, make_lib_name):
156 # Extract AAR file to target_dir.
157 with zipfile.ZipFile(artifact_file) as zip:
158 zip.extractall(target_dir)
159
160 # Rename classes.jar to match the make artifact
161 classes_jar = os.path.join(target_dir, "classes.jar")
162 if os.path.exists(classes_jar):
163 # If it has resources, it needs a libs dir.
164 res_dir = os.path.join(target_dir, "res")
165 if os.path.exists(res_dir) and os.listdir(res_dir):
166 libs_dir = os.path.join(target_dir, "libs")
167 if not os.path.exists(libs_dir):
168 os.mkdir(libs_dir)
169 else:
170 libs_dir = target_dir
171 target_jar = os.path.join(libs_dir, make_lib_name + ".jar")
172 os.rename(classes_jar, target_jar)
173
174 # Remove or preserve empty dirs.
175 for root, dirs, files in os.walk(target_dir):
176 for dir in dirs:
177 dir_path = os.path.join(root, dir)
178 if not os.listdir(dir_path):
179 os.rmdir(dir_path)
180
181 # Remove top-level cruft.
182 for file in blacklist_files:
183 file_path = os.path.join(target_dir, file)
184 if os.path.exists(file_path):
185 os.remove(file_path)
186
Alan Viverettef17c9402017-07-19 12:57:40 -0400187
Alan Viverette45837092017-05-12 14:50:53 -0400188def fetch_artifact(target, buildId, artifact_path):
189 print 'Fetching %s from %s...' % (artifact_path, target)
190 fetchCmd = [FETCH_ARTIFACT, '--bid', str(buildId), '--target', target, artifact_path]
191 try:
192 subprocess.check_output(fetchCmd, stderr=subprocess.STDOUT)
193 except subprocess.CalledProcessError:
194 print >> sys.stderr, 'FAIL: Unable to retrieve %s artifact for build ID %d' % (artifact_path, buildId)
195 return None
196 return artifact_path
197
198
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400199def fetch_and_extract(target, build_id, file):
200 artifact_path = fetch_artifact(target, build_id, file)
Alan Viverette45837092017-05-12 14:50:53 -0400201 if not artifact_path:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400202 return None
Alan Viverette45837092017-05-12 14:50:53 -0400203
204 # Unzip the repo archive into a separate directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400205 repo_dir = os.path.basename(artifact_path)[:-4]
Alan Viverette45837092017-05-12 14:50:53 -0400206 with zipfile.ZipFile(artifact_path) as zipFile:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400207 zipFile.extractall(repo_dir)
208
209 return repo_dir
210
211
212def update_support(target, build_id):
213 repo_dir = fetch_and_extract(target, build_id, 'top-of-tree-m2repository-%s.zip' % build_id)
214 if not repo_dir:
215 print >> sys.stderr, 'Failed to extract Support Library repository'
216 return
Alan Viverette45837092017-05-12 14:50:53 -0400217
218 # Transform the repo archive into a Makefile-compatible format.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400219 transform_maven_repo(repo_dir, support_dir)
220
221
222def update_constraint(target, build_id):
223 layout_dir = fetch_and_extract(target, build_id, 'com.android.support.constraint-constraint-layout-%s.zip' % build_id)
224 solver_dir = fetch_and_extract(target, build_id, 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id)
225 if not layout_dir or not solver_dir:
226 print >> sys.stderr, 'Failed to extract Constraint Layout repositories'
227 return
228
229 # Passing False here is an inelegant solution, but it means we can replace
230 # the top-level directory without worrying about other child directories.
231 transform_maven_repo(layout_dir, os.path.join(extras_dir, 'constraint-layout'), False)
232 transform_maven_repo(solver_dir, os.path.join(extras_dir, 'constraint-layout-solver'), False)
Alan Viverette45837092017-05-12 14:50:53 -0400233
234
235def extract_to(zip_file, paths, filename, parent_path):
236 zip_path = filter(lambda path: filename in path, paths)[0]
237 src_path = zip_file.extract(zip_path)
238 dst_path = path(parent_path, filename)
239 mv(src_path, dst_path)
240
241
242def update_sdk_repo(target, buildId):
243 platform = 'darwin' if 'mac' in target else 'linux'
244 artifact_path = fetch_artifact(target, buildId, 'sdk-repo-%s-platforms-%s.zip' % (platform, buildId))
245 if not artifact_path:
246 return
247
248 with zipfile.ZipFile(artifact_path) as zipFile:
249 paths = zipFile.namelist()
250
251 extract_to(zipFile, paths, 'android.jar', current_path)
252 extract_to(zipFile, paths, 'uiautomator.jar', current_path)
253 extract_to(zipFile, paths, 'framework.aidl', current_path)
254
255 # Unclear if this is actually necessary.
256 extract_to(zipFile, paths, 'framework.aidl', system_path)
257
258
259def update_system(target, buildId):
260 artifact_path = fetch_artifact(target, buildId, 'android_system.jar')
261 if not artifact_path:
262 return
263
264 mv(artifact_path, path(system_path, 'android.jar'))
265
Alan Viverette95f6d362017-04-06 09:40:50 -0400266
Alan Viveretted4527e62017-05-11 15:03:25 -0400267parser = argparse.ArgumentParser(
Alan Viverette45837092017-05-12 14:50:53 -0400268 description=('Update current prebuilts'))
Alan Viveretted4527e62017-05-11 15:03:25 -0400269parser.add_argument(
270 'buildId',
271 type=int,
Alan Viveretted4527e62017-05-11 15:03:25 -0400272 help='Build server build ID')
273parser.add_argument(
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400274 '-c', '--constraint', action="store_true",
275 help='If specified, updates only Constraint Layout')
276parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400277 '-s', '--support', action="store_true",
278 help='If specified, updates only the Support Library')
Alan Viverette45837092017-05-12 14:50:53 -0400279parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400280 '-p', '--platform', action="store_true",
281 help='If specified, updates only the Android Platform')
Alan Viveretted4527e62017-05-11 15:03:25 -0400282args = parser.parse_args()
283if not args.buildId:
284 parser.error("You must specify a build ID")
285 sys.exit(1)
286
287try:
288 # Make sure we don't overwrite any pending changes.
289 subprocess.check_call(['git', 'diff', '--quiet', '--', '**'])
290 subprocess.check_call(['git', 'diff', '--quiet', '--cached', '--', '**'])
291except subprocess.CalledProcessError:
292 print >> sys.stderr, "FAIL: There are uncommitted changes here; please revert or stash"
293 sys.exit(1)
294
295try:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400296 has_args = args.support or args.platform or args.constraint
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400297
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400298 if has_args and args.constraint:
299 update_constraint('studio', args.buildId)
300 if not has_args or args.support:
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400301 update_support('support_library', args.buildId)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400302 if not has_args or args.platform:
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400303 update_sdk_repo('sdk_phone_armv7-sdk_mac', args.buildId)
304 update_system('sdk_phone_armv7-sdk_mac', args.buildId)
Alan Viveretted4527e62017-05-11 15:03:25 -0400305
306 # Commit all changes.
Alan Viverette45837092017-05-12 14:50:53 -0400307 subprocess.check_call(['git', 'add', current_path])
308 subprocess.check_call(['git', 'add', system_path])
309 msg = "Import support libs from build %s" % args.buildId
Alan Viveretted4527e62017-05-11 15:03:25 -0400310 subprocess.check_call(['git', 'commit', '-m', msg])
311 print 'Be sure to upload this manually to gerrit.'
312
313finally:
314 # Revert all stray files, including the downloaded zip.
315 try:
316 with open(os.devnull, 'w') as bitbucket:
317 subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
318 subprocess.check_call(
319 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!'], stdout=bitbucket)
320 subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
321 except subprocess.CalledProcessError:
Alan Viverette44ad4e42017-08-09 17:45:00 -0400322 print >> sys.stderr, "ERROR: Failed cleaning up, manual cleanup required!!!"