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
4 # found in the LICENSE file.
6 """Installs deps for using SDK emulator for testing.
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.
20 from pylib
import cmd_helper
21 from pylib
import constants
22 from pylib
import pexpect
23 from pylib
.utils
import run_tests_helper
26 DEFAULT_ANDROID_API_LEVEL
= constants
.ANDROID_SDK_VERSION
28 # From the Android Developer's website.
29 # Keep this up to date; the user can install older API levels as necessary.
30 SDK_BASE_URL
= 'http://dl.google.com/android/adt'
31 SDK_ZIP
= 'adt-bundle-linux-x86_64-20131030.zip'
33 # Android x86 system image from the Intel website:
34 # http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean-bin
35 # These don't exist prior to Android-15.
36 # As of 08 Nov 2013, Android-19 is not yet available either.
38 15: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-15_r01.zip',
39 16: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-16_r01.zip',
40 17: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-17_r01.zip',
41 18: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-18_r01.zip',
42 19: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-19_r01.zip'}
45 """Check if SDK is already installed.
48 True if the emulator SDK directory (src/android_emulator_sdk/) exists.
50 return os
.path
.exists(constants
.EMULATOR_SDK_ROOT
)
53 def CheckSDKPlatform(api_level
=DEFAULT_ANDROID_API_LEVEL
):
54 """Check if the "SDK Platform" for the specified API level is installed.
55 This is necessary in order for the emulator to run when the target
59 api_level: the Android API level to check; defaults to the latest API.
62 True if the platform is already installed.
64 android_binary
= os
.path
.join(constants
.EMULATOR_SDK_ROOT
,
65 'sdk', 'tools', 'android')
66 pattern
= re
.compile('id: [0-9]+ or "android-%d"' % api_level
)
68 exit_code
, stdout
= cmd_helper
.GetCmdStatusAndOutput(
69 [android_binary
, 'list'])
71 raise Exception('\'android list\' command failed')
72 for line
in stdout
.split('\n'):
73 if pattern
.match(line
):
77 logging
.exception('Unable to execute \'android list\'')
81 def CheckX86Image(api_level
=DEFAULT_ANDROID_API_LEVEL
):
82 """Check if Android system images have been installed.
85 api_level: the Android API level to check for; defaults to the latest API.
88 True if sdk/system-images/android-<api_level>/x86 exists inside
91 api_target
= 'android-%d' % api_level
92 return os
.path
.exists(os
.path
.join(constants
.EMULATOR_SDK_ROOT
,
93 'sdk', 'system-images',
98 """Quickly check whether KVM is enabled.
101 True iff /dev/kvm exists (Linux only).
103 return os
.path
.exists('/dev/kvm')
107 """Run kvm-ok as root to check that KVM is properly enabled after installation
108 of the required packages.
111 True iff KVM is enabled (/dev/kvm exists). On failure, returns False
112 but also print detailed information explaining why KVM isn't enabled
113 (e.g. CPU doesn't support it, or BIOS disabled it).
116 # Note: kvm-ok is in /usr/sbin, so always use 'sudo' to run it.
117 return not cmd_helper
.RunCmd(['sudo', 'kvm-ok'])
119 logging
.info('kvm-ok not installed')
124 """Download the SDK and unzip it into EMULATOR_SDK_ROOT."""
125 logging
.info('Download Android SDK.')
126 sdk_url
= '%s/%s' % (SDK_BASE_URL
, SDK_ZIP
)
128 cmd_helper
.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url
])
129 print 'curled unzipping...'
130 rc
= cmd_helper
.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/'])
132 raise Exception('ERROR: could not download/unzip Android SDK.')
133 # Get the name of the sub-directory that everything will be extracted to.
134 dirname
, _
= os
.path
.splitext(SDK_ZIP
)
135 zip_dir
= '/tmp/%s' % dirname
136 # Move the extracted directory to EMULATOR_SDK_ROOT
137 shutil
.move(zip_dir
, constants
.EMULATOR_SDK_ROOT
)
139 os
.unlink('/tmp/sdk.zip')
143 """Installs KVM packages."""
144 rc
= cmd_helper
.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
146 logging
.critical('ERROR: Did not install KVM. Make sure hardware '
147 'virtualization is enabled in BIOS (i.e. Intel VT-x or '
149 # TODO(navabi): Use modprobe kvm-amd on AMD processors.
150 rc
= cmd_helper
.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
152 logging
.critical('ERROR: Did not add KVM module to Linux Kernel. Make sure '
153 'hardware virtualization is enabled in BIOS.')
154 # Now check to ensure KVM acceleration can be used.
156 logging
.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
157 'virtualization is enabled in BIOS (i.e. Intel VT-x or '
161 def GetX86Image(api_level
=DEFAULT_ANDROID_API_LEVEL
):
162 """Download x86 system image from Intel's website.
165 api_level: the Android API level to download for.
167 logging
.info('Download x86 system image directory into sdk directory.')
168 # TODO(andrewhayden): Use python tempfile lib instead
169 temp_file
= '/tmp/x86_img_android-%d.zip' % api_level
170 if api_level
not in X86_IMG_URLS
:
171 raise Exception('ERROR: no URL known for x86 image for android-%s' %
174 cmd_helper
.RunCmd(['curl', '-o', temp_file
, X86_IMG_URLS
[api_level
]])
175 rc
= cmd_helper
.RunCmd(['unzip', '-o', temp_file
, '-d', '/tmp/'])
177 raise Exception('ERROR: Could not download/unzip image zip.')
178 api_target
= 'android-%d' % api_level
179 sys_imgs
= os
.path
.join(constants
.EMULATOR_SDK_ROOT
, 'sdk',
180 'system-images', api_target
, 'x86')
181 logging
.info('Deploying system image to %s' % sys_imgs
)
182 shutil
.move('/tmp/x86', sys_imgs
)
187 def GetSDKPlatform(api_level
=DEFAULT_ANDROID_API_LEVEL
):
188 """Update the SDK to include the platform specified.
191 api_level: the Android API level to download
193 android_binary
= os
.path
.join(constants
.EMULATOR_SDK_ROOT
,
194 'sdk', 'tools', 'android')
195 pattern
= re
.compile('\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' %
198 # 2- SDK Platform Android 4.3, API 18, revision 2
199 exit_code
, stdout
= cmd_helper
.GetCmdStatusAndOutput(
200 [android_binary
, 'list', 'sdk'])
202 raise Exception('\'android list sdk\' command return %d' % exit_code
)
203 for line
in stdout
.split('\n'):
204 match
= pattern
.match(line
)
206 index
= match
.group(1)
207 print('package %s corresponds to platform level %d' % (index
, api_level
))
208 # update sdk --no-ui --filter $INDEX
209 update_command
= [android_binary
,
210 'update', 'sdk', '--no-ui', '--filter', index
]
211 update_command_str
= ' '.join(update_command
)
212 logging
.info('running update command: %s' % update_command_str
)
213 update_process
= pexpect
.spawn(update_command_str
)
214 # TODO(andrewhayden): Do we need to bug the user about this?
215 if update_process
.expect('Do you accept the license') != 0:
216 raise Exception('License agreement check failed')
217 update_process
.sendline('y')
218 if update_process
.expect('Done. 1 package installed.') == 0:
219 print('Successfully installed platform for API level %d' % api_level
)
222 raise Exception('Failed to install platform update')
223 raise Exception('Could not find android-%d update for the SDK!' % api_level
)
227 opt_parser
= optparse
.OptionParser(
228 description
='Install dependencies for running the Android emulator')
229 opt_parser
.add_option('--api-level', dest
='api_level',
230 help='The API level (e.g., 19 for Android 4.4) to ensure is available',
231 type='int', default
=DEFAULT_ANDROID_API_LEVEL
)
232 opt_parser
.add_option('-v', dest
='verbose', action
='store_true',
233 help='enable verbose logging')
234 options
, _
= opt_parser
.parse_args(argv
[1:])
236 # run_tests_helper will set logging to INFO or DEBUG
237 # We achieve verbose output by configuring it with 2 (==DEBUG)
239 if (options
.verbose
):
241 logging
.basicConfig(level
=logging
.INFO
,
242 format
='# %(asctime)-15s: %(message)s')
243 run_tests_helper
.SetLogLevel(verbose_count
=verbosity
)
245 # Calls below will download emulator SDK and/or system images only if needed.
247 logging
.info('android_emulator_sdk/ already exists, skipping download.')
251 # Check target. The target has to be installed in order to run the emulator.
252 if CheckSDKPlatform(options
.api_level
):
253 logging
.info('SDK platform android-%d already present, skipping.' %
256 logging
.info('SDK platform android-%d not present, installing.' %
258 GetSDKPlatform(options
.api_level
)
260 # Download the x86 system image only if needed.
261 if CheckX86Image(options
.api_level
):
262 logging
.info('x86 image for android-%d already present, skipping.' %
265 GetX86Image(options
.api_level
)
267 # Make sure KVM packages are installed and enabled.
269 logging
.info('KVM already installed and enabled.')
274 if __name__
== '__main__':
275 sys
.exit(main(sys
.argv
))