blob: 90fd446fcf92dcc893cd3b4948a7beaeb51dc75e [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
Mads Agerac794132017-11-09 11:38:45 +01007import create_maven_release
Rico Windb4621c12017-08-28 12:48:53 +02008import d8
9import os
10import r8
Rico Wind0c24ae72017-09-08 11:33:56 +020011import subprocess
Rico Windb4621c12017-08-28 12:48:53 +020012import sys
13import utils
Yohann Roussel73f58e12017-10-13 17:33:14 +020014import shutil
15import zipfile
Rico Windb4621c12017-08-28 12:48:53 +020016
Rico Wind792e8c72017-08-30 09:43:46 +020017ARCHIVE_BUCKET = 'r8-releases'
Rico Windb4621c12017-08-28 12:48:53 +020018
19def GetVersion():
Yohann Roussel73f58e12017-10-13 17:33:14 +020020 r8_version = r8.run(['--version'], build = False).splitlines()[0].strip()
21 d8_version = d8.run(['--version'], build = False).splitlines()[0].strip()
Rico Windb4621c12017-08-28 12:48:53 +020022 # The version printed is "D8 vVERSION_NUMBER" and "R8 vVERSION_NUMBER"
23 # Sanity check that versions match.
24 if d8_version.split()[1] != r8_version.split()[1]:
25 raise Exception(
26 'Version mismatch: \n%s\n%s' % (d8_version, r8_version))
27 return d8_version.split()[1]
28
Rico Wind0c24ae72017-09-08 11:33:56 +020029def GetGitBranches():
30 return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD'])
Rico Windb4621c12017-08-28 12:48:53 +020031
Rico Wind0c24ae72017-09-08 11:33:56 +020032def GetGitHash():
33 return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
Rico Windb4621c12017-08-28 12:48:53 +020034
Rico Wind0c24ae72017-09-08 11:33:56 +020035def IsMaster(version):
Rico Wind2f720292017-10-06 14:32:12 +020036 branches = subprocess.check_output(['git', 'branch', '-r', '--contains',
Rico Wind0c24ae72017-09-08 11:33:56 +020037 'HEAD'])
38 if not version.endswith('-dev'):
39 # Sanity check, we don't want to archive on top of release builds EVER
40 # Note that even though we branch, we never push the bots to build the same
41 # commit as master on a branch since we always change the version to
42 # not have dev (or we crash here :-)).
43 if 'origin/master' in branches:
44 raise Exception('We are seeing origin/master in a commit that '
45 'don\'t have -dev in version')
Mads Agerac794132017-11-09 11:38:45 +010046 return False
Rico Wind0c24ae72017-09-08 11:33:56 +020047 if not 'origin/master' in branches:
48 raise Exception('We are not seeing origin/master '
49 'in a commit that have -dev in version')
Mads Agerac794132017-11-09 11:38:45 +010050 return True
Rico Wind0c24ae72017-09-08 11:33:56 +020051
52def GetStorageDestination(storage_prefix, version, file_name, is_master):
53 # We archive master commits under raw/master instead of directly under raw
54 archive_dir = 'raw/master' if is_master else 'raw'
55 return '%s%s/%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET, archive_dir,
56 version, file_name)
57
58def GetUploadDestination(version, file_name, is_master):
59 return GetStorageDestination('gs://', version, file_name, is_master)
60
61def GetUrl(version, file_name, is_master):
Rico Windb4621c12017-08-28 12:48:53 +020062 return GetStorageDestination('http://storage.googleapis.com/',
Rico Wind0c24ae72017-09-08 11:33:56 +020063 version, file_name, is_master)
Rico Windb4621c12017-08-28 12:48:53 +020064
65def Main():
66 if not 'BUILDBOT_BUILDERNAME' in os.environ:
67 raise Exception('You are not a bot, don\'t archive builds')
Mads Ager0bd1ebd2017-11-22 13:40:21 +010068 # Create maven release first which uses a build that exclude dependencies.
Mads Agera4911eb2017-11-22 13:19:36 +010069 create_maven_release.main(["--out", utils.LIBS])
70
Mads Ager0bd1ebd2017-11-22 13:40:21 +010071 # Generate and copy the build that exclude dependencies.
72 gradle.RunGradleExcludeDeps([utils.R8])
73 shutil.copyfile(utils.R8_JAR, utils.R8_EXCLUDE_DEPS_JAR)
74
Mads Agerac794132017-11-09 11:38:45 +010075 # Ensure all archived artifacts has been built before archiving.
76 gradle.RunGradle([utils.D8, utils.R8, utils.COMPATDX, utils.COMPATPROGUARD])
Rico Windb4621c12017-08-28 12:48:53 +020077 version = GetVersion()
Mads Agerac794132017-11-09 11:38:45 +010078 is_master = IsMaster(version)
Rico Wind0c24ae72017-09-08 11:33:56 +020079 if is_master:
80 # On master we use the git hash to archive with
81 print 'On master, using git hash for archiving'
82 version = GetGitHash()
83
Yohann Roussel73f58e12017-10-13 17:33:14 +020084 with utils.TempDir() as temp:
85 version_file = os.path.join(temp, 'r8-version.properties')
86 with open(version_file,'w') as version_writer:
87 version_writer.write('version.sha=' + GetGitHash() + '\n')
Mads Agerac794132017-11-09 11:38:45 +010088 version_writer.write(
89 'releaser=go/r8bot (' + os.environ.get('BUILDBOT_SLAVENAME') + ')\n')
Yohann Roussel73f58e12017-10-13 17:33:14 +020090 version_writer.write('version-file.version.code=1\n')
91
Mads Agerac794132017-11-09 11:38:45 +010092 for file in [utils.D8_JAR,
93 utils.R8_JAR,
Mads Ager0bd1ebd2017-11-22 13:40:21 +010094 utils.R8_EXCLUDE_DEPS_JAR,
Mads Agerac794132017-11-09 11:38:45 +010095 utils.COMPATDX_JAR,
96 utils.COMPATPROGUARD_JAR,
Mads Ager12a56bc2017-11-27 11:51:25 +010097 utils.MAVEN_ZIP,
98 utils.GENERATED_LICENSE]:
Mads Agerac794132017-11-09 11:38:45 +010099 file_name = os.path.basename(file)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200100 tagged_jar = os.path.join(temp, file_name)
Mads Agerac794132017-11-09 11:38:45 +0100101 shutil.copyfile(file, tagged_jar)
Mads Agerd25bfde2017-11-27 13:09:55 +0100102 if file_name.endswith('.jar'):
Mads Agerafc0cda2017-11-27 13:04:27 +0100103 with zipfile.ZipFile(tagged_jar, 'a') as zip:
104 zip.write(version_file, os.path.basename(version_file))
Yohann Roussel73f58e12017-10-13 17:33:14 +0200105 destination = GetUploadDestination(version, file_name, is_master)
106 print('Uploading %s to %s' % (tagged_jar, destination))
107 utils.upload_file_to_cloud_storage(tagged_jar, destination)
108 print('File available at: %s' % GetUrl(version, file_name, is_master))
Rico Windb4621c12017-08-28 12:48:53 +0200109
110if __name__ == '__main__':
111 sys.exit(Main())