blob: b88eb8d32989044ea9517d886245e58f34fb9a6a [file] [log] [blame]
Rico Windb4621c12017-08-28 12:48:53 +02001#!/usr/bin/env python
2# Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
Søren Gjessedc9d8a22017-10-12 12:40:59 +02006import gradle
Rico Windb4621c12017-08-28 12:48:53 +02007import d8
8import os
9import r8
Rico Wind0c24ae72017-09-08 11:33:56 +020010import subprocess
Rico Windb4621c12017-08-28 12:48:53 +020011import sys
12import utils
Yohann Roussel73f58e12017-10-13 17:33:14 +020013import shutil
14import zipfile
Rico Windb4621c12017-08-28 12:48:53 +020015
Rico Wind792e8c72017-08-30 09:43:46 +020016ARCHIVE_BUCKET = 'r8-releases'
Rico Windb4621c12017-08-28 12:48:53 +020017
18def GetVersion():
Yohann Roussel73f58e12017-10-13 17:33:14 +020019 r8_version = r8.run(['--version'], build = False).splitlines()[0].strip()
20 d8_version = d8.run(['--version'], build = False).splitlines()[0].strip()
Rico Windb4621c12017-08-28 12:48:53 +020021 # The version printed is "D8 vVERSION_NUMBER" and "R8 vVERSION_NUMBER"
22 # Sanity check that versions match.
23 if d8_version.split()[1] != r8_version.split()[1]:
24 raise Exception(
25 'Version mismatch: \n%s\n%s' % (d8_version, r8_version))
26 return d8_version.split()[1]
27
Rico Wind0c24ae72017-09-08 11:33:56 +020028def GetGitBranches():
29 return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD'])
Rico Windb4621c12017-08-28 12:48:53 +020030
Rico Wind0c24ae72017-09-08 11:33:56 +020031def GetGitHash():
32 return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
Rico Windb4621c12017-08-28 12:48:53 +020033
Rico Wind0c24ae72017-09-08 11:33:56 +020034def IsMaster(version):
Rico Wind2f720292017-10-06 14:32:12 +020035 branches = subprocess.check_output(['git', 'branch', '-r', '--contains',
Rico Wind0c24ae72017-09-08 11:33:56 +020036 'HEAD'])
37 if not version.endswith('-dev'):
38 # Sanity check, we don't want to archive on top of release builds EVER
39 # Note that even though we branch, we never push the bots to build the same
40 # commit as master on a branch since we always change the version to
41 # not have dev (or we crash here :-)).
42 if 'origin/master' in branches:
43 raise Exception('We are seeing origin/master in a commit that '
44 'don\'t have -dev in version')
45 return False;
46 if not 'origin/master' in branches:
47 raise Exception('We are not seeing origin/master '
48 'in a commit that have -dev in version')
49 return True;
50
51def GetStorageDestination(storage_prefix, version, file_name, is_master):
52 # We archive master commits under raw/master instead of directly under raw
53 archive_dir = 'raw/master' if is_master else 'raw'
54 return '%s%s/%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET, archive_dir,
55 version, file_name)
56
57def GetUploadDestination(version, file_name, is_master):
58 return GetStorageDestination('gs://', version, file_name, is_master)
59
60def GetUrl(version, file_name, is_master):
Rico Windb4621c12017-08-28 12:48:53 +020061 return GetStorageDestination('http://storage.googleapis.com/',
Rico Wind0c24ae72017-09-08 11:33:56 +020062 version, file_name, is_master)
Rico Windb4621c12017-08-28 12:48:53 +020063
64def Main():
65 if not 'BUILDBOT_BUILDERNAME' in os.environ:
66 raise Exception('You are not a bot, don\'t archive builds')
67 version = GetVersion()
Yohann Roussel73f58e12017-10-13 17:33:14 +020068 is_master = True #IsMaster(version)
Rico Wind0c24ae72017-09-08 11:33:56 +020069 if is_master:
70 # On master we use the git hash to archive with
71 print 'On master, using git hash for archiving'
72 version = GetGitHash()
73
Søren Gjessedc9d8a22017-10-12 12:40:59 +020074 # Ensure all archived artifacts has been built before archiving.
75 gradle.RunGradle([utils.D8, utils.R8, utils.COMPATDX, utils.COMPATPROGUARD])
76
Yohann Roussel73f58e12017-10-13 17:33:14 +020077 with utils.TempDir() as temp:
78 version_file = os.path.join(temp, 'r8-version.properties')
79 with open(version_file,'w') as version_writer:
80 version_writer.write('version.sha=' + GetGitHash() + '\n')
Yohann Roussel8f26fa92017-10-23 16:23:06 +020081 version_writer.write('releaser=go/r8bot (' + os.environ.get('BUILDBOT_SLAVENAME') + ')\n')
Yohann Roussel73f58e12017-10-13 17:33:14 +020082 version_writer.write('version-file.version.code=1\n')
83
84 for jar in [utils.D8_JAR, utils.R8_JAR, utils.COMPATDX_JAR, utils.COMPATPROGUARD_JAR]:
85 file_name = os.path.basename(jar)
86 tagged_jar = os.path.join(temp, file_name)
87 shutil.copyfile(jar, tagged_jar)
88 with zipfile.ZipFile(tagged_jar, 'a') as zip:
89 zip.write(version_file, os.path.basename(version_file))
90 destination = GetUploadDestination(version, file_name, is_master)
91 print('Uploading %s to %s' % (tagged_jar, destination))
92 utils.upload_file_to_cloud_storage(tagged_jar, destination)
93 print('File available at: %s' % GetUrl(version, file_name, is_master))
Rico Windb4621c12017-08-28 12:48:53 +020094
95if __name__ == '__main__':
96 sys.exit(Main())