Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 2 | |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 3 | # 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 Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 6 | import os, sys, getopt, zipfile, re |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 7 | import argparse |
| 8 | import subprocess |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 9 | from shutil import copyfile, rmtree, which |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 10 | from distutils.version import LooseVersion |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 11 | from functools import reduce |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 12 | |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 13 | current_path = 'current' |
| 14 | system_path = 'system_current' |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 15 | support_dir = os.path.join(current_path, 'support') |
| 16 | extras_dir = os.path.join(current_path, 'extras') |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 17 | |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 18 | # See go/fetch_artifact for details on this script. |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 19 | FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact' |
| 20 | |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 21 | # Does not import support-v4, which is handled as a separate Android.mk (../support-v4) to |
| 22 | # statically include dependencies. Similarly, the support-v13 module is imported as |
| 23 | # support-v13-nodeps and then handled as a separate Android.mk (../support-v13) to statically |
| 24 | # include dependencies. |
| 25 | maven_to_make = { |
| 26 | 'animated-vector-drawable': ['android-support-animatedvectordrawable', 'graphics/drawable'], |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 27 | 'appcompat-v7': ['android-support-v7-appcompat', 'v7/appcompat'], |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 28 | 'cardview-v7': ['android-support-v7-cardview', 'v7/cardview'], |
| 29 | 'customtabs': ['android-support-customtabs', 'customtabs'], |
| 30 | 'design': ['android-support-design', 'design'], |
| 31 | 'exifinterface': ['android-support-exifinterface', 'exifinterface'], |
| 32 | 'gridlayout-v7': ['android-support-v7-gridlayout', 'v7/gridlayout'], |
| 33 | 'leanback-v17': ['android-support-v17-leanback', 'v17/leanback'], |
| 34 | 'mediarouter-v7': ['android-support-v7-mediarouter', 'v7/mediarouter'], |
| 35 | 'multidex': ['android-support-multidex', 'multidex/library'], |
| 36 | 'multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'], |
| 37 | 'palette-v7': ['android-support-v7-palette', 'v7/palette'], |
| 38 | 'percent': ['android-support-percent', 'percent'], |
| 39 | 'preference-leanback-v17': ['android-support-v17-preference-leanback', 'v17/preference-leanback'], |
| 40 | 'preference-v14': ['android-support-v14-preference', 'v14/preference'], |
| 41 | 'preference-v7': ['android-support-v7-preference', 'v7/preference'], |
| 42 | 'recommendation': ['android-support-recommendation', 'recommendation'], |
| 43 | 'recyclerview-v7': ['android-support-v7-recyclerview', 'v7/recyclerview'], |
| 44 | 'support-annotations': ['android-support-annotations', 'annotations'], |
| 45 | 'support-compat': ['android-support-compat', 'compat'], |
| 46 | 'support-core-ui': ['android-support-core-ui', 'core-ui'], |
| 47 | 'support-core-utils': ['android-support-core-utils', 'core-utils'], |
| 48 | 'support-dynamic-animation': ['android-support-dynamic-animation', 'dynamic-animation'], |
| 49 | 'support-emoji': ['android-support-emoji', 'emoji'], |
| 50 | 'support-emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'], |
| 51 | 'support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'], |
| 52 | 'support-fragment': ['android-support-fragment', 'fragment'], |
| 53 | 'support-media-compat': ['android-support-media-compat', 'media-compat'], |
| 54 | 'support-tv-provider': ['android-support-tv-provider', 'tv-provider'], |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 55 | 'support-v4': ['android-support-v4', 'v4'], |
| 56 | 'support-v13': ['android-support-v13', 'v13'], |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 57 | 'support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'], |
| 58 | 'transition': ['android-support-transition', 'transition'], |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 59 | 'wear': ['android-support-wear', 'wear'], |
| 60 | 'constraint-layout': ['android-support-constraint-layout', 'constraint-layout'], |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 61 | 'constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver'], |
| 62 | 'android.arch.core:runtime': ['android-arch-core-runtime', 'arch-core/runtime'], |
| 63 | 'android.arch.core:common': ['android-arch-core-common', 'arch-core/common'], |
| 64 | 'android.arch.lifecycle:runtime': ['android-arch-lifecycle-runtime', 'arch-lifecycle/runtime'], |
| 65 | 'android.arch.lifecycle:common': ['android-arch-lifecycle-common', 'arch-lifecycle/common'] |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | # Always remove these files. |
| 69 | blacklist_files = [ |
| 70 | 'annotations.zip', |
| 71 | 'public.txt', |
| 72 | 'R.txt', |
Alan Viverette | 44ad4e4 | 2017-08-09 17:45:00 -0400 | [diff] [blame] | 73 | 'AndroidManifest.xml', |
Alan Viverette | f48d9b4 | 2017-08-17 14:45:46 -0400 | [diff] [blame] | 74 | os.path.join('libs','noto-emoji-compat-java.jar') |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 75 | ] |
| 76 | |
Alan Viverette | 44ad4e4 | 2017-08-09 17:45:00 -0400 | [diff] [blame] | 77 | artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$") |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 78 | |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 79 | |
| 80 | class MavenLibraryInfo: |
| 81 | def __init__(self, key, group_id, artifact_id, version, root, repo_dir, file): |
| 82 | self.key = key |
| 83 | self.group_id = group_id |
| 84 | self.artifact_id = artifact_id |
| 85 | self.version = version |
| 86 | self.root = root |
| 87 | self.repo_dir = repo_dir |
| 88 | self.file = file |
| 89 | |
| 90 | |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 91 | def print_e(*args, **kwargs): |
| 92 | print(*args, file=sys.stderr, **kwargs) |
| 93 | |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 94 | |
| 95 | def touch(fname, times=None): |
| 96 | with open(fname, 'a'): |
| 97 | os.utime(fname, times) |
| 98 | |
| 99 | |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 100 | def path(*path_parts): |
| 101 | return reduce((lambda x, y: os.path.join(x, y)), path_parts) |
| 102 | |
| 103 | |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 104 | def flatten(list): |
| 105 | return reduce((lambda x, y: "%s %s" % (x, y)), list) |
| 106 | |
| 107 | |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 108 | def rm(path): |
| 109 | if os.path.isdir(path): |
| 110 | rmtree(path) |
| 111 | elif os.path.exists(path): |
| 112 | os.remove(path) |
| 113 | |
| 114 | |
| 115 | def mv(src_path, dst_path): |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 116 | if os.path.exists(dst_path): |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 117 | rm(dst_path) |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 118 | if not os.path.exists(os.path.dirname(dst_path)): |
| 119 | os.makedirs(os.path.dirname(dst_path)) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 120 | os.rename(src_path, dst_path) |
| 121 | |
| 122 | |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 123 | def detect_artifacts(repo_dirs): |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 124 | maven_lib_info = {} |
| 125 | |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 126 | # Find the latest revision for each artifact, remove others |
| 127 | for repo_dir in repo_dirs: |
| 128 | for root, dirs, files in os.walk(repo_dir): |
| 129 | for file in files: |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 130 | if file[-4:] == ".pom": |
| 131 | # Read the POM (hack hack hack). |
| 132 | group_id = '' |
| 133 | artifact_id = '' |
| 134 | version = '' |
| 135 | file = os.path.join(root, file) |
| 136 | with open(file) as pom_file: |
| 137 | for line in pom_file: |
| 138 | if line[:11] == ' <groupId>': |
| 139 | group_id = line[11:-11] |
| 140 | elif line[:14] == ' <artifactId>': |
| 141 | artifact_id = line[14:-14] |
| 142 | elif line[:11] == ' <version>': |
| 143 | version = line[11:-11] |
| 144 | if group_id == '' or artifact_id == '' or version == '': |
| 145 | print_e('Failed to find Maven artifact data in ' + file) |
| 146 | continue |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 147 | |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 148 | # Locate the artifact. |
| 149 | artifact_file = file[:-4] |
| 150 | if os.path.exists(artifact_file + '.jar'): |
| 151 | artifact_file = artifact_file + '.jar' |
| 152 | elif os.path.exists(artifact_file + '.aar'): |
| 153 | artifact_file = artifact_file + '.aar' |
| 154 | else: |
| 155 | print_e('Failed to find artifact for ' + artifact_file) |
| 156 | continue |
| 157 | |
| 158 | # Make relative to root. |
| 159 | artifact_file = artifact_file[len(root) + 1:] |
| 160 | |
| 161 | # Find the mapping. |
| 162 | group_artifact = group_id + ':' + artifact_id |
| 163 | if artifact_id in maven_to_make: |
| 164 | key = artifact_id |
| 165 | elif group_artifact in maven_to_make: |
| 166 | key = group_artifact |
| 167 | else: |
| 168 | print_e('Failed to find artifact mapping for ' + group_artifact) |
| 169 | continue |
| 170 | |
| 171 | # Store the latest version. |
| 172 | version = LooseVersion(version) |
| 173 | if key not in maven_lib_info \ |
| 174 | or version > maven_lib_info[key].version: |
| 175 | maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version, |
| 176 | root, repo_dir, artifact_file) |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 177 | |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 178 | return maven_lib_info |
| 179 | |
| 180 | |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 181 | def transform_maven_repo(repo_dirs, update_dir, extract_res=True): |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 182 | cwd = os.getcwd() |
| 183 | |
| 184 | # Use a temporary working directory. |
| 185 | working_dir = os.path.join(cwd, 'support_tmp') |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 186 | maven_lib_info = detect_artifacts(repo_dirs) |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 187 | |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 188 | if not maven_lib_info: |
| 189 | print_e('Failed to detect artifacts') |
| 190 | return False |
| 191 | |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 192 | for info in maven_lib_info.values(): |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 193 | transform_maven_lib(working_dir, info, extract_res) |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 194 | |
| 195 | with open(os.path.join(working_dir, 'Android.mk'), 'w') as f: |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 196 | args = ["pom2mk", "-transitive", "-sdk-version", "current"] |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 197 | args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name][0] for name in maven_to_make]) |
| 198 | args.extend(["."]) |
| 199 | subprocess.check_call(args, stdout=f, cwd=working_dir) |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 200 | |
| 201 | # Replace the old directory. |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 202 | output_dir = os.path.join(cwd, update_dir) |
| 203 | mv(working_dir, output_dir) |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 204 | return True |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 205 | |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 206 | |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 207 | def transform_maven_lib(working_dir, artifact_info, extract_res): |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 208 | # Move library into working dir |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 209 | new_dir = os.path.join(working_dir, os.path.relpath(artifact_info.root, artifact_info.repo_dir)) |
| 210 | mv(artifact_info.root, new_dir) |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 211 | |
| 212 | for dirpath, dirs, files in os.walk(new_dir): |
| 213 | for f in files: |
| 214 | if '-sources.jar' in f: |
| 215 | os.remove(os.path.join(dirpath, f)) |
| 216 | |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 217 | matcher = artifact_pattern.match(artifact_info.file) |
| 218 | maven_lib_name = artifact_info.key |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 219 | maven_lib_vers = matcher.group(2) |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 220 | maven_lib_type = artifact_info.file[-3:] |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 221 | |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 222 | make_lib_name = maven_to_make[artifact_info.key][0] |
| 223 | make_dir_name = maven_to_make[artifact_info.key][1] |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 224 | |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 225 | if extract_res: |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 226 | artifact_file = os.path.join(new_dir, artifact_info.file) |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 227 | target_dir = os.path.join(working_dir, make_dir_name) |
| 228 | if not os.path.exists(target_dir): |
| 229 | os.makedirs(target_dir) |
| 230 | |
| 231 | if maven_lib_type == "aar": |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 232 | process_aar(artifact_file, target_dir) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 233 | |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 234 | if maven_lib_type == "aar": |
Dan Willemsen | d677b60 | 2017-11-08 22:13:17 -0800 | [diff] [blame] | 235 | with zipfile.ZipFile(artifact_file) as zip: |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 236 | manifests_dir = os.path.join(working_dir, "manifests") |
| 237 | zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name)) |
Dan Willemsen | d677b60 | 2017-11-08 22:13:17 -0800 | [diff] [blame] | 238 | |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 239 | print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 240 | |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 241 | |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 242 | def process_aar(artifact_file, target_dir): |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 243 | # Extract AAR file to target_dir. |
| 244 | with zipfile.ZipFile(artifact_file) as zip: |
| 245 | zip.extractall(target_dir) |
| 246 | |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 247 | # Remove classes.jar |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 248 | classes_jar = os.path.join(target_dir, "classes.jar") |
| 249 | if os.path.exists(classes_jar): |
Dan Willemsen | 814152e | 2017-11-06 13:22:11 -0800 | [diff] [blame] | 250 | os.remove(classes_jar) |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 251 | |
| 252 | # Remove or preserve empty dirs. |
| 253 | for root, dirs, files in os.walk(target_dir): |
| 254 | for dir in dirs: |
| 255 | dir_path = os.path.join(root, dir) |
| 256 | if not os.listdir(dir_path): |
| 257 | os.rmdir(dir_path) |
| 258 | |
| 259 | # Remove top-level cruft. |
| 260 | for file in blacklist_files: |
| 261 | file_path = os.path.join(target_dir, file) |
| 262 | if os.path.exists(file_path): |
| 263 | os.remove(file_path) |
| 264 | |
Alan Viverette | f17c940 | 2017-07-19 12:57:40 -0400 | [diff] [blame] | 265 | |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 266 | def fetch_artifact(target, build_id, artifact_path): |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 267 | print('Fetching %s from %s...' % (artifact_path, target)) |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 268 | fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path] |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 269 | try: |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 270 | subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 271 | except subprocess.CalledProcessError: |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 272 | print_e('FAIL: Unable to retrieve %s artifact for build ID %d' % (artifact_path, build_id)) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 273 | return None |
| 274 | return artifact_path |
| 275 | |
| 276 | |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 277 | def fetch_and_extract(target, build_id, file): |
| 278 | artifact_path = fetch_artifact(target, build_id, file) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 279 | if not artifact_path: |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 280 | return None |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 281 | |
| 282 | # Unzip the repo archive into a separate directory. |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 283 | repo_dir = os.path.basename(artifact_path)[:-4] |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 284 | with zipfile.ZipFile(artifact_path) as zipFile: |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 285 | zipFile.extractall(repo_dir) |
| 286 | |
| 287 | return repo_dir |
| 288 | |
| 289 | |
| 290 | def update_support(target, build_id): |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 291 | repo_file = 'top-of-tree-m2repository-%s.zip' % build_id |
| 292 | repo_dir = fetch_and_extract(target, build_id, repo_file) |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 293 | if not repo_dir: |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 294 | print_e('Failed to extract Support Library repository') |
Alan Viverette | f48d9b4 | 2017-08-17 14:45:46 -0400 | [diff] [blame] | 295 | return False |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 296 | |
| 297 | # Transform the repo archive into a Makefile-compatible format. |
Alan Viverette | d8ce722 | 2017-12-11 17:24:43 -0500 | [diff] [blame^] | 298 | return transform_maven_repo([repo_dir], support_dir) |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 299 | |
| 300 | |
| 301 | def update_toolkit(target, build_id): |
| 302 | repo_dir = fetch_and_extract(target, build_id, 'top-of-tree-m2repository-%s.zip' % build_id) |
| 303 | if not repo_dir: |
| 304 | print_e('Failed to extract App Toolkit repository') |
| 305 | return False |
| 306 | |
| 307 | # Transform the repo archive into a Makefile-compatible format. |
| 308 | return transform_maven_repo([repo_dir], os.path.join(extras_dir, 'app-toolkit')) |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 309 | |
| 310 | |
| 311 | def update_constraint(target, build_id): |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 312 | layout_dir = fetch_and_extract(target, build_id, |
| 313 | 'com.android.support.constraint-constraint-layout-%s.zip' % build_id) |
| 314 | solver_dir = fetch_and_extract(target, build_id, |
| 315 | 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id) |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 316 | if not layout_dir or not solver_dir: |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 317 | print_e('Failed to extract Constraint Layout repositories') |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 318 | return False |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 319 | |
| 320 | # Passing False here is an inelegant solution, but it means we can replace |
| 321 | # the top-level directory without worrying about other child directories. |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 322 | return transform_maven_repo([layout_dir, solver_dir], |
| 323 | os.path.join(extras_dir, 'constraint-layout'), extract_res=False) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 324 | |
| 325 | |
| 326 | def extract_to(zip_file, paths, filename, parent_path): |
| 327 | zip_path = filter(lambda path: filename in path, paths)[0] |
| 328 | src_path = zip_file.extract(zip_path) |
| 329 | dst_path = path(parent_path, filename) |
| 330 | mv(src_path, dst_path) |
| 331 | |
| 332 | |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 333 | def update_sdk_repo(target, build_id): |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 334 | platform = 'darwin' if 'mac' in target else 'linux' |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 335 | artifact_path = fetch_artifact( |
| 336 | target, build_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id)) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 337 | if not artifact_path: |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 338 | return False |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 339 | |
| 340 | with zipfile.ZipFile(artifact_path) as zipFile: |
| 341 | paths = zipFile.namelist() |
| 342 | |
| 343 | extract_to(zipFile, paths, 'android.jar', current_path) |
| 344 | extract_to(zipFile, paths, 'uiautomator.jar', current_path) |
| 345 | extract_to(zipFile, paths, 'framework.aidl', current_path) |
Paul Duffin | d891f3c | 2017-12-06 11:32:56 +0000 | [diff] [blame] | 346 | extract_to(zipFile, paths, 'optional/android.test.base.jar', current_path) |
Paul Duffin | 2b1a146 | 2017-07-14 16:21:33 +0100 | [diff] [blame] | 347 | extract_to(zipFile, paths, 'optional/android.test.mock.jar', current_path) |
| 348 | extract_to(zipFile, paths, 'optional/android.test.runner.jar', current_path) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 349 | |
| 350 | # Unclear if this is actually necessary. |
| 351 | extract_to(zipFile, paths, 'framework.aidl', system_path) |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 352 | return True |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 353 | |
| 354 | |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 355 | def update_system(target, build_id): |
| 356 | artifact_path = fetch_artifact(target, build_id, 'android_system.jar') |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 357 | if not artifact_path: |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 358 | return False |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 359 | |
| 360 | mv(artifact_path, path(system_path, 'android.jar')) |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 361 | return True |
| 362 | |
| 363 | |
| 364 | def append(text, more_text): |
| 365 | if text: |
| 366 | return "%s, %s" % (text, more_text) |
| 367 | return more_text |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 368 | |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 369 | |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 370 | parser = argparse.ArgumentParser( |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 371 | description=('Update current prebuilts')) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 372 | parser.add_argument( |
| 373 | 'buildId', |
| 374 | type=int, |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 375 | help='Build server build ID') |
| 376 | parser.add_argument( |
Alan Viverette | 3e57a4a | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 377 | '-c', '--constraint', action="store_true", |
| 378 | help='If specified, updates only Constraint Layout') |
| 379 | parser.add_argument( |
Alan Viverette | 5bfe05b | 2017-06-06 14:21:29 -0400 | [diff] [blame] | 380 | '-s', '--support', action="store_true", |
| 381 | help='If specified, updates only the Support Library') |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 382 | parser.add_argument( |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 383 | '-t', '--toolkit', action="store_true", |
| 384 | help='If specified, updates only the App Toolkit') |
| 385 | parser.add_argument( |
Alan Viverette | 5bfe05b | 2017-06-06 14:21:29 -0400 | [diff] [blame] | 386 | '-p', '--platform', action="store_true", |
| 387 | help='If specified, updates only the Android Platform') |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 388 | args = parser.parse_args() |
| 389 | if not args.buildId: |
| 390 | parser.error("You must specify a build ID") |
| 391 | sys.exit(1) |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 392 | if not (args.support or args.platform or args.constraint): |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 393 | parser.error("You must specify at least one of --constraint, --support, --toolkit, or --platform") |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 394 | sys.exit(1) |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 395 | if which('pom2mk') is None: |
| 396 | parser.error("Cannot find pom2mk in path; please run lunch to set up build environment") |
| 397 | sys.exit(1) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 398 | |
| 399 | try: |
| 400 | # Make sure we don't overwrite any pending changes. |
| 401 | subprocess.check_call(['git', 'diff', '--quiet', '--', '**']) |
| 402 | subprocess.check_call(['git', 'diff', '--quiet', '--cached', '--', '**']) |
| 403 | except subprocess.CalledProcessError: |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 404 | print_e('FAIL: There are uncommitted changes here; please revert or stash') |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 405 | sys.exit(1) |
| 406 | |
| 407 | try: |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 408 | components = None |
| 409 | if args.constraint: |
| 410 | if update_constraint('studio', args.buildId): |
| 411 | components = append(components, 'Constraint Layout') |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 412 | print_e('Failed to update Constraint Layout, aborting...') |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 413 | else: |
| 414 | sys.exit(1) |
| 415 | if args.support: |
| 416 | if update_support('support_library', args.buildId): |
| 417 | components = append(components, 'Support Library') |
| 418 | else: |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 419 | print_e('Failed to update Support Library, aborting...') |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 420 | sys.exit(1) |
Alan Viverette | c960cfb | 2017-12-04 13:09:22 -0500 | [diff] [blame] | 421 | if args.toolkit: |
| 422 | if update_toolkit('support_library_app_toolkit', args.buildId): |
| 423 | components = append(components, 'App Toolkit') |
| 424 | else: |
| 425 | print_e('Failed to update App Toolkit, aborting...') |
| 426 | sys.exit(1) |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 427 | if args.platform: |
| 428 | if update_sdk_repo('sdk_phone_armv7-sdk_mac', args.buildId) \ |
| 429 | and update_system('sdk_phone_armv7-sdk_mac', args.buildId): |
| 430 | components = append(components, 'platform SDK') |
| 431 | else: |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 432 | print_e('Failed to update platform SDK, aborting...') |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 433 | sys.exit(1) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 434 | |
| 435 | # Commit all changes. |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 436 | subprocess.check_call(['git', 'add', current_path]) |
| 437 | subprocess.check_call(['git', 'add', system_path]) |
Alan Viverette | cd3de26 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 438 | msg = "Import %s from build %s\n\n%s" % (components, args.buildId, flatten(sys.argv)) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 439 | subprocess.check_call(['git', 'commit', '-m', msg]) |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 440 | print('Remember to test this change before uploading it to Gerrit!') |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 441 | |
| 442 | finally: |
| 443 | # Revert all stray files, including the downloaded zip. |
| 444 | try: |
| 445 | with open(os.devnull, 'w') as bitbucket: |
| 446 | subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket) |
| 447 | subprocess.check_call( |
| 448 | ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!'], stdout=bitbucket) |
| 449 | subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket) |
| 450 | except subprocess.CalledProcessError: |
Alan Viverette | f5cb0e6 | 2017-11-17 15:15:23 -0500 | [diff] [blame] | 451 | print_e('ERROR: Failed cleaning up, manual cleanup required!!!') |