blob: 815e416669383d215d9668a62f102fe4ebee72bb [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
Rico Wind1a29c4f2018-01-25 08:43:08 +010054 version_dir = GetVersionDestination(storage_prefix, version, is_master)
55 return '%s/%s' % (version_dir, file_name)
56
57def GetVersionDestination(storage_prefix, version, is_master):
Rico Wind0c24ae72017-09-08 11:33:56 +020058 archive_dir = 'raw/master' if is_master else 'raw'
Rico Wind1a29c4f2018-01-25 08:43:08 +010059 return '%s%s/%s/%s' % (storage_prefix, ARCHIVE_BUCKET, archive_dir, version)
Rico Wind0c24ae72017-09-08 11:33:56 +020060
61def GetUploadDestination(version, file_name, is_master):
62 return GetStorageDestination('gs://', version, file_name, is_master)
63
64def GetUrl(version, file_name, is_master):
Rico Windb4621c12017-08-28 12:48:53 +020065 return GetStorageDestination('http://storage.googleapis.com/',
Rico Wind0c24ae72017-09-08 11:33:56 +020066 version, file_name, is_master)
Rico Windb4621c12017-08-28 12:48:53 +020067
68def Main():
69 if not 'BUILDBOT_BUILDERNAME' in os.environ:
70 raise Exception('You are not a bot, don\'t archive builds')
Mads Ager0bd1ebd2017-11-22 13:40:21 +010071 # Create maven release first which uses a build that exclude dependencies.
Mads Agera4911eb2017-11-22 13:19:36 +010072 create_maven_release.main(["--out", utils.LIBS])
73
Mads Ager0bd1ebd2017-11-22 13:40:21 +010074 # Generate and copy the build that exclude dependencies.
Mads Agerb10c07f2017-11-27 13:25:52 +010075 gradle.RunGradleExcludeDeps([utils.R8, utils.R8_SRC])
Mads Ager0bd1ebd2017-11-22 13:40:21 +010076 shutil.copyfile(utils.R8_JAR, utils.R8_EXCLUDE_DEPS_JAR)
77
Mads Agerac794132017-11-09 11:38:45 +010078 # Ensure all archived artifacts has been built before archiving.
79 gradle.RunGradle([utils.D8, utils.R8, utils.COMPATDX, utils.COMPATPROGUARD])
Rico Windb4621c12017-08-28 12:48:53 +020080 version = GetVersion()
Mads Agerac794132017-11-09 11:38:45 +010081 is_master = IsMaster(version)
Rico Wind0c24ae72017-09-08 11:33:56 +020082 if is_master:
83 # On master we use the git hash to archive with
84 print 'On master, using git hash for archiving'
85 version = GetGitHash()
86
Rico Wind1a29c4f2018-01-25 08:43:08 +010087 destination = GetVersionDestination('gs://', version, is_master)
88 if utils.cloud_storage_exists(destination):
89 raise Exception('Target archive directory %s already exists' % destination)
Yohann Roussel73f58e12017-10-13 17:33:14 +020090 with utils.TempDir() as temp:
91 version_file = os.path.join(temp, 'r8-version.properties')
92 with open(version_file,'w') as version_writer:
93 version_writer.write('version.sha=' + GetGitHash() + '\n')
Mads Agerac794132017-11-09 11:38:45 +010094 version_writer.write(
95 'releaser=go/r8bot (' + os.environ.get('BUILDBOT_SLAVENAME') + ')\n')
Yohann Roussel73f58e12017-10-13 17:33:14 +020096 version_writer.write('version-file.version.code=1\n')
97
Mads Agerac794132017-11-09 11:38:45 +010098 for file in [utils.D8_JAR,
99 utils.R8_JAR,
Mads Agerb10c07f2017-11-27 13:25:52 +0100100 utils.R8_SRC_JAR,
Mads Ager0bd1ebd2017-11-22 13:40:21 +0100101 utils.R8_EXCLUDE_DEPS_JAR,
Mads Agerac794132017-11-09 11:38:45 +0100102 utils.COMPATDX_JAR,
103 utils.COMPATPROGUARD_JAR,
Mads Ager12a56bc2017-11-27 11:51:25 +0100104 utils.MAVEN_ZIP,
105 utils.GENERATED_LICENSE]:
Mads Agerac794132017-11-09 11:38:45 +0100106 file_name = os.path.basename(file)
Yohann Roussel73f58e12017-10-13 17:33:14 +0200107 tagged_jar = os.path.join(temp, file_name)
Mads Agerac794132017-11-09 11:38:45 +0100108 shutil.copyfile(file, tagged_jar)
Mads Agerb10c07f2017-11-27 13:25:52 +0100109 if file_name.endswith('.jar') and not file_name.endswith('-src.jar'):
Mads Agerafc0cda2017-11-27 13:04:27 +0100110 with zipfile.ZipFile(tagged_jar, 'a') as zip:
111 zip.write(version_file, os.path.basename(version_file))
Yohann Roussel73f58e12017-10-13 17:33:14 +0200112 destination = GetUploadDestination(version, file_name, is_master)
113 print('Uploading %s to %s' % (tagged_jar, destination))
114 utils.upload_file_to_cloud_storage(tagged_jar, destination)
115 print('File available at: %s' % GetUrl(version, file_name, is_master))
Rico Windd0d88cf2018-02-09 09:46:11 +0100116 # Upload extracted maven directory for easy testing in studio.
117 zip_ref = zipfile.ZipFile(utils.MAVEN_ZIP, 'r')
118 zip_ref.extractall(temp)
119 zip_ref.close()
120 utils.upload_dir_to_cloud_storage(
121 os.path.join(temp, 'com'),
122 GetUploadDestination(version, 'com', is_master))
123 print('Maven repo root available at: %s' % GetUrl(version, '', is_master))
Rico Windb4621c12017-08-28 12:48:53 +0200124
125if __name__ == '__main__':
126 sys.exit(Main())