Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 1 | #!/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 Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 6 | import gradle |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 7 | import d8 |
| 8 | import os |
| 9 | import r8 |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 10 | import subprocess |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 11 | import sys |
| 12 | import utils |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 13 | import shutil |
| 14 | import zipfile |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 15 | |
Rico Wind | 792e8c7 | 2017-08-30 09:43:46 +0200 | [diff] [blame] | 16 | ARCHIVE_BUCKET = 'r8-releases' |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 17 | |
| 18 | def GetVersion(): |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 19 | r8_version = r8.run(['--version'], build = False).splitlines()[0].strip() |
| 20 | d8_version = d8.run(['--version'], build = False).splitlines()[0].strip() |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 21 | # 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 Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 28 | def GetGitBranches(): |
| 29 | return subprocess.check_output(['git', 'show', '-s', '--pretty=%d', 'HEAD']) |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 30 | |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 31 | def GetGitHash(): |
| 32 | return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip() |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 33 | |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 34 | def IsMaster(version): |
Rico Wind | 2f72029 | 2017-10-06 14:32:12 +0200 | [diff] [blame] | 35 | branches = subprocess.check_output(['git', 'branch', '-r', '--contains', |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 36 | '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 | |
| 51 | def 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 | |
| 57 | def GetUploadDestination(version, file_name, is_master): |
| 58 | return GetStorageDestination('gs://', version, file_name, is_master) |
| 59 | |
| 60 | def GetUrl(version, file_name, is_master): |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 61 | return GetStorageDestination('http://storage.googleapis.com/', |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 62 | version, file_name, is_master) |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 63 | |
| 64 | def 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 Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 68 | is_master = True #IsMaster(version) |
Rico Wind | 0c24ae7 | 2017-09-08 11:33:56 +0200 | [diff] [blame] | 69 | 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 Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 74 | # Ensure all archived artifacts has been built before archiving. |
| 75 | gradle.RunGradle([utils.D8, utils.R8, utils.COMPATDX, utils.COMPATPROGUARD]) |
| 76 | |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 77 | 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 Roussel | 8f26fa9 | 2017-10-23 16:23:06 +0200 | [diff] [blame] | 81 | version_writer.write('releaser=go/r8bot (' + os.environ.get('BUILDBOT_SLAVENAME') + ')\n') |
Yohann Roussel | 73f58e1 | 2017-10-13 17:33:14 +0200 | [diff] [blame] | 82 | 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 Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 94 | |
| 95 | if __name__ == '__main__': |
| 96 | sys.exit(Main()) |