Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 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. |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 10 | """Script for building and testing WebRTC AAR.""" |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 11 | |
| 12 | import argparse |
| 13 | import logging |
| 14 | import os |
| 15 | import re |
| 16 | import shutil |
| 17 | import subprocess |
| 18 | import sys |
| 19 | import tempfile |
| 20 | |
| 21 | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 22 | CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
| 23 | |
| 24 | sys.path.append(os.path.join(CHECKOUT_ROOT, 'tools_webrtc')) |
| 25 | from android.build_aar import BuildAar |
| 26 | |
| 27 | ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'] |
| 28 | ARTIFACT_ID = 'google-webrtc' |
| 29 | COMMIT_POSITION_REGEX = r'^Cr-Commit-Position: refs/heads/master@{#(\d+)}$' |
| 30 | GRADLEW_BIN = os.path.join(CHECKOUT_ROOT, |
| 31 | 'examples/androidtests/third_party/gradle/gradlew') |
| 32 | ADB_BIN = os.path.join(CHECKOUT_ROOT, |
| 33 | 'third_party/android_sdk/public/platform-tools/adb') |
| 34 | AAR_PROJECT_DIR = os.path.join(CHECKOUT_ROOT, 'examples/aarproject') |
| 35 | |
| 36 | |
| 37 | def _ParseArgs(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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() |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def _GetCommitHash(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 60 | commit_hash = subprocess.check_output( |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 61 | ['git', 'rev-parse', 'HEAD'], cwd=CHECKOUT_ROOT).decode('UTF-8').strip() |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 62 | return commit_hash |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def _GetCommitPos(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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) |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def _TestAAR(build_dir): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 78 | """Runs AppRTCMobile tests using the AAR. Returns true if the tests pass.""" |
| 79 | logging.info('Testing library.') |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 80 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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']) |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 86 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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 |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 100 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 101 | return True # Tests pass |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def BuildAndTestAar(use_goma, skip_tests, build_dir): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 105 | version = '1.0.' + _GetCommitPos() |
| 106 | commit = _GetCommitHash() |
| 107 | logging.info('Building and Testing AAR version %s with hash %s', version, |
| 108 | commit) |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 109 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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: |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 132 | if use_tmp_dir: |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 133 | shutil.rmtree(build_dir, True) |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 134 | |
| 135 | |
| 136 | def main(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 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) |
Byoungchan Lee | 0d6d948 | 2021-07-16 03:13:37 +0900 | [diff] [blame] | 140 | |
| 141 | |
| 142 | if __name__ == '__main__': |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 143 | sys.exit(main()) |