Sami Kalliomäki | dbb15a7 | 2017-10-05 16:15:02 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
| 10 | |
| 11 | """Script for publishing WebRTC AAR on Bintray. |
| 12 | |
| 13 | Set BINTRAY_USER and BINTRAY_API_KEY environment variables before running |
| 14 | this script for authentication. |
| 15 | """ |
| 16 | |
| 17 | import argparse |
| 18 | import logging |
| 19 | import os |
| 20 | import re |
| 21 | import shutil |
| 22 | import subprocess |
| 23 | import sys |
| 24 | import tempfile |
| 25 | import time |
| 26 | |
| 27 | |
| 28 | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 29 | CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
| 30 | |
| 31 | sys.path.append(os.path.join(CHECKOUT_ROOT, 'third_party')) |
| 32 | import requests |
| 33 | import jinja2 |
| 34 | |
| 35 | sys.path.append(os.path.join(CHECKOUT_ROOT, 'tools_webrtc')) |
| 36 | from android.build_aar import BuildAar |
| 37 | |
| 38 | |
| 39 | ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'] |
| 40 | REPOSITORY_API = 'https://api.bintray.com/content/google/webrtc/google-webrtc' |
| 41 | GROUP_ID = 'org/webrtc' |
| 42 | ARTIFACT_ID = 'google-webrtc' |
| 43 | COMMIT_POSITION_REGEX = r'^Cr-Commit-Position: refs/heads/master@{#(\d+)}$' |
| 44 | UPLOAD_TIMEOUT_SECONDS = 10.0 |
| 45 | UPLOAD_TRIES = 3 |
| 46 | # The sleep time is increased exponentially. |
| 47 | UPLOAD_RETRY_BASE_SLEEP_SECONDS = 2.0 |
| 48 | |
| 49 | |
| 50 | def _ParseArgs(): |
| 51 | parser = argparse.ArgumentParser(description='Releases WebRTC on Bintray.') |
| 52 | parser.add_argument('--use-goma', action='store_true', default=False, |
| 53 | help='Use goma.') |
| 54 | parser.add_argument('--verbose', action='store_true', default=False, |
| 55 | help='Debug logging.') |
| 56 | return parser.parse_args() |
| 57 | |
| 58 | |
| 59 | def _GetCommitHash(): |
| 60 | commit_hash = subprocess.check_output( |
| 61 | ['git', 'rev-parse', 'HEAD'], cwd=CHECKOUT_ROOT).strip() |
| 62 | return commit_hash |
| 63 | |
| 64 | |
| 65 | def _GetCommitPos(): |
| 66 | commit_message = subprocess.check_output( |
| 67 | ['git', 'rev-list', '--format=%B', '--max-count=1', 'HEAD'], |
| 68 | cwd=CHECKOUT_ROOT) |
| 69 | commit_pos_match = re.search( |
| 70 | COMMIT_POSITION_REGEX, commit_message, re.MULTILINE) |
| 71 | if not commit_pos_match: |
| 72 | raise Exception('Commit position not found in the commit message: %s' |
| 73 | % commit_message) |
| 74 | return commit_pos_match.group(1) |
| 75 | |
| 76 | |
| 77 | def _UploadFile(user, password, filename, version, target_file): |
| 78 | # URL is of format: |
| 79 | # <repository_api>/<version>/<group_id>/<artifact_id>/<version>/<target_file> |
| 80 | # Example: |
| 81 | # https://api.bintray.com/content/google/webrtc/google-webrtc/1.0.19742/org/webrtc/google-webrtc/1.0.19742/google-webrtc-1.0.19742.aar |
| 82 | |
| 83 | target_dir = version + '/' + GROUP_ID + '/' + ARTIFACT_ID + '/' + version |
| 84 | target_path = target_dir + '/' + target_file |
| 85 | url = REPOSITORY_API + '/' + target_path |
| 86 | |
| 87 | logging.info('Uploading %s to %s', filename, url) |
| 88 | with open(filename) as fh: |
| 89 | file_data = fh.read() |
| 90 | |
| 91 | for attempt in xrange(UPLOAD_TRIES): |
| 92 | try: |
| 93 | response = requests.put(url, data=file_data, auth=(user, password), |
| 94 | timeout=UPLOAD_TIMEOUT_SECONDS) |
| 95 | break |
| 96 | except requests.exceptions.Timeout as e: |
| 97 | logging.warning('Timeout while uploading: %s', e) |
| 98 | time.sleep(UPLOAD_RETRY_BASE_SLEEP_SECONDS ** attempt) |
| 99 | else: |
| 100 | raise Exception('Failed to upload %s' % filename) |
| 101 | |
| 102 | if not response.ok: |
| 103 | raise Exception('Failed to upload %s. Response: %s' % (filename, response)) |
| 104 | logging.info('Uploaded %s: %s', filename, response) |
| 105 | |
| 106 | |
| 107 | def _GeneratePom(target_file, version, commit): |
| 108 | env = jinja2.Environment( |
| 109 | loader=jinja2.PackageLoader('release_aar'), |
| 110 | ) |
| 111 | template = env.get_template('pom.jinja') |
| 112 | pom = template.render(version=version, commit=commit) |
| 113 | with open(target_file, 'w') as fh: |
| 114 | fh.write(pom) |
| 115 | |
| 116 | |
| 117 | def ReleaseAar(use_goma): |
| 118 | version = '1.0.' + _GetCommitPos() |
| 119 | commit = _GetCommitHash() |
| 120 | logging.info('Releasing AAR version %s with hash %s', version, commit) |
| 121 | |
| 122 | user = os.environ.get('BINTRAY_USER', None) |
| 123 | api_key = os.environ.get('BINTRAY_API_KEY', None) |
| 124 | if not user or not api_key: |
| 125 | raise Exception('Environment variables BINTRAY_USER and BINTRAY_API_KEY ' |
| 126 | 'must be defined.') |
| 127 | |
| 128 | tmp_dir = tempfile.mkdtemp() |
| 129 | |
| 130 | try: |
| 131 | base_name = ARTIFACT_ID + '-' + version |
| 132 | aar_file = os.path.join(tmp_dir, base_name + '.aar') |
| 133 | third_party_licenses_file = os.path.join(tmp_dir, 'LICENSE.md') |
| 134 | pom_file = os.path.join(tmp_dir, base_name + '.pom') |
| 135 | |
| 136 | logging.info('Building at %s', tmp_dir) |
| 137 | BuildAar(ARCHS, aar_file, |
| 138 | use_goma=use_goma, |
| 139 | ext_build_dir=os.path.join(tmp_dir, 'aar-build')) |
| 140 | _GeneratePom(pom_file, version, commit) |
| 141 | |
| 142 | _UploadFile(user, api_key, aar_file, version, base_name + '.aar') |
| 143 | _UploadFile(user, api_key, third_party_licenses_file, version, |
| 144 | 'THIRD_PARTY_LICENSES.md') |
| 145 | _UploadFile(user, api_key, pom_file, version, base_name + '.pom') |
| 146 | finally: |
| 147 | shutil.rmtree(tmp_dir, True) |
| 148 | |
| 149 | logging.info('Library successfully uploaded. Please test and publish it on ' |
| 150 | 'Bintray.') |
| 151 | |
| 152 | |
| 153 | def main(): |
| 154 | args = _ParseArgs() |
| 155 | logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) |
| 156 | ReleaseAar(args.use_goma) |
| 157 | |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | sys.exit(main()) |