blob: c2ae06516a696233da93d1f6b9e67fe0209db46c [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')
55AAR_PROJECT_DIR = os.path.join(CHECKOUT_ROOT, 'examples/aarproject')
56AAR_PROJECT_GRADLE = os.path.join(AAR_PROJECT_DIR, 'build.gradle')
57AAR_PROJECT_APP_GRADLE = os.path.join(AAR_PROJECT_DIR, 'app', 'build.gradle')
58AAR_PROJECT_DEPENDENCY = "implementation 'org.webrtc:google-webrtc:1.0.+'"
59AAR_PROJECT_VERSION_DEPENDENCY = "implementation 'org.webrtc:google-webrtc:%s'"
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +020060
61
62def _ParseArgs():
63 parser = argparse.ArgumentParser(description='Releases WebRTC on Bintray.')
64 parser.add_argument('--use-goma', action='store_true', default=False,
65 help='Use goma.')
Sami Kalliomäki201509b2017-10-09 13:44:25 +020066 parser.add_argument('--skip-tests', action='store_true', default=False,
67 help='Skips running the tests.')
68 parser.add_argument('--publish', action='store_true', default=False,
69 help='Automatically publishes the library if the tests pass.')
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +020070 parser.add_argument('--verbose', action='store_true', default=False,
71 help='Debug logging.')
72 return parser.parse_args()
73
74
75def _GetCommitHash():
76 commit_hash = subprocess.check_output(
77 ['git', 'rev-parse', 'HEAD'], cwd=CHECKOUT_ROOT).strip()
78 return commit_hash
79
80
81def _GetCommitPos():
82 commit_message = subprocess.check_output(
83 ['git', 'rev-list', '--format=%B', '--max-count=1', 'HEAD'],
84 cwd=CHECKOUT_ROOT)
85 commit_pos_match = re.search(
86 COMMIT_POSITION_REGEX, commit_message, re.MULTILINE)
87 if not commit_pos_match:
88 raise Exception('Commit position not found in the commit message: %s'
89 % commit_message)
90 return commit_pos_match.group(1)
91
92
93def _UploadFile(user, password, filename, version, target_file):
94# URL is of format:
95 # <repository_api>/<version>/<group_id>/<artifact_id>/<version>/<target_file>
96 # Example:
97 # 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
98
99 target_dir = version + '/' + GROUP_ID + '/' + ARTIFACT_ID + '/' + version
100 target_path = target_dir + '/' + target_file
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200101 url = CONTENT_API + '/' + target_path
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200102
103 logging.info('Uploading %s to %s', filename, url)
104 with open(filename) as fh:
105 file_data = fh.read()
106
107 for attempt in xrange(UPLOAD_TRIES):
108 try:
109 response = requests.put(url, data=file_data, auth=(user, password),
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200110 timeout=API_TIMEOUT_SECONDS)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200111 break
112 except requests.exceptions.Timeout as e:
113 logging.warning('Timeout while uploading: %s', e)
114 time.sleep(UPLOAD_RETRY_BASE_SLEEP_SECONDS ** attempt)
115 else:
116 raise Exception('Failed to upload %s' % filename)
117
118 if not response.ok:
119 raise Exception('Failed to upload %s. Response: %s' % (filename, response))
120 logging.info('Uploaded %s: %s', filename, response)
121
122
123def _GeneratePom(target_file, version, commit):
124 env = jinja2.Environment(
125 loader=jinja2.PackageLoader('release_aar'),
126 )
127 template = env.get_template('pom.jinja')
128 pom = template.render(version=version, commit=commit)
129 with open(target_file, 'w') as fh:
130 fh.write(pom)
131
132
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200133def _TestAAR(tmp_dir, username, password, version):
134 """Runs AppRTCMobile tests using the AAR. Returns true if the tests pass."""
135 logging.info('Testing library.')
136 env = jinja2.Environment(
137 loader=jinja2.PackageLoader('release_aar'),
138 )
139
140 gradle_backup = os.path.join(tmp_dir, 'build.gradle.backup')
141 app_gradle_backup = os.path.join(tmp_dir, 'app-build.gradle.backup')
142
143 # Make backup copies of the project files before modifying them.
144 shutil.copy2(AAR_PROJECT_GRADLE, gradle_backup)
145 shutil.copy2(AAR_PROJECT_APP_GRADLE, app_gradle_backup)
146
147 try:
148 maven_repository_template = env.get_template('maven-repository.jinja')
149 maven_repository = maven_repository_template.render(
150 url=MAVEN_REPOSITORY, username=username, password=password)
151
152 # Append Maven repository to build file to download unpublished files.
153 with open(AAR_PROJECT_GRADLE, 'a') as gradle_file:
154 gradle_file.write(maven_repository)
155
156 # Read app build file.
157 with open(AAR_PROJECT_APP_GRADLE, 'r') as gradle_app_file:
158 gradle_app = gradle_app_file.read()
159
160 if AAR_PROJECT_DEPENDENCY not in gradle_app:
161 raise Exception(
162 '%s not found in the build file.' % AAR_PROJECT_DEPENDENCY)
163 # Set version to the version to be tested.
164 target_dependency = AAR_PROJECT_VERSION_DEPENDENCY % version
165 gradle_app = gradle_app.replace(AAR_PROJECT_DEPENDENCY, target_dependency)
166
167 # Write back.
168 with open(AAR_PROJECT_APP_GRADLE, 'w') as gradle_app_file:
169 gradle_app_file.write(gradle_app)
170
171 # Run tests.
172 try:
173 # First clean the project.
174 subprocess.check_call([GRADLEW_BIN, 'clean'], cwd=AAR_PROJECT_DIR)
175 # Then run the tests.
176 subprocess.check_call([GRADLEW_BIN, 'connectedDebugAndroidTest'],
177 cwd=AAR_PROJECT_DIR)
178 except subprocess.CalledProcessError:
179 logging.exception('Test failure.')
180 return False # Clean or tests failed
181
182 return True # Tests pass
183 finally:
184 # Restore backups.
185 shutil.copy2(gradle_backup, AAR_PROJECT_GRADLE)
186 shutil.copy2(app_gradle_backup, AAR_PROJECT_APP_GRADLE)
187
188
189def _PublishAAR(user, password, version, additional_args):
190 args = {
191 'publish_wait_for_secs': 0 # Publish asynchronously.
192 }
193 args.update(additional_args)
194
195 url = CONTENT_API + '/' + version + '/publish'
196 response = requests.post(url, data=json.dumps(args), auth=(user, password),
197 timeout=API_TIMEOUT_SECONDS)
198
199 if not response.ok:
200 raise Exception('Failed to publish. Response: %s' % response)
201
202
203def _DeleteUnpublishedVersion(user, password, version):
204 url = PACKAGES_API + '/versions/' + version
205 response = requests.get(url, auth=(user, password),
206 timeout=API_TIMEOUT_SECONDS)
207 if not response.ok:
208 raise Exception('Failed to get version info. Response: %s' % response)
209
210 version_info = json.loads(response.content)
211 if version_info['published']:
212 logging.info('Version has already been published, not deleting.')
213 return
214
215 logging.info('Deleting unpublished version.')
216 response = requests.delete(url, auth=(user, password),
217 timeout=API_TIMEOUT_SECONDS)
218 if not response.ok:
219 raise Exception('Failed to delete version. Response: %s' % response)
220
221
222def ReleaseAar(use_goma, skip_tests, publish):
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200223 version = '1.0.' + _GetCommitPos()
224 commit = _GetCommitHash()
225 logging.info('Releasing AAR version %s with hash %s', version, commit)
226
227 user = os.environ.get('BINTRAY_USER', None)
228 api_key = os.environ.get('BINTRAY_API_KEY', None)
229 if not user or not api_key:
230 raise Exception('Environment variables BINTRAY_USER and BINTRAY_API_KEY '
231 'must be defined.')
232
233 tmp_dir = tempfile.mkdtemp()
234
235 try:
236 base_name = ARTIFACT_ID + '-' + version
237 aar_file = os.path.join(tmp_dir, base_name + '.aar')
238 third_party_licenses_file = os.path.join(tmp_dir, 'LICENSE.md')
239 pom_file = os.path.join(tmp_dir, base_name + '.pom')
240
241 logging.info('Building at %s', tmp_dir)
242 BuildAar(ARCHS, aar_file,
243 use_goma=use_goma,
244 ext_build_dir=os.path.join(tmp_dir, 'aar-build'))
245 _GeneratePom(pom_file, version, commit)
246
247 _UploadFile(user, api_key, aar_file, version, base_name + '.aar')
248 _UploadFile(user, api_key, third_party_licenses_file, version,
249 'THIRD_PARTY_LICENSES.md')
250 _UploadFile(user, api_key, pom_file, version, base_name + '.pom')
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200251
252 tests_pass = skip_tests or _TestAAR(tmp_dir, user, api_key, version)
253 if not tests_pass:
254 logging.info('Discarding library.')
255 _PublishAAR(user, api_key, version, {'discard': True})
256 _DeleteUnpublishedVersion(user, api_key, version)
257 raise Exception('Test failure. Discarded library.')
258
259 if publish:
260 logging.info('Publishing library.')
261 _PublishAAR(user, api_key, version, {})
262 else:
263 logging.info('Note: The library has not not been published automatically.'
264 ' Please do so manually if desired.')
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200265 finally:
266 shutil.rmtree(tmp_dir, True)
267
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200268
269def main():
270 args = _ParseArgs()
271 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
Sami Kalliomäki201509b2017-10-09 13:44:25 +0200272 ReleaseAar(args.use_goma, args.skip_tests, args.publish)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200273
274
275if __name__ == '__main__':
276 sys.exit(main())