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