Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(694)

Side by Side Diff: build/android/install_emulator_deps.py

Issue 1341503002: fix emulator.py by using Android SDK in third_party/android_tools instead of downloading adt (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixing logging error Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/avd.py ('k') | build/android/pylib/constants/__init__.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Installs deps for using SDK emulator for testing. 6 """Installs deps for using SDK emulator for testing.
7 7
8 The script will download the SDK and system images, if they are not present, and 8 The script will download the SDK and system images, if they are not present, and
9 install and enable KVM, if virtualization has been enabled in the BIOS. 9 install and enable KVM, if virtualization has been enabled in the BIOS.
10 """ 10 """
11 11
12 12
13 import logging 13 import logging
14 import optparse 14 import optparse
15 import os 15 import os
16 import re 16 import re
17 import shutil
18 import sys 17 import sys
19 18
20 from devil.utils import cmd_helper 19 from devil.utils import cmd_helper
21 from devil.utils import run_tests_helper 20 from devil.utils import run_tests_helper
22 from pylib import constants 21 from pylib import constants
23 from pylib import pexpect 22 from pylib import pexpect
24 23
25 # Android API level 24 # Android API level
26 DEFAULT_ANDROID_API_LEVEL = constants.ANDROID_SDK_VERSION 25 DEFAULT_ANDROID_API_LEVEL = constants.ANDROID_SDK_VERSION
27 26
28 # From the Android Developer's website. 27 # Default Time out for downloading SDK component
29 # Keep this up to date; the user can install older API levels as necessary. 28 DOWNLOAD_SYSTEM_IMAGE_TIMEOUT = 30
30 SDK_BASE_URL = 'http://dl.google.com/android/adt' 29 DOWNLOAD_SDK_PLATFORM_TIMEOUT = 60
31 SDK_ZIP = 'adt-bundle-linux-x86_64-20131030.zip'
32
33 # pylint: disable=line-too-long
34 # Android x86 system image from the Intel website:
35 # http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean -bin
36 # These don't exist prior to Android-15.
37 # As of 08 Nov 2013, Android-19 is not yet available either.
38 X86_IMG_URLS = {
39 15: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-15_r01.zi p',
40 16: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-16_r01.zi p',
41 17: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-17_r01.zi p',
42 18: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-18_r01.zi p',
43 19: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-19_r01.zi p'}
44 #pylint: enable=line-too-long
45 30
46 def CheckSDK(): 31 def CheckSDK():
47 """Check if SDK is already installed. 32 """Check if SDK is already installed.
48 33
49 Returns: 34 Returns:
50 True if the emulator SDK directory (src/android_emulator_sdk/) exists. 35 True if the emulator SDK directory (src/android_emulator_sdk/) exists.
51 """ 36 """
52 return os.path.exists(constants.EMULATOR_SDK_ROOT) 37 return os.path.exists(constants.ANDROID_SDK_ROOT)
53 38
54 39
55 def CheckSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): 40 def CheckSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL):
56 """Check if the "SDK Platform" for the specified API level is installed. 41 """Check if the "SDK Platform" for the specified API level is installed.
57 This is necessary in order for the emulator to run when the target 42 This is necessary in order for the emulator to run when the target
58 is specified. 43 is specified.
59 44
60 Args: 45 Args:
61 api_level: the Android API level to check; defaults to the latest API. 46 api_level: the Android API level to check; defaults to the latest API.
62 47
63 Returns: 48 Returns:
64 True if the platform is already installed. 49 True if the platform is already installed.
65 """ 50 """
66 android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, 51 android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android')
67 'sdk', 'tools', 'android')
68 pattern = re.compile('id: [0-9]+ or "android-%d"' % api_level) 52 pattern = re.compile('id: [0-9]+ or "android-%d"' % api_level)
69 try: 53 try:
70 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput( 54 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(
71 [android_binary, 'list']) 55 [android_binary, 'list'])
72 if exit_code != 0: 56 if exit_code != 0:
73 raise Exception('\'android list\' command failed') 57 raise Exception('\'android list\' command failed')
74 for line in stdout.split('\n'): 58 for line in stdout.split('\n'):
75 if pattern.match(line): 59 if pattern.match(line):
76 return True 60 return True
77 return False 61 return False
78 except OSError: 62 except OSError:
79 logging.exception('Unable to execute \'android list\'') 63 logging.exception('Unable to execute \'android list\'')
80 return False 64 return False
81 65
82 66
83 def CheckX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): 67 def CheckX86Image(api_level=DEFAULT_ANDROID_API_LEVEL):
84 """Check if Android system images have been installed. 68 """Check if Android system images have been installed.
85 69
86 Args: 70 Args:
87 api_level: the Android API level to check for; defaults to the latest API. 71 api_level: the Android API level to check for; defaults to the latest API.
88 72
89 Returns: 73 Returns:
90 True if sdk/system-images/android-<api_level>/x86 exists inside 74 True if x86 image has been previously downloaded.
91 EMULATOR_SDK_ROOT.
92 """ 75 """
93 api_target = 'android-%d' % api_level 76 api_target = 'android-%d' % api_level
94 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, 77 return os.path.exists(os.path.join(constants.ANDROID_SDK_ROOT,
95 'sdk', 'system-images', 78 'system-images', api_target, 'default',
96 api_target, 'x86')) 79 'x86'))
97 80
98 81
99 def CheckKVM(): 82 def CheckKVM():
100 """Quickly check whether KVM is enabled. 83 """Quickly check whether KVM is enabled.
101 84
102 Returns: 85 Returns:
103 True iff /dev/kvm exists (Linux only). 86 True iff /dev/kvm exists (Linux only).
104 """ 87 """
105 return os.path.exists('/dev/kvm') 88 return os.path.exists('/dev/kvm')
106 89
107 90
108 def RunKvmOk(): 91 def RunKvmOk():
109 """Run kvm-ok as root to check that KVM is properly enabled after installation 92 """Run kvm-ok as root to check that KVM is properly enabled after installation
110 of the required packages. 93 of the required packages.
111 94
112 Returns: 95 Returns:
113 True iff KVM is enabled (/dev/kvm exists). On failure, returns False 96 True iff KVM is enabled (/dev/kvm exists). On failure, returns False
114 but also print detailed information explaining why KVM isn't enabled 97 but also print detailed information explaining why KVM isn't enabled
115 (e.g. CPU doesn't support it, or BIOS disabled it). 98 (e.g. CPU doesn't support it, or BIOS disabled it).
116 """ 99 """
117 try: 100 try:
118 # Note: kvm-ok is in /usr/sbin, so always use 'sudo' to run it. 101 # Note: kvm-ok is in /usr/sbin, so always use 'sudo' to run it.
119 return not cmd_helper.RunCmd(['sudo', 'kvm-ok']) 102 return not cmd_helper.RunCmd(['sudo', 'kvm-ok'])
120 except OSError: 103 except OSError:
121 logging.info('kvm-ok not installed') 104 logging.info('kvm-ok not installed')
122 return False 105 return False
123 106
124 107
125 def GetSDK():
126 """Download the SDK and unzip it into EMULATOR_SDK_ROOT."""
127 logging.info('Download Android SDK.')
128 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP)
129 try:
130 cmd_helper.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url])
131 print 'curled unzipping...'
132 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/'])
133 if rc:
134 raise Exception('ERROR: could not download/unzip Android SDK.')
135 # Get the name of the sub-directory that everything will be extracted to.
136 dirname, _ = os.path.splitext(SDK_ZIP)
137 zip_dir = '/tmp/%s' % dirname
138 # Move the extracted directory to EMULATOR_SDK_ROOT
139 shutil.move(zip_dir, constants.EMULATOR_SDK_ROOT)
140 finally:
141 os.unlink('/tmp/sdk.zip')
142
143
144 def InstallKVM(): 108 def InstallKVM():
145 """Installs KVM packages.""" 109 """Installs KVM packages."""
146 rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm']) 110 rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
147 if rc: 111 if rc:
148 logging.critical('ERROR: Did not install KVM. Make sure hardware ' 112 logging.critical('ERROR: Did not install KVM. Make sure hardware '
149 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' 113 'virtualization is enabled in BIOS (i.e. Intel VT-x or '
150 'AMD SVM).') 114 'AMD SVM).')
151 # TODO(navabi): Use modprobe kvm-amd on AMD processors. 115 # TODO(navabi): Use modprobe kvm-amd on AMD processors.
152 rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel']) 116 rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
153 if rc: 117 if rc:
154 logging.critical('ERROR: Did not add KVM module to Linux Kernel. Make sure ' 118 logging.critical('ERROR: Did not add KVM module to Linux Kernel. Make sure '
155 'hardware virtualization is enabled in BIOS.') 119 'hardware virtualization is enabled in BIOS.')
156 # Now check to ensure KVM acceleration can be used. 120 # Now check to ensure KVM acceleration can be used.
157 if not RunKvmOk(): 121 if not RunKvmOk():
158 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' 122 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
159 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' 123 'virtualization is enabled in BIOS (i.e. Intel VT-x or '
160 'AMD SVM).') 124 'AMD SVM).')
161 125
162 126
127 def UpdateSDK(api_level, package_name, package_pattern, timeout):
128 """This function update SDK with a filter index.
129
130 Args:
131 api_level: the Android API level to download for.
132 package_name: logging name of package that is being updated.
133 package_pattern: the pattern to match the filter index from.
134 timeout: the amount of time wait for update command.
135 """
136 android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android')
137
138 list_sdk_repo_command = [android_binary, 'list', 'sdk', '--all']
139
140 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(list_sdk_repo_command)
141
142 if exit_code != 0:
143 raise Exception('\'android list sdk --all\' command return %d' % exit_code)
144
145 for line in stdout.split('\n'):
146 match = package_pattern.match(line)
147 if match:
148 index = match.group(1)
149 logging.info('package %s corresponds to %s with api level %d',
150 index, package_name, api_level)
151 update_command = [android_binary, 'update', 'sdk', '--no-ui', '--all',
152 '--filter', index]
153 update_command_str = ' '.join(update_command)
154 logging.info('running update command: %s', update_command_str)
155 update_process = pexpect.spawn(update_command_str)
156
157 if update_process.expect('Do you accept the license') != 0:
158 raise Exception('License agreement check failed')
159 update_process.sendline('y')
160 if update_process.expect(
161 'Done. 1 package installed.', timeout=timeout) == 0:
162 logging.info('Successfully installed %s for API level %d',
163 package_name, api_level)
164 return
165 else:
166 raise Exception('Failed to install platform update')
167 raise Exception('Could not find android-%d update for the SDK!' % api_level)
168
163 def GetX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): 169 def GetX86Image(api_level=DEFAULT_ANDROID_API_LEVEL):
164 """Download x86 system image from Intel's website. 170 """Download x86 system image from Intel's website.
165 171
166 Args: 172 Args:
167 api_level: the Android API level to download for. 173 api_level: the Android API level to download for.
168 """ 174 """
169 logging.info('Download x86 system image directory into sdk directory.') 175 logging.info('Download x86 system image directory into sdk directory.')
170 # TODO(andrewhayden): Use python tempfile lib instead
171 temp_file = '/tmp/x86_img_android-%d.zip' % api_level
172 if api_level not in X86_IMG_URLS:
173 raise Exception('ERROR: no URL known for x86 image for android-%s' %
174 api_level)
175 try:
176 cmd_helper.RunCmd(['curl', '-o', temp_file, X86_IMG_URLS[api_level]])
177 rc = cmd_helper.RunCmd(['unzip', '-o', temp_file, '-d', '/tmp/'])
178 if rc:
179 raise Exception('ERROR: Could not download/unzip image zip.')
180 api_target = 'android-%d' % api_level
181 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk',
182 'system-images', api_target, 'x86')
183 logging.info('Deploying system image to %s', sys_imgs)
184 shutil.move('/tmp/x86', sys_imgs)
185 finally:
186 os.unlink(temp_file)
187 176
177 x86_package_pattern = re.compile(
178 r'\s*([0-9]+)- Intel x86 Atom System Image, Android API %d.*' % api_level)
179
180 UpdateSDK(api_level, 'x86 system image', x86_package_pattern,
181 DOWNLOAD_SYSTEM_IMAGE_TIMEOUT)
188 182
189 def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): 183 def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL):
190 """Update the SDK to include the platform specified. 184 """Update the SDK to include the platform specified.
191 185
192 Args: 186 Args:
193 api_level: the Android API level to download 187 api_level: the Android API level to download
194 """ 188 """
195 android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, 189 logging.info('Download SDK Platform directory into sdk directory.')
196 'sdk', 'tools', 'android') 190
197 pattern = re.compile( 191 platform_package_pattern = re.compile(
198 r'\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' % api_level) 192 r'\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' % api_level)
199 # Example: 193
200 # 2- SDK Platform Android 4.3, API 18, revision 2 194 UpdateSDK(api_level, 'SDK Platform', platform_package_pattern,
201 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput( 195 DOWNLOAD_SDK_PLATFORM_TIMEOUT)
202 [android_binary, 'list', 'sdk'])
203 if exit_code != 0:
204 raise Exception('\'android list sdk\' command return %d' % exit_code)
205 for line in stdout.split('\n'):
206 match = pattern.match(line)
207 if match:
208 index = match.group(1)
209 print 'package %s corresponds to platform level %d' % (index, api_level)
210 # update sdk --no-ui --filter $INDEX
211 update_command = [android_binary,
212 'update', 'sdk', '--no-ui', '--filter', index]
213 update_command_str = ' '.join(update_command)
214 logging.info('running update command: %s', update_command_str)
215 update_process = pexpect.spawn(update_command_str)
216 # TODO(andrewhayden): Do we need to bug the user about this?
217 if update_process.expect('Do you accept the license') != 0:
218 raise Exception('License agreement check failed')
219 update_process.sendline('y')
220 if update_process.expect('Done. 1 package installed.') == 0:
221 print 'Successfully installed platform for API level %d' % api_level
222 return
223 else:
224 raise Exception('Failed to install platform update')
225 raise Exception('Could not find android-%d update for the SDK!' % api_level)
226 196
227 197
228 def main(argv): 198 def main(argv):
229 opt_parser = optparse.OptionParser( 199 opt_parser = optparse.OptionParser(
230 description='Install dependencies for running the Android emulator') 200 description='Install dependencies for running the Android emulator')
231 opt_parser.add_option('--api-level', dest='api_level', 201 opt_parser.add_option('--api-level',
232 help='The API level (e.g., 19 for Android 4.4) to ensure is available', 202 dest='api_level',
233 type='int', default=DEFAULT_ANDROID_API_LEVEL) 203 help=('The API level (e.g., 19 for Android 4.4) to '
234 opt_parser.add_option('-v', dest='verbose', action='store_true', 204 'ensure is available'),
235 help='enable verbose logging') 205 type='int',
206 default=DEFAULT_ANDROID_API_LEVEL)
207 opt_parser.add_option('-v',
208 dest='verbosity',
209 default=1,
210 action='count',
211 help='Verbose level (multiple times for more)')
236 options, _ = opt_parser.parse_args(argv[1:]) 212 options, _ = opt_parser.parse_args(argv[1:])
237 213
238 # run_tests_helper will set logging to INFO or DEBUG 214 run_tests_helper.SetLogLevel(verbose_count=options.verbosity)
239 # We achieve verbose output by configuring it with 2 (==DEBUG)
240 verbosity = 1
241 if options.verbose:
242 verbosity = 2
243 logging.basicConfig(level=logging.INFO,
244 format='# %(asctime)-15s: %(message)s')
245 run_tests_helper.SetLogLevel(verbose_count=verbosity)
246 215
247 # Calls below will download emulator SDK and/or system images only if needed. 216 # Calls below will download emulator SDK and/or system images only if needed.
248 if CheckSDK(): 217 if CheckSDK():
249 logging.info('android_emulator_sdk/ already exists, skipping download.') 218 logging.info('android_emulator_sdk/ exists')
250 else: 219 else:
251 GetSDK() 220 logging.critical('ERROR: Emulator SDK not installed in %s'
221 , constants.ANDROID_SDK_ROOT)
222 return 1
252 223
253 # Check target. The target has to be installed in order to run the emulator. 224 # Check target. The target has to be installed in order to run the emulator.
254 if CheckSDKPlatform(options.api_level): 225 if CheckSDKPlatform(options.api_level):
255 logging.info('SDK platform android-%d already present, skipping.', 226 logging.info('SDK platform android-%d already present, skipping.',
256 options.api_level) 227 options.api_level)
257 else: 228 else:
258 logging.info('SDK platform android-%d not present, installing.', 229 logging.info('SDK platform android-%d not present, installing.',
259 options.api_level) 230 options.api_level)
260 GetSDKPlatform(options.api_level) 231 GetSDKPlatform(options.api_level)
261 232
262 # Download the x86 system image only if needed. 233 # Download the x86 system image only if needed.
263 if CheckX86Image(options.api_level): 234 if CheckX86Image(options.api_level):
264 logging.info('x86 image for android-%d already present, skipping.', 235 logging.info('x86 image for android-%d already present, skipping.',
265 options.api_level) 236 options.api_level)
266 else: 237 else:
267 GetX86Image(options.api_level) 238 GetX86Image(options.api_level)
268 239
269 # Make sure KVM packages are installed and enabled. 240 # Make sure KVM packages are installed and enabled.
270 if CheckKVM(): 241 if CheckKVM():
271 logging.info('KVM already installed and enabled.') 242 logging.info('KVM already installed and enabled.')
272 else: 243 else:
273 InstallKVM() 244 InstallKVM()
274 245
275 246
276 if __name__ == '__main__': 247 if __name__ == '__main__':
277 sys.exit(main(sys.argv)) 248 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « build/android/avd.py ('k') | build/android/pylib/constants/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698