blob: 1e91bf99a4090be9fd031fdac52d1d52468c5e2a [file] [log] [blame]
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +02001#!/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
13Set BINTRAY_USER and BINTRAY_API_KEY environment variables before running
14this script for authentication.
15"""
16
17import argparse
Sami Kalliomäki201509b2017-10-09 13:44:25 +020018import json
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +020019import logging
20import os
21import re
22import shutil
23import subprocess
24import sys
25import tempfile
26import time
27
28
29SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
30CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
31
32sys.path.append(os.path.join(CHECKOUT_ROOT, 'third_party'))
33import requests
34import jinja2
35
36sys.path.append(os.path.join(CHECKOUT_ROOT, 'tools_webrtc'))
37from android.build_aar import BuildAar
38
39
40ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64']
Sami Kalliomäki201509b2017-10-09 13:44:25 +020041MAVEN_REPOSITORY = 'https://google.bintray.com/webrtc'
42API = 'https://api.bintray.com'
43PACKAGE_PATH = 'google/webrtc/google-webrtc'
44CONTENT_API = API + '/content/' + PACKAGE_PATH
45PACKAGES_API = API + '/packages/' + PACKAGE_PATH
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +020046GROUP_ID = 'org/webrtc'
47ARTIFACT_ID = 'google-webrtc'
48COMMIT_POSITION_REGEX = r'^Cr-Commit-Position: refs/heads/master@{#(\d+)}$'
Sami Kalliomäki201509b2017-10-09 13:44:25 +020049API_TIMEOUT_SECONDS = 10.0
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +020050UPLOAD_TRIES = 3
51# The sleep time is increased exponentially.
52UPLOAD_RETRY_BASE_SLEEP_SECONDS = 2.0
Sami Kalliomäki201509b2017-10-09 13:44:25 +020053GRADLEW_BIN = os.path.join(CHECKOUT_ROOT,
54 'examples/androidtests/third_party/gradle/gradlew')
Sami Kalliomäki91e26fd2018-01-22 15:08:24 +010055ADB_BIN = os.path.join(CHECKOUT_ROOT,
56 'third_party/android_tools/sdk/platform-tools/adb')
Sami Kalliomäki201509b2017-10-09 13:44:25 +020057AAR_PROJECT_DIR = os.path.join(CHECKOUT_ROOT, 'examples/aarproject')
58AAR_PROJECT_GRADLE = os.path.join(AAR_PROJECT_DIR, 'build.gradle')
59AAR_PROJECT_APP_GRADLE = os.path.join(AAR_PROJECT_DIR, 'app', 'build.gradle')
60AAR_PROJECT_DEPENDENCY = "implementation 'org.webrtc:google-webrtc:1.0.+'"
61AAR_PROJECT_VERSION_DEPENDENCY = "implementation 'org.webrtc:google-webrtc:%s'"
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +020062
63
64def _ParseArgs():
65 parser = argparse.ArgumentParser(description='Releases WebRTC on Bintray.')
66 parser.add_argument('--use-goma', action='store_true', default=False,
67 help='Use goma.')
Sami Kalliomäki201509b2017-10-09 13:44:25 +020068 parser.add_argument('--skip-tests', action='store_true', default=False,
69 help='Skips running the tests.')
70 parser.add_argument('--publish', action='store_true', default=False,
71 help='Automatically publishes the library if the tests pass.')
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +020072 parser.add_argument('--verbose', action='store_true', default=False,
73 help='Debug logging.')
74 return parser.parse_args()
75
76
77def _GetCommitHash():
78 commit_hash = subprocess.check_output(
79 ['git', 'rev-parse', 'HEAD'], cwd=CHECKOUT_ROOT).strip()
80 return commit_hash
81
82
83def _GetCommitPos():
84 commit_message = subprocess.check_output(
85 ['git', 'rev-list', '--format=%B', '--max-count=1', 'HEAD'],
86 cwd=CHECKOUT_ROOT)
87 commit_pos_match = re.search(
88 COMMIT_POSITION_REGEX, commit_message, re.MULTILINE)
89 if not commit_pos_match:
90 raise Exception('Commit position not found in the commit message: %s'
91 % commit_message)
92 return commit_pos_match.group(1)
93
94
95def _UploadFile(user, password, filename, version, target_file):
96# URL is of format:
97 # <repository_api>/<version>/<group_id>/<artifact_id>/<version>/<target_file>
98 # Example:
99 # 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
100
101 target_dir = version + '/' + GROUP_ID + '/' + ARTIFACT_ID + '/' + version
102 target_path = target_dir + '/' + target_file
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200103 url = CONTENT_API + '/' + target_path
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200104
105 logging.info('Uploading %s to %s', filename, url)
106 with open(filename) as fh:
107 file_data = fh.read()
108
109 for attempt in xrange(UPLOAD_TRIES):
110 try:
111 response = requests.put(url, data=file_data, auth=(user, password),
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200112 timeout=API_TIMEOUT_SECONDS)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200113 break
114 except requests.exceptions.Timeout as e:
115 logging.warning('Timeout while uploading: %s', e)
116 time.sleep(UPLOAD_RETRY_BASE_SLEEP_SECONDS ** attempt)
117 else:
118 raise Exception('Failed to upload %s' % filename)
119
120 if not response.ok:
121 raise Exception('Failed to upload %s. Response: %s' % (filename, response))
122 logging.info('Uploaded %s: %s', filename, response)
123
124
125def _GeneratePom(target_file, version, commit):
126 env = jinja2.Environment(
127 loader=jinja2.PackageLoader('release_aar'),
128 )
129 template = env.get_template('pom.jinja')
130 pom = template.render(version=version, commit=commit)
131 with open(target_file, 'w') as fh:
132 fh.write(pom)
133
134
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200135def _TestAAR(tmp_dir, username, password, version):
136 """Runs AppRTCMobile tests using the AAR. Returns true if the tests pass."""
137 logging.info('Testing library.')
138 env = jinja2.Environment(
139 loader=jinja2.PackageLoader('release_aar'),
140 )
141
142 gradle_backup = os.path.join(tmp_dir, 'build.gradle.backup')
143 app_gradle_backup = os.path.join(tmp_dir, 'app-build.gradle.backup')
144
145 # Make backup copies of the project files before modifying them.
146 shutil.copy2(AAR_PROJECT_GRADLE, gradle_backup)
147 shutil.copy2(AAR_PROJECT_APP_GRADLE, app_gradle_backup)
148
149 try:
150 maven_repository_template = env.get_template('maven-repository.jinja')
151 maven_repository = maven_repository_template.render(
152 url=MAVEN_REPOSITORY, username=username, password=password)
153
154 # Append Maven repository to build file to download unpublished files.
155 with open(AAR_PROJECT_GRADLE, 'a') as gradle_file:
156 gradle_file.write(maven_repository)
157
158 # Read app build file.
159 with open(AAR_PROJECT_APP_GRADLE, 'r') as gradle_app_file:
160 gradle_app = gradle_app_file.read()
161
162 if AAR_PROJECT_DEPENDENCY not in gradle_app:
163 raise Exception(
164 '%s not found in the build file.' % AAR_PROJECT_DEPENDENCY)
165 # Set version to the version to be tested.
166 target_dependency = AAR_PROJECT_VERSION_DEPENDENCY % version
167 gradle_app = gradle_app.replace(AAR_PROJECT_DEPENDENCY, target_dependency)
168
169 # Write back.
170 with open(AAR_PROJECT_APP_GRADLE, 'w') as gradle_app_file:
171 gradle_app_file.write(gradle_app)
172
Sami Kalliomäki91e26fd2018-01-22 15:08:24 +0100173 # Uninstall any existing version of AppRTCMobile.
174 logging.info('Uninstalling previous AppRTCMobile versions. It is okay for '
175 'these commands to fail if AppRTCMobile is not installed.')
176 subprocess.call([ADB_BIN, 'uninstall', 'org.appspot.apprtc'])
177 subprocess.call([ADB_BIN, 'uninstall', 'org.appspot.apprtc.test'])
178
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200179 # Run tests.
180 try:
181 # First clean the project.
182 subprocess.check_call([GRADLEW_BIN, 'clean'], cwd=AAR_PROJECT_DIR)
183 # Then run the tests.
184 subprocess.check_call([GRADLEW_BIN, 'connectedDebugAndroidTest'],
185 cwd=AAR_PROJECT_DIR)
186 except subprocess.CalledProcessError:
187 logging.exception('Test failure.')
188 return False # Clean or tests failed
189
190 return True # Tests pass
191 finally:
192 # Restore backups.
193 shutil.copy2(gradle_backup, AAR_PROJECT_GRADLE)
194 shutil.copy2(app_gradle_backup, AAR_PROJECT_APP_GRADLE)
195
196
197def _PublishAAR(user, password, version, additional_args):
198 args = {
199 'publish_wait_for_secs': 0 # Publish asynchronously.
200 }
201 args.update(additional_args)
202
203 url = CONTENT_API + '/' + version + '/publish'
204 response = requests.post(url, data=json.dumps(args), auth=(user, password),
205 timeout=API_TIMEOUT_SECONDS)
206
207 if not response.ok:
208 raise Exception('Failed to publish. Response: %s' % response)
209
210
211def _DeleteUnpublishedVersion(user, password, version):
212 url = PACKAGES_API + '/versions/' + version
213 response = requests.get(url, auth=(user, password),
214 timeout=API_TIMEOUT_SECONDS)
215 if not response.ok:
216 raise Exception('Failed to get version info. Response: %s' % response)
217
218 version_info = json.loads(response.content)
219 if version_info['published']:
220 logging.info('Version has already been published, not deleting.')
221 return
222
223 logging.info('Deleting unpublished version.')
224 response = requests.delete(url, auth=(user, password),
225 timeout=API_TIMEOUT_SECONDS)
226 if not response.ok:
227 raise Exception('Failed to delete version. Response: %s' % response)
228
229
230def ReleaseAar(use_goma, skip_tests, publish):
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200231 version = '1.0.' + _GetCommitPos()
232 commit = _GetCommitHash()
233 logging.info('Releasing AAR version %s with hash %s', version, commit)
234
235 user = os.environ.get('BINTRAY_USER', None)
236 api_key = os.environ.get('BINTRAY_API_KEY', None)
237 if not user or not api_key:
238 raise Exception('Environment variables BINTRAY_USER and BINTRAY_API_KEY '
239 'must be defined.')
240
241 tmp_dir = tempfile.mkdtemp()
242
243 try:
244 base_name = ARTIFACT_ID + '-' + version
245 aar_file = os.path.join(tmp_dir, base_name + '.aar')
246 third_party_licenses_file = os.path.join(tmp_dir, 'LICENSE.md')
247 pom_file = os.path.join(tmp_dir, base_name + '.pom')
248
249 logging.info('Building at %s', tmp_dir)
250 BuildAar(ARCHS, aar_file,
251 use_goma=use_goma,
252 ext_build_dir=os.path.join(tmp_dir, 'aar-build'))
253 _GeneratePom(pom_file, version, commit)
254
255 _UploadFile(user, api_key, aar_file, version, base_name + '.aar')
256 _UploadFile(user, api_key, third_party_licenses_file, version,
257 'THIRD_PARTY_LICENSES.md')
258 _UploadFile(user, api_key, pom_file, version, base_name + '.pom')
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200259
260 tests_pass = skip_tests or _TestAAR(tmp_dir, user, api_key, version)
261 if not tests_pass:
262 logging.info('Discarding library.')
263 _PublishAAR(user, api_key, version, {'discard': True})
264 _DeleteUnpublishedVersion(user, api_key, version)
265 raise Exception('Test failure. Discarded library.')
266
267 if publish:
268 logging.info('Publishing library.')
269 _PublishAAR(user, api_key, version, {})
270 else:
271 logging.info('Note: The library has not not been published automatically.'
272 ' Please do so manually if desired.')
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200273 finally:
274 shutil.rmtree(tmp_dir, True)
275
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200276
277def main():
278 args = _ParseArgs()
279 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200280 ReleaseAar(args.use_goma, args.skip_tests, args.publish)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200281
282
283if __name__ == '__main__':
284 sys.exit(main())