1#!/usr/bin/env vpython3 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"""Script for building and testing WebRTC AAR.""" 11 12import argparse 13import logging 14import os 15import re 16import shutil 17import subprocess 18import sys 19import tempfile 20 21SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) 22CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) 23 24sys.path.append(os.path.join(CHECKOUT_ROOT, 'tools_webrtc')) 25from android.build_aar import BuildAar 26 27ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'] 28ARTIFACT_ID = 'google-webrtc' 29COMMIT_POSITION_REGEX = r'^Cr-Commit-Position: refs/heads/master@{#(\d+)}$' 30GRADLEW_BIN = os.path.join(CHECKOUT_ROOT, 31 'examples/androidtests/third_party/gradle/gradlew') 32ADB_BIN = os.path.join(CHECKOUT_ROOT, 33 'third_party/android_sdk/public/platform-tools/adb') 34AAR_PROJECT_DIR = os.path.join(CHECKOUT_ROOT, 'examples/aarproject') 35 36 37def _ParseArgs(): 38 parser = argparse.ArgumentParser(description='Releases WebRTC on Bintray.') 39 parser.add_argument('--use-goma', 40 action='store_true', 41 default=False, 42 help='Use goma.') 43 parser.add_argument('--skip-tests', 44 action='store_true', 45 default=False, 46 help='Skips running the tests.') 47 parser.add_argument( 48 '--build-dir', 49 default=None, 50 help='Temporary directory to store the build files. If not specified, ' 51 'a new directory will be created.') 52 parser.add_argument('--verbose', 53 action='store_true', 54 default=False, 55 help='Debug logging.') 56 return parser.parse_args() 57 58 59def _GetCommitHash(): 60 commit_hash = subprocess.check_output( 61 ['git', 'rev-parse', 'HEAD'], cwd=CHECKOUT_ROOT).decode('UTF-8').strip() 62 return commit_hash 63 64 65def _GetCommitPos(): 66 commit_message = subprocess.check_output( 67 ['git', 'rev-list', '--format=%B', '--max-count=1', 'HEAD'], 68 cwd=CHECKOUT_ROOT).decode('UTF-8') 69 commit_pos_match = re.search(COMMIT_POSITION_REGEX, commit_message, 70 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 77def _TestAAR(build_dir): 78 """Runs AppRTCMobile tests using the AAR. Returns true if the tests pass.""" 79 logging.info('Testing library.') 80 81 # Uninstall any existing version of AppRTCMobile. 82 logging.info('Uninstalling previous AppRTCMobile versions. It is okay for ' 83 'these commands to fail if AppRTCMobile is not installed.') 84 subprocess.call([ADB_BIN, 'uninstall', 'org.appspot.apprtc']) 85 subprocess.call([ADB_BIN, 'uninstall', 'org.appspot.apprtc.test']) 86 87 # Run tests. 88 try: 89 # First clean the project. 90 subprocess.check_call([GRADLEW_BIN, 'clean'], cwd=AAR_PROJECT_DIR) 91 # Then run the tests. 92 subprocess.check_call([ 93 GRADLEW_BIN, 'connectedDebugAndroidTest', 94 '-PaarDir=' + os.path.abspath(build_dir) 95 ], 96 cwd=AAR_PROJECT_DIR) 97 except subprocess.CalledProcessError: 98 logging.exception('Test failure.') 99 return False # Clean or tests failed 100 101 return True # Tests pass 102 103 104def BuildAndTestAar(use_goma, skip_tests, build_dir): 105 version = '1.0.' + _GetCommitPos() 106 commit = _GetCommitHash() 107 logging.info('Building and Testing AAR version %s with hash %s', version, 108 commit) 109 110 # If build directory is not specified, create a temporary directory. 111 use_tmp_dir = not build_dir 112 if use_tmp_dir: 113 build_dir = tempfile.mkdtemp() 114 115 try: 116 base_name = ARTIFACT_ID + '-' + version 117 aar_file = os.path.join(build_dir, base_name + '.aar') 118 119 logging.info('Building at %s', build_dir) 120 BuildAar(ARCHS, 121 aar_file, 122 use_goma=use_goma, 123 ext_build_dir=os.path.join(build_dir, 'aar-build')) 124 125 tests_pass = skip_tests or _TestAAR(build_dir) 126 if not tests_pass: 127 raise Exception('Test failure.') 128 129 logging.info('Test success.') 130 131 finally: 132 if use_tmp_dir: 133 shutil.rmtree(build_dir, True) 134 135 136def main(): 137 args = _ParseArgs() 138 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) 139 BuildAndTestAar(args.use_goma, args.skip_tests, args.build_dir) 140 141 142if __name__ == '__main__': 143 sys.exit(main()) 144