2013-03-28 04:23:22 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""Installs deps for using SDK emulator for testing.
|
|
|
|
|
2013-10-05 03:01:59 +04:00
|
|
|
The script will download the SDK and system images, if they are not present, and
|
2013-03-28 04:23:22 +04:00
|
|
|
install and enable KVM, if virtualization has been enabled in the BIOS.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
2013-11-18 23:05:54 +04:00
|
|
|
import optparse
|
2013-03-28 04:23:22 +04:00
|
|
|
import os
|
2013-11-18 23:05:54 +04:00
|
|
|
import re
|
2013-03-28 04:23:22 +04:00
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from pylib import cmd_helper
|
|
|
|
from pylib import constants
|
2013-11-18 23:05:54 +04:00
|
|
|
from pylib import pexpect
|
2013-03-28 04:23:22 +04:00
|
|
|
from pylib.utils import run_tests_helper
|
|
|
|
|
2013-11-18 23:05:54 +04:00
|
|
|
# Android API level
|
|
|
|
DEFAULT_ANDROID_API_LEVEL = constants.ANDROID_SDK_VERSION
|
|
|
|
|
2013-10-05 03:01:59 +04:00
|
|
|
# From the Android Developer's website.
|
2013-11-18 23:05:54 +04:00
|
|
|
# Keep this up to date; the user can install older API levels as necessary.
|
2013-10-05 03:01:59 +04:00
|
|
|
SDK_BASE_URL = 'http://dl.google.com/android/adt'
|
2013-11-18 23:05:54 +04:00
|
|
|
SDK_ZIP = 'adt-bundle-linux-x86_64-20131030.zip'
|
2013-09-13 12:18:38 +04:00
|
|
|
|
2013-03-28 04:23:22 +04:00
|
|
|
# Android x86 system image from the Intel website:
|
|
|
|
# http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean-bin
|
2013-11-18 23:05:54 +04:00
|
|
|
# These don't exist prior to Android-15.
|
|
|
|
# As of 08 Nov 2013, Android-19 is not yet available either.
|
|
|
|
X86_IMG_URLS = {
|
|
|
|
15: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-15_r01.zip',
|
|
|
|
16: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-16_r01.zip',
|
|
|
|
17: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-17_r01.zip',
|
|
|
|
18: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-18_r01.zip'}
|
2013-03-28 04:23:22 +04:00
|
|
|
|
|
|
|
def CheckSDK():
|
|
|
|
"""Check if SDK is already installed.
|
|
|
|
|
|
|
|
Returns:
|
2013-10-05 03:01:59 +04:00
|
|
|
True if the emulator SDK directory (src/android_emulator_sdk/) exists.
|
2013-03-28 04:23:22 +04:00
|
|
|
"""
|
2013-10-05 03:01:59 +04:00
|
|
|
return os.path.exists(constants.EMULATOR_SDK_ROOT)
|
2013-09-13 12:18:38 +04:00
|
|
|
|
|
|
|
|
2013-11-18 23:05:54 +04:00
|
|
|
def CheckSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL):
|
|
|
|
"""Check if the "SDK Platform" for the specified API level is installed.
|
|
|
|
This is necessary in order for the emulator to run when the target
|
|
|
|
is specified.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
api_level: the Android API level to check; defaults to the latest API.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the platform is already installed.
|
|
|
|
"""
|
|
|
|
android_binary = os.path.join(constants.EMULATOR_SDK_ROOT,
|
|
|
|
'sdk', 'tools', 'android')
|
|
|
|
pattern = re.compile('id: [0-9]+ or "android-%d"' % api_level)
|
|
|
|
try:
|
|
|
|
exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(
|
|
|
|
[android_binary, 'list'])
|
|
|
|
if exit_code != 0:
|
|
|
|
raise Exception('\'android list\' command failed')
|
|
|
|
for line in stdout.split('\n'):
|
|
|
|
if pattern.match(line):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
except OSError:
|
|
|
|
logging.exception('Unable to execute \'android list\'')
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def CheckX86Image(api_level=DEFAULT_ANDROID_API_LEVEL):
|
2013-03-28 04:23:22 +04:00
|
|
|
"""Check if Android system images have been installed.
|
|
|
|
|
2013-11-18 23:05:54 +04:00
|
|
|
Args:
|
|
|
|
api_level: the Android API level to check for; defaults to the latest API.
|
|
|
|
|
2013-03-28 04:23:22 +04:00
|
|
|
Returns:
|
2013-11-18 23:05:54 +04:00
|
|
|
True if sdk/system-images/android-<api_level>/x86 exists inside
|
|
|
|
EMULATOR_SDK_ROOT.
|
2013-03-28 04:23:22 +04:00
|
|
|
"""
|
2013-11-18 23:05:54 +04:00
|
|
|
api_target = 'android-%d' % api_level
|
2013-03-28 04:23:22 +04:00
|
|
|
return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT,
|
2013-10-05 03:01:59 +04:00
|
|
|
'sdk', 'system-images',
|
2013-11-18 23:05:54 +04:00
|
|
|
api_target, 'x86'))
|
2013-03-28 04:23:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
def CheckKVM():
|
|
|
|
"""Check if KVM is enabled.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if kvm-ok returns 0 (already enabled)
|
|
|
|
"""
|
2013-04-09 09:40:41 +04:00
|
|
|
try:
|
|
|
|
return not cmd_helper.RunCmd(['kvm-ok'])
|
|
|
|
except OSError:
|
2013-04-10 05:55:31 +04:00
|
|
|
logging.info('kvm-ok not installed')
|
2013-04-09 09:40:41 +04:00
|
|
|
return False
|
2013-03-28 04:23:22 +04:00
|
|
|
|
|
|
|
|
2013-10-05 03:01:59 +04:00
|
|
|
def GetSDK():
|
|
|
|
"""Download the SDK and unzip it into EMULATOR_SDK_ROOT."""
|
|
|
|
logging.info('Download Android SDK.')
|
|
|
|
sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP)
|
|
|
|
try:
|
|
|
|
cmd_helper.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url])
|
|
|
|
print 'curled unzipping...'
|
|
|
|
rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/'])
|
|
|
|
if rc:
|
|
|
|
raise Exception('ERROR: could not download/unzip Android SDK.')
|
|
|
|
# Get the name of the sub-directory that everything will be extracted to.
|
|
|
|
dirname, _ = os.path.splitext(SDK_ZIP)
|
|
|
|
zip_dir = '/tmp/%s' % dirname
|
|
|
|
# Move the extracted directory to EMULATOR_SDK_ROOT
|
|
|
|
shutil.move(zip_dir, constants.EMULATOR_SDK_ROOT)
|
|
|
|
finally:
|
|
|
|
os.unlink('/tmp/sdk.zip')
|
|
|
|
|
|
|
|
|
2013-03-28 04:23:22 +04:00
|
|
|
def InstallKVM():
|
|
|
|
"""Installs KVM packages."""
|
|
|
|
rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
|
|
|
|
if rc:
|
|
|
|
logging.critical('ERROR: Did not install KVM. Make sure hardware '
|
|
|
|
'virtualization is enabled in BIOS (i.e. Intel VT-x or '
|
|
|
|
'AMD SVM).')
|
|
|
|
# TODO(navabi): Use modprobe kvm-amd on AMD processors.
|
|
|
|
rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
|
|
|
|
if rc:
|
|
|
|
logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure '
|
|
|
|
'hardware virtualization is enabled in BIOS.')
|
|
|
|
# Now check to ensure KVM acceleration can be used.
|
|
|
|
rc = cmd_helper.RunCmd(['kvm-ok'])
|
|
|
|
if rc:
|
|
|
|
logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
|
|
|
|
'virtualization is enabled in BIOS (i.e. Intel VT-x or '
|
|
|
|
'AMD SVM).')
|
|
|
|
|
|
|
|
|
2013-11-18 23:05:54 +04:00
|
|
|
def GetX86Image(api_level=DEFAULT_ANDROID_API_LEVEL):
|
|
|
|
"""Download x86 system image from Intel's website.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
api_level: the Android API level to download for.
|
|
|
|
"""
|
2013-03-28 04:23:22 +04:00
|
|
|
logging.info('Download x86 system image directory into sdk directory.')
|
2013-11-18 23:05:54 +04:00
|
|
|
# TODO(andrewhayden): Use python tempfile lib instead
|
|
|
|
temp_file = '/tmp/x86_img_android-%d.zip' % api_level
|
|
|
|
if api_level not in X86_IMG_URLS:
|
|
|
|
raise Exception('ERROR: no URL known for x86 image for android-%s' %
|
|
|
|
api_level)
|
2013-03-28 04:23:22 +04:00
|
|
|
try:
|
2013-11-18 23:05:54 +04:00
|
|
|
cmd_helper.RunCmd(['curl', '-o', temp_file, X86_IMG_URLS[api_level]])
|
|
|
|
rc = cmd_helper.RunCmd(['unzip', '-o', temp_file, '-d', '/tmp/'])
|
2013-03-28 04:23:22 +04:00
|
|
|
if rc:
|
2013-09-13 12:18:38 +04:00
|
|
|
raise Exception('ERROR: Could not download/unzip image zip.')
|
2013-11-18 23:05:54 +04:00
|
|
|
api_target = 'android-%d' % api_level
|
2013-10-05 03:01:59 +04:00
|
|
|
sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk',
|
2013-11-18 23:05:54 +04:00
|
|
|
'system-images', api_target, 'x86')
|
|
|
|
logging.info('Deploying system image to %s' % sys_imgs)
|
2013-03-28 04:23:22 +04:00
|
|
|
shutil.move('/tmp/x86', sys_imgs)
|
|
|
|
finally:
|
2013-11-18 23:05:54 +04:00
|
|
|
os.unlink(temp_file)
|
|
|
|
|
|
|
|
|
|
|
|
def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL):
|
|
|
|
"""Update the SDK to include the platform specified.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
api_level: the Android API level to download
|
|
|
|
"""
|
|
|
|
android_binary = os.path.join(constants.EMULATOR_SDK_ROOT,
|
|
|
|
'sdk', 'tools', 'android')
|
|
|
|
pattern = re.compile('\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' %
|
|
|
|
api_level)
|
|
|
|
# Example:
|
|
|
|
# 2- SDK Platform Android 4.3, API 18, revision 2
|
|
|
|
exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(
|
|
|
|
[android_binary, 'list', 'sdk'])
|
|
|
|
if exit_code != 0:
|
|
|
|
raise Exception('\'android list sdk\' command return %d' % exit_code)
|
|
|
|
for line in stdout.split('\n'):
|
|
|
|
match = pattern.match(line)
|
|
|
|
if match:
|
|
|
|
index = match.group(1)
|
|
|
|
print('package %s corresponds to platform level %d' % (index, api_level))
|
|
|
|
# update sdk --no-ui --filter $INDEX
|
|
|
|
update_command = [android_binary,
|
|
|
|
'update', 'sdk', '--no-ui', '--filter', index]
|
|
|
|
update_command_str = ' '.join(update_command)
|
|
|
|
logging.info('running update command: %s' % update_command_str)
|
|
|
|
update_process = pexpect.spawn(update_command_str)
|
|
|
|
# TODO(andrewhayden): Do we need to bug the user about this?
|
|
|
|
if update_process.expect('Do you accept the license') != 0:
|
|
|
|
raise Exception('License agreement check failed')
|
|
|
|
update_process.sendline('y')
|
|
|
|
if update_process.expect('Done. 1 package installed.') == 0:
|
|
|
|
print('Successfully installed platform for API level %d' % api_level)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise Exception('Failed to install platform update')
|
|
|
|
raise Exception('Could not find android-%d update for the SDK!' % api_level)
|
2013-03-28 04:23:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
2013-11-18 23:05:54 +04:00
|
|
|
opt_parser = optparse.OptionParser(
|
|
|
|
description='Install dependencies for running the Android emulator')
|
|
|
|
opt_parser.add_option('--api-level', dest='api_level',
|
|
|
|
help='The API level (e.g., 19 for Android 4.4) to ensure is available',
|
|
|
|
type='int', default=DEFAULT_ANDROID_API_LEVEL)
|
|
|
|
opt_parser.add_option('-v', dest='verbose', action='store_true',
|
|
|
|
help='enable verbose logging')
|
|
|
|
options, _ = opt_parser.parse_args(argv[1:])
|
|
|
|
|
|
|
|
# run_tests_helper will set logging to INFO or DEBUG
|
|
|
|
# We achieve verbose output by configuring it with 2 (==DEBUG)
|
|
|
|
verbosity = 1
|
|
|
|
if (options.verbose):
|
|
|
|
verbosity = 2
|
2013-03-28 04:23:22 +04:00
|
|
|
logging.basicConfig(level=logging.INFO,
|
|
|
|
format='# %(asctime)-15s: %(message)s')
|
2013-11-18 23:05:54 +04:00
|
|
|
run_tests_helper.SetLogLevel(verbose_count=verbosity)
|
2013-03-28 04:23:22 +04:00
|
|
|
|
2013-10-05 03:01:59 +04:00
|
|
|
# Calls below will download emulator SDK and/or system images only if needed.
|
|
|
|
if CheckSDK():
|
|
|
|
logging.info('android_emulator_sdk/ already exists, skipping download.')
|
2013-09-13 12:18:38 +04:00
|
|
|
else:
|
2013-10-05 03:01:59 +04:00
|
|
|
GetSDK()
|
2013-09-13 12:18:38 +04:00
|
|
|
|
2013-11-18 23:05:54 +04:00
|
|
|
# Check target. The target has to be installed in order to run the emulator.
|
|
|
|
if CheckSDKPlatform(options.api_level):
|
|
|
|
logging.info('SDK platform android-%d already present, skipping.' %
|
|
|
|
options.api_level)
|
|
|
|
else:
|
|
|
|
logging.info('SDK platform android-%d not present, installing.' %
|
|
|
|
options.api_level)
|
|
|
|
GetSDKPlatform(options.api_level)
|
|
|
|
|
2013-10-05 03:01:59 +04:00
|
|
|
# Download the x86 system image only if needed.
|
2013-11-18 23:05:54 +04:00
|
|
|
if CheckX86Image(options.api_level):
|
|
|
|
logging.info('x86 image for android-%d already present, skipping.' %
|
|
|
|
options.api_level)
|
2013-03-28 04:23:22 +04:00
|
|
|
else:
|
2013-11-18 23:05:54 +04:00
|
|
|
GetX86Image(options.api_level)
|
2013-03-28 04:23:22 +04:00
|
|
|
|
|
|
|
# Make sure KVM packages are installed and enabled.
|
|
|
|
if CheckKVM():
|
|
|
|
logging.info('KVM already installed and enabled.')
|
|
|
|
else:
|
|
|
|
InstallKVM()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv))
|