Enable us to archive on the master branch

On master we will archive to:
raw/master/GIT_HASH/{d8,r8}.jar
instead of
raw/version/{d8,r8}.jar

Added some sanity checking to make sure we don't mess this up. In case
of anything ever going wrong I have made our archive bucket store old
version (on overwrite/delete, see
https://cloud.google.com/storage/docs/object-versioning)

R=ager@google.com, yroussel@google.com

Bug:
Change-Id: I1f6dc4b2f2d4c17cd8e3a84840ba6808f7054385
diff --git a/tools/archive.py b/tools/archive.py
index ee1053d..3d4523b 100755
--- a/tools/archive.py
+++ b/tools/archive.py
@@ -6,6 +6,7 @@
 import d8
 import os
 import r8
+import subprocess
 import sys
 import utils
 
@@ -21,27 +22,58 @@
         'Version mismatch: \n%s\n%s' % (d8_version, r8_version))
   return d8_version.split()[1]
 
-def GetStorageDestination(storage_prefix, version, file_name):
-  return '%s%s/raw/%s/%s' % (storage_prefix, ARCHIVE_BUCKET, version, file_name)
+def GetGitBranches():
+  return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD'])
 
-def GetUploadDestination(version, file_name):
-  return GetStorageDestination('gs://', version, file_name)
+def GetGitHash():
+  return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
 
-def GetUrl(version, file_name):
+def IsMaster(version):
+  branches = subprocess.check_output(['git', 'show', '-s', '--pretty=%d',
+                                      'HEAD'])
+  if not version.endswith('-dev'):
+    # Sanity check, we don't want to archive on top of release builds EVER
+    # Note that even though we branch, we never push the bots to build the same
+    # commit as master on a branch since we always change the version to
+    # not have dev (or we crash here :-)).
+    if 'origin/master' in branches:
+      raise Exception('We are seeing origin/master in a commit that '
+                      'don\'t have -dev in version')
+    return False;
+  if not 'origin/master' in branches:
+      raise Exception('We are not seeing origin/master '
+                      'in a commit that have -dev in version')
+  return True;
+
+def GetStorageDestination(storage_prefix, version, file_name, is_master):
+  # We archive master commits under raw/master instead of directly under raw
+  archive_dir = 'raw/master' if is_master else 'raw'
+  return '%s%s/%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET, archive_dir,
+                            version, file_name)
+
+def GetUploadDestination(version, file_name, is_master):
+  return GetStorageDestination('gs://', version, file_name, is_master)
+
+def GetUrl(version, file_name, is_master):
   return GetStorageDestination('http://storage.googleapis.com/',
-                               version,
-                               file_name)
+                               version, file_name, is_master)
 
 def Main():
   if not 'BUILDBOT_BUILDERNAME' in os.environ:
     raise Exception('You are not a bot, don\'t archive builds')
   version = GetVersion()
+  is_master = IsMaster(version)
+  if is_master:
+    # On master we use the git hash to archive with
+    print 'On master, using git hash for archiving'
+    version = GetGitHash()
+
   for jar in [utils.D8_JAR, utils.R8_JAR]:
     file_name = os.path.basename(jar)
-    destination = GetUploadDestination(version, file_name)
+    destination = GetUploadDestination(version, file_name, is_master)
     print('Uploading %s to %s' % (jar, destination))
     utils.upload_file_to_cloud_storage(jar, destination)
-    print('File available at: %s' % GetUrl(version, file_name))
+    print('File available at: %s' % GetUrl(version, file_name, is_master))
 
 if __name__ == '__main__':
   sys.exit(Main())