Clean up prebuilts update script

Improves failure and commit messages, bring code style in line with PEP.

Test: ./update_current.py -p -s 4270497
Bug: 64661766
Change-Id: I276a06eaabdd61d4a1d4c0a9a712b2b3fc1b2002
diff --git a/update_current.py b/update_current.py
index 4547330..249ac96 100755
--- a/update_current.py
+++ b/update_current.py
@@ -1,5 +1,8 @@
 #!/usr/bin/python
 
+# This script is used to update platform SDK prebuilts, Support Library, and a variety of other
+# prebuilt libraries used by Android's Makefile builds. For details on how to use this script,
+# visit go/update-prebuilts.
 import os, sys, getopt, zipfile, re
 import argparse
 import subprocess
@@ -11,7 +14,7 @@
 support_dir = os.path.join(current_path, 'support')
 extras_dir = os.path.join(current_path, 'extras')
 
-# See go/fetch_artifact
+# See go/fetch_artifact for details on this script.
 FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
 
 # Does not import support-v4, which is handled as a separate Android.mk (../support-v4) to
@@ -77,6 +80,10 @@
     return reduce((lambda x, y: os.path.join(x, y)), path_parts)
 
 
+def flatten(list):
+    return reduce((lambda x, y: "%s %s" % (x, y)), list)
+
+
 def rm(path):
     if os.path.isdir(path):
         rmtree(path)
@@ -86,7 +93,7 @@
 
 def mv(src_path, dst_path):
     if os.path.exists(dst_path):
-        rmtree(dst_path)
+        rm(dst_path)
     if not os.path.exists(os.path.dirname(dst_path)):
         os.makedirs(os.path.dirname(dst_path))
     os.rename(src_path, dst_path)
@@ -185,13 +192,13 @@
             os.remove(file_path)
 
 
-def fetch_artifact(target, buildId, artifact_path):
+def fetch_artifact(target, build_id, artifact_path):
     print 'Fetching %s from %s...' % (artifact_path, target)
-    fetchCmd = [FETCH_ARTIFACT, '--bid', str(buildId), '--target', target, artifact_path]
+    fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path]
     try:
-        subprocess.check_output(fetchCmd, stderr=subprocess.STDOUT)
+        subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
     except subprocess.CalledProcessError:
-        print >> sys.stderr, 'FAIL: Unable to retrieve %s artifact for build ID %d' % (artifact_path, buildId)
+        print >> sys.stderr, 'FAIL: Unable to retrieve %s artifact for build ID %d' % (artifact_path, build_id)
         return None
     return artifact_path
 
@@ -210,7 +217,8 @@
 
 
 def update_support(target, build_id):
-    repo_dir = fetch_and_extract(target, build_id, 'top-of-tree-m2repository-%s.zip' % build_id)
+    repo_file = 'top-of-tree-m2repository-%s.zip' % build_id
+    repo_dir = fetch_and_extract(target, build_id, repo_file)
     if not repo_dir:
         print >> sys.stderr, 'Failed to extract Support Library repository'
         return
@@ -223,13 +231,13 @@
     layout_dir = fetch_and_extract(target, build_id, 'com.android.support.constraint-constraint-layout-%s.zip' % build_id)
     solver_dir = fetch_and_extract(target, build_id, 'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id)
     if not layout_dir or not solver_dir:
-        print >> sys.stderr, 'Failed to extract Constraint Layout repositories'
-        return
+        return False
 
     # Passing False here is an inelegant solution, but it means we can replace
     # the top-level directory without worrying about other child directories.
     transform_maven_repo(layout_dir, os.path.join(extras_dir, 'constraint-layout'), False)
     transform_maven_repo(solver_dir, os.path.join(extras_dir, 'constraint-layout-solver'), False)
+    return True
 
 
 def extract_to(zip_file, paths, filename, parent_path):
@@ -239,11 +247,12 @@
     mv(src_path, dst_path)
 
 
-def update_sdk_repo(target, buildId):
+def update_sdk_repo(target, build_id):
     platform = 'darwin' if 'mac' in target else 'linux'
-    artifact_path = fetch_artifact(target, buildId, 'sdk-repo-%s-platforms-%s.zip' % (platform, buildId))
+    artifact_path = fetch_artifact(
+        target, build_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id))
     if not artifact_path:
-        return
+        return False
 
     with zipfile.ZipFile(artifact_path) as zipFile:
         paths = zipFile.namelist()
@@ -254,14 +263,22 @@
 
         # Unclear if this is actually necessary.
         extract_to(zipFile, paths, 'framework.aidl', system_path)
+    return True
 
 
-def update_system(target, buildId):
-    artifact_path = fetch_artifact(target, buildId, 'android_system.jar')
+def update_system(target, build_id):
+    artifact_path = fetch_artifact(target, build_id, 'android_system.jar')
     if not artifact_path:
-        return
+        return False
 
     mv(artifact_path, path(system_path, 'android.jar'))
+    return True
+
+
+def append(text, more_text):
+    if text:
+        return "%s, %s" % (text, more_text)
+    return more_text
 
 
 parser = argparse.ArgumentParser(
@@ -283,6 +300,9 @@
 if not args.buildId:
     parser.error("You must specify a build ID")
     sys.exit(1)
+if not (args.support or args.platform or args.constraint):
+    parser.error("You must specify at least one of --constraint, --support, or --platform")
+    sys.exit(1)
 
 try:
     # Make sure we don't overwrite any pending changes.
@@ -293,22 +313,30 @@
     sys.exit(1)
 
 try:
-    has_args = args.support or args.platform or args.constraint
-
-    if has_args and args.constraint:
-        update_constraint('studio', args.buildId)
-    if not has_args or args.support:
-        update_support('support_library', args.buildId)
-    if not has_args or args.platform:
-        update_sdk_repo('sdk_phone_armv7-sdk_mac', args.buildId)
-        update_system('sdk_phone_armv7-sdk_mac', args.buildId)
+    components = None
+    if args.constraint:
+        if update_constraint('studio', args.buildId):
+            components = append(components, 'Constraint Layout')
+        else:
+            sys.exit(1)
+    if args.support:
+        if update_support('support_library', args.buildId):
+            components = append(components, 'Support Library')
+        else:
+            sys.exit(1)
+    if args.platform:
+        if update_sdk_repo('sdk_phone_armv7-sdk_mac', args.buildId) \
+                and update_system('sdk_phone_armv7-sdk_mac', args.buildId):
+            components = append(components, 'platform SDK')
+        else:
+            sys.exit(1)
 
     # Commit all changes.
     subprocess.check_call(['git', 'add', current_path])
     subprocess.check_call(['git', 'add', system_path])
-    msg = "Import support libs from build %s" % args.buildId
+    msg = "Import %s from build %s\n\n%s" % (components, args.buildId, flatten(sys.argv))
     subprocess.check_call(['git', 'commit', '-m', msg])
-    print 'Be sure to upload this manually to gerrit.'
+    print 'Remember to test this change before uploading it to Gerrit!'
 
 finally:
     # Revert all stray files, including the downloaded zip.