Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Alan Viverette | 13401d3 | 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 | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 9 | from shutil import copyfile, rmtree |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 10 | from distutils.version import LooseVersion |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 11 | |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 12 | current_path = 'current' |
| 13 | system_path = 'system_current' |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 14 | support_dir = os.path.join(current_path, 'support') |
| 15 | extras_dir = os.path.join(current_path, 'extras') |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 16 | |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 17 | # See go/fetch_artifact for details on this script. |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 18 | FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact' |
| 19 | |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 20 | # Does not import support-v4, which is handled as a separate Android.mk (../support-v4) to |
| 21 | # statically include dependencies. Similarly, the support-v13 module is imported as |
| 22 | # support-v13-nodeps and then handled as a separate Android.mk (../support-v13) to statically |
| 23 | # include dependencies. |
| 24 | maven_to_make = { |
| 25 | 'animated-vector-drawable': ['android-support-animatedvectordrawable', 'graphics/drawable'], |
Alan Viverette | 0aaa625 | 2017-05-12 11:33:52 -0400 | [diff] [blame] | 26 | 'appcompat-v7': ['android-support-v7-appcompat-nodeps', 'v7/appcompat'], |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 27 | 'cardview-v7': ['android-support-v7-cardview', 'v7/cardview'], |
| 28 | 'customtabs': ['android-support-customtabs', 'customtabs'], |
| 29 | 'design': ['android-support-design', 'design'], |
| 30 | 'exifinterface': ['android-support-exifinterface', 'exifinterface'], |
| 31 | 'gridlayout-v7': ['android-support-v7-gridlayout', 'v7/gridlayout'], |
| 32 | 'leanback-v17': ['android-support-v17-leanback', 'v17/leanback'], |
| 33 | 'mediarouter-v7': ['android-support-v7-mediarouter', 'v7/mediarouter'], |
| 34 | 'multidex': ['android-support-multidex', 'multidex/library'], |
| 35 | 'multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'], |
| 36 | 'palette-v7': ['android-support-v7-palette', 'v7/palette'], |
| 37 | 'percent': ['android-support-percent', 'percent'], |
| 38 | 'preference-leanback-v17': ['android-support-v17-preference-leanback', 'v17/preference-leanback'], |
| 39 | 'preference-v14': ['android-support-v14-preference', 'v14/preference'], |
| 40 | 'preference-v7': ['android-support-v7-preference', 'v7/preference'], |
| 41 | 'recommendation': ['android-support-recommendation', 'recommendation'], |
| 42 | 'recyclerview-v7': ['android-support-v7-recyclerview', 'v7/recyclerview'], |
| 43 | 'support-annotations': ['android-support-annotations', 'annotations'], |
| 44 | 'support-compat': ['android-support-compat', 'compat'], |
| 45 | 'support-core-ui': ['android-support-core-ui', 'core-ui'], |
| 46 | 'support-core-utils': ['android-support-core-utils', 'core-utils'], |
| 47 | 'support-dynamic-animation': ['android-support-dynamic-animation', 'dynamic-animation'], |
| 48 | 'support-emoji': ['android-support-emoji', 'emoji'], |
| 49 | 'support-emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'], |
| 50 | 'support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'], |
| 51 | 'support-fragment': ['android-support-fragment', 'fragment'], |
| 52 | 'support-media-compat': ['android-support-media-compat', 'media-compat'], |
| 53 | 'support-tv-provider': ['android-support-tv-provider', 'tv-provider'], |
Alan Viverette | 0aaa625 | 2017-05-12 11:33:52 -0400 | [diff] [blame] | 54 | 'support-v13': ['android-support-v13-nodeps', 'v13'], |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 55 | 'support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'], |
| 56 | 'transition': ['android-support-transition', 'transition'], |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 57 | 'wear': ['android-support-wear', 'wear'], |
| 58 | 'constraint-layout': ['android-support-constraint-layout', 'constraint-layout'], |
| 59 | 'constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver'] |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | # Always remove these files. |
| 63 | blacklist_files = [ |
| 64 | 'annotations.zip', |
| 65 | 'public.txt', |
| 66 | 'R.txt', |
Alan Viverette | 44ad4e4 | 2017-08-09 17:45:00 -0400 | [diff] [blame] | 67 | 'AndroidManifest.xml', |
Alan Viverette | 7fdd4ef | 2017-08-17 14:45:46 -0400 | [diff] [blame] | 68 | os.path.join('libs','noto-emoji-compat-java.jar') |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 69 | ] |
| 70 | |
Alan Viverette | 44ad4e4 | 2017-08-09 17:45:00 -0400 | [diff] [blame] | 71 | 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] | 72 | |
| 73 | |
| 74 | def touch(fname, times=None): |
| 75 | with open(fname, 'a'): |
| 76 | os.utime(fname, times) |
| 77 | |
| 78 | |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 79 | def path(*path_parts): |
| 80 | return reduce((lambda x, y: os.path.join(x, y)), path_parts) |
| 81 | |
| 82 | |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 83 | def flatten(list): |
| 84 | return reduce((lambda x, y: "%s %s" % (x, y)), list) |
| 85 | |
| 86 | |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 87 | def rm(path): |
| 88 | if os.path.isdir(path): |
| 89 | rmtree(path) |
| 90 | elif os.path.exists(path): |
| 91 | os.remove(path) |
| 92 | |
| 93 | |
| 94 | def mv(src_path, dst_path): |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 95 | if os.path.exists(dst_path): |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 96 | rm(dst_path) |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 97 | if not os.path.exists(os.path.dirname(dst_path)): |
| 98 | os.makedirs(os.path.dirname(dst_path)) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 99 | os.rename(src_path, dst_path) |
| 100 | |
| 101 | |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 102 | def detect_artifacts(repo_dir): |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 103 | maven_lib_info = {} |
| 104 | |
Alan Viverette | 5bfe05b | 2017-06-06 14:21:29 -0400 | [diff] [blame] | 105 | # Find the latest revision for each artifact. |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 106 | for root, dirs, files in os.walk(repo_dir): |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 107 | for file in files: |
| 108 | matcher = artifact_pattern.match(file) |
| 109 | if matcher: |
| 110 | maven_lib_name = matcher.group(1) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 111 | maven_lib_vers = LooseVersion(matcher.group(2)) |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 112 | |
| 113 | if maven_lib_name in maven_to_make: |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 114 | if maven_lib_name not in maven_lib_info \ |
| 115 | or maven_lib_vers > maven_lib_info[maven_lib_name][0]: |
| 116 | maven_lib_info[maven_lib_name] = [maven_lib_vers, root, file] |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 117 | |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 118 | return maven_lib_info |
| 119 | |
| 120 | |
| 121 | def transform_maven_repo(repo_dir, update_dir, use_make_dir=True): |
| 122 | maven_lib_info = detect_artifacts(repo_dir) |
| 123 | |
| 124 | cwd = os.getcwd() |
| 125 | |
| 126 | # Use a temporary working directory. |
| 127 | working_dir = os.path.join(cwd, 'support_tmp') |
| 128 | if os.path.exists(working_dir): |
| 129 | rmtree(working_dir) |
| 130 | os.mkdir(working_dir) |
| 131 | |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 132 | for info in maven_lib_info.values(): |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 133 | transform_maven_lib(working_dir, info[1], info[2], use_make_dir) |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 134 | |
| 135 | # Replace the old directory. |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 136 | output_dir = os.path.join(cwd, update_dir) |
| 137 | mv(working_dir, output_dir) |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 138 | |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 139 | |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 140 | def transform_maven_lib(working_dir, root, file, use_make_dir): |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 141 | matcher = artifact_pattern.match(file) |
| 142 | maven_lib_name = matcher.group(1) |
| 143 | maven_lib_vers = matcher.group(2) |
| 144 | maven_lib_type = matcher.group(3) |
| 145 | |
| 146 | make_lib_name = maven_to_make[maven_lib_name][0] |
| 147 | make_dir_name = maven_to_make[maven_lib_name][1] |
| 148 | artifact_file = os.path.join(root, file) |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 149 | target_dir = os.path.join(working_dir, make_dir_name) if use_make_dir else working_dir |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 150 | if not os.path.exists(target_dir): |
| 151 | os.makedirs(target_dir) |
| 152 | |
| 153 | if maven_lib_type == "aar": |
| 154 | process_aar(artifact_file, target_dir, make_lib_name) |
| 155 | else: |
| 156 | target_file = os.path.join(target_dir, make_lib_name + ".jar") |
| 157 | os.rename(artifact_file, target_file) |
| 158 | |
| 159 | print maven_lib_vers, ":", maven_lib_name, "->", make_lib_name |
| 160 | |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 161 | |
| 162 | def process_aar(artifact_file, target_dir, make_lib_name): |
| 163 | # Extract AAR file to target_dir. |
| 164 | with zipfile.ZipFile(artifact_file) as zip: |
| 165 | zip.extractall(target_dir) |
| 166 | |
| 167 | # Rename classes.jar to match the make artifact |
| 168 | classes_jar = os.path.join(target_dir, "classes.jar") |
| 169 | if os.path.exists(classes_jar): |
| 170 | # If it has resources, it needs a libs dir. |
| 171 | res_dir = os.path.join(target_dir, "res") |
| 172 | if os.path.exists(res_dir) and os.listdir(res_dir): |
| 173 | libs_dir = os.path.join(target_dir, "libs") |
| 174 | if not os.path.exists(libs_dir): |
| 175 | os.mkdir(libs_dir) |
| 176 | else: |
| 177 | libs_dir = target_dir |
| 178 | target_jar = os.path.join(libs_dir, make_lib_name + ".jar") |
| 179 | os.rename(classes_jar, target_jar) |
| 180 | |
| 181 | # Remove or preserve empty dirs. |
| 182 | for root, dirs, files in os.walk(target_dir): |
| 183 | for dir in dirs: |
| 184 | dir_path = os.path.join(root, dir) |
| 185 | if not os.listdir(dir_path): |
| 186 | os.rmdir(dir_path) |
| 187 | |
| 188 | # Remove top-level cruft. |
| 189 | for file in blacklist_files: |
| 190 | file_path = os.path.join(target_dir, file) |
| 191 | if os.path.exists(file_path): |
| 192 | os.remove(file_path) |
| 193 | |
Alan Viverette | f17c940 | 2017-07-19 12:57:40 -0400 | [diff] [blame] | 194 | |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 195 | def fetch_artifact(target, build_id, artifact_path): |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 196 | print 'Fetching %s from %s...' % (artifact_path, target) |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 197 | fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path] |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 198 | try: |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 199 | subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 200 | except subprocess.CalledProcessError: |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 201 | print >> sys.stderr, '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] | 202 | return None |
| 203 | return artifact_path |
| 204 | |
| 205 | |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 206 | def fetch_and_extract(target, build_id, file): |
| 207 | artifact_path = fetch_artifact(target, build_id, file) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 208 | if not artifact_path: |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 209 | return None |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 210 | |
| 211 | # Unzip the repo archive into a separate directory. |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 212 | repo_dir = os.path.basename(artifact_path)[:-4] |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 213 | with zipfile.ZipFile(artifact_path) as zipFile: |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 214 | zipFile.extractall(repo_dir) |
| 215 | |
| 216 | return repo_dir |
| 217 | |
| 218 | |
| 219 | def update_support(target, build_id): |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 220 | repo_file = 'top-of-tree-m2repository-%s.zip' % build_id |
| 221 | repo_dir = fetch_and_extract(target, build_id, repo_file) |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 222 | if not repo_dir: |
| 223 | print >> sys.stderr, 'Failed to extract Support Library repository' |
Alan Viverette | 7fdd4ef | 2017-08-17 14:45:46 -0400 | [diff] [blame] | 224 | return False |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 225 | |
| 226 | # Transform the repo archive into a Makefile-compatible format. |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 227 | transform_maven_repo(repo_dir, support_dir) |
Alan Viverette | 7fdd4ef | 2017-08-17 14:45:46 -0400 | [diff] [blame] | 228 | return True |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 229 | |
| 230 | |
| 231 | def update_constraint(target, build_id): |
| 232 | layout_dir = fetch_and_extract(target, build_id, 'com.android.support.constraint-constraint-layout-%s.zip' % build_id) |
| 233 | solver_dir = fetch_and_extract(target, build_id, 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id) |
| 234 | if not layout_dir or not solver_dir: |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 235 | return False |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 236 | |
| 237 | # Passing False here is an inelegant solution, but it means we can replace |
| 238 | # the top-level directory without worrying about other child directories. |
| 239 | transform_maven_repo(layout_dir, os.path.join(extras_dir, 'constraint-layout'), False) |
| 240 | transform_maven_repo(solver_dir, os.path.join(extras_dir, 'constraint-layout-solver'), False) |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 241 | return True |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 242 | |
| 243 | |
| 244 | def extract_to(zip_file, paths, filename, parent_path): |
| 245 | zip_path = filter(lambda path: filename in path, paths)[0] |
| 246 | src_path = zip_file.extract(zip_path) |
| 247 | dst_path = path(parent_path, filename) |
| 248 | mv(src_path, dst_path) |
| 249 | |
| 250 | |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 251 | def update_sdk_repo(target, build_id): |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 252 | platform = 'darwin' if 'mac' in target else 'linux' |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 253 | artifact_path = fetch_artifact( |
| 254 | target, build_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id)) |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 255 | if not artifact_path: |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 256 | return False |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 257 | |
| 258 | with zipfile.ZipFile(artifact_path) as zipFile: |
| 259 | paths = zipFile.namelist() |
| 260 | |
| 261 | extract_to(zipFile, paths, 'android.jar', current_path) |
| 262 | extract_to(zipFile, paths, 'uiautomator.jar', current_path) |
| 263 | extract_to(zipFile, paths, 'framework.aidl', current_path) |
| 264 | |
| 265 | # Unclear if this is actually necessary. |
| 266 | extract_to(zipFile, paths, 'framework.aidl', system_path) |
Jiyong Park | b773d29 | 2018-01-30 23:33:19 +0900 | [diff] [blame] | 267 | |
Colin Cross | 4f4b906 | 2018-02-14 10:03:19 -0800 | [diff] [blame^] | 268 | artifact_path = fetch_artifact(target, build_id.fs_id, 'core.current.stubs.jar') |
Jiyong Park | b773d29 | 2018-01-30 23:33:19 +0900 | [diff] [blame] | 269 | if not artifact_path: |
| 270 | return False |
| 271 | |
| 272 | mv(artifact_path, path(current_path, 'core.jar')) |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 273 | return True |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 274 | |
| 275 | |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 276 | def update_system(target, build_id): |
| 277 | artifact_path = fetch_artifact(target, build_id, 'android_system.jar') |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 278 | if not artifact_path: |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 279 | return False |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 280 | |
| 281 | mv(artifact_path, path(system_path, 'android.jar')) |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 282 | return True |
| 283 | |
| 284 | |
| 285 | def append(text, more_text): |
| 286 | if text: |
| 287 | return "%s, %s" % (text, more_text) |
| 288 | return more_text |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 289 | |
Alan Viverette | 95f6d36 | 2017-04-06 09:40:50 -0400 | [diff] [blame] | 290 | |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 291 | parser = argparse.ArgumentParser( |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 292 | description=('Update current prebuilts')) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 293 | parser.add_argument( |
| 294 | 'buildId', |
| 295 | type=int, |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 296 | help='Build server build ID') |
| 297 | parser.add_argument( |
Alan Viverette | 726bdf6 | 2017-08-11 15:49:47 -0400 | [diff] [blame] | 298 | '-c', '--constraint', action="store_true", |
| 299 | help='If specified, updates only Constraint Layout') |
| 300 | parser.add_argument( |
Alan Viverette | 5bfe05b | 2017-06-06 14:21:29 -0400 | [diff] [blame] | 301 | '-s', '--support', action="store_true", |
| 302 | help='If specified, updates only the Support Library') |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 303 | parser.add_argument( |
Alan Viverette | 5bfe05b | 2017-06-06 14:21:29 -0400 | [diff] [blame] | 304 | '-p', '--platform', action="store_true", |
| 305 | help='If specified, updates only the Android Platform') |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 306 | args = parser.parse_args() |
| 307 | if not args.buildId: |
| 308 | parser.error("You must specify a build ID") |
| 309 | sys.exit(1) |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 310 | if not (args.support or args.platform or args.constraint): |
| 311 | parser.error("You must specify at least one of --constraint, --support, or --platform") |
| 312 | sys.exit(1) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 313 | |
| 314 | try: |
| 315 | # Make sure we don't overwrite any pending changes. |
| 316 | subprocess.check_call(['git', 'diff', '--quiet', '--', '**']) |
| 317 | subprocess.check_call(['git', 'diff', '--quiet', '--cached', '--', '**']) |
| 318 | except subprocess.CalledProcessError: |
| 319 | print >> sys.stderr, "FAIL: There are uncommitted changes here; please revert or stash" |
| 320 | sys.exit(1) |
| 321 | |
| 322 | try: |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 323 | components = None |
| 324 | if args.constraint: |
| 325 | if update_constraint('studio', args.buildId): |
| 326 | components = append(components, 'Constraint Layout') |
Alan Viverette | 7fdd4ef | 2017-08-17 14:45:46 -0400 | [diff] [blame] | 327 | print >> sys.stderr, 'Failed to update Constraint Layout, aborting...' |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 328 | else: |
| 329 | sys.exit(1) |
| 330 | if args.support: |
| 331 | if update_support('support_library', args.buildId): |
| 332 | components = append(components, 'Support Library') |
| 333 | else: |
Alan Viverette | 7fdd4ef | 2017-08-17 14:45:46 -0400 | [diff] [blame] | 334 | print >> sys.stderr, 'Failed to update Support Library, aborting...' |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 335 | sys.exit(1) |
| 336 | if args.platform: |
| 337 | if update_sdk_repo('sdk_phone_armv7-sdk_mac', args.buildId) \ |
| 338 | and update_system('sdk_phone_armv7-sdk_mac', args.buildId): |
| 339 | components = append(components, 'platform SDK') |
| 340 | else: |
Alan Viverette | 7fdd4ef | 2017-08-17 14:45:46 -0400 | [diff] [blame] | 341 | print >> sys.stderr, 'Failed to update platform SDK, aborting...' |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 342 | sys.exit(1) |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 343 | |
| 344 | # Commit all changes. |
Alan Viverette | 4583709 | 2017-05-12 14:50:53 -0400 | [diff] [blame] | 345 | subprocess.check_call(['git', 'add', current_path]) |
| 346 | subprocess.check_call(['git', 'add', system_path]) |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 347 | 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] | 348 | subprocess.check_call(['git', 'commit', '-m', msg]) |
Alan Viverette | 13401d3 | 2017-08-14 09:51:30 -0400 | [diff] [blame] | 349 | print 'Remember to test this change before uploading it to Gerrit!' |
Alan Viverette | d4527e6 | 2017-05-11 15:03:25 -0400 | [diff] [blame] | 350 | |
| 351 | finally: |
| 352 | # Revert all stray files, including the downloaded zip. |
| 353 | try: |
| 354 | with open(os.devnull, 'w') as bitbucket: |
| 355 | subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket) |
| 356 | subprocess.check_call( |
| 357 | ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!'], stdout=bitbucket) |
| 358 | subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket) |
| 359 | except subprocess.CalledProcessError: |
Alan Viverette | 44ad4e4 | 2017-08-09 17:45:00 -0400 | [diff] [blame] | 360 | print >> sys.stderr, "ERROR: Failed cleaning up, manual cleanup required!!!" |