Chromium Code Reviews| Index: build/android/avd.py |
| diff --git a/build/android/avd.py b/build/android/avd.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..19096f2ee7230a1d65f6f9f0cf41350b7421bd77 |
| --- /dev/null |
| +++ b/build/android/avd.py |
| @@ -0,0 +1,205 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2012 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. |
| + |
| +"""Launches Android Virtual Devices with a set configuration for testing Chrome. |
| + |
| +The script will download the SDK and system images, if they are not present, |
| +install ane enable KVM, if virtualization has been enabled in the BIOS, and then |
| +launch a specified number of Android Virtual Devices (AVD's). |
| +""" |
| + |
| + |
| +import logging |
| +import optparse |
| +import os |
| +import subprocess |
| +import shutil |
| +import sys |
| + |
| +from pylib.utils import emulator |
| + |
| +# From the Android Developer's website. |
| +SDK_BASE_URL = 'http://dl.google.com/android/adt' |
| +SDK_ZIP = 'adt-bundle-linux-x86_64-20130219.zip' |
| + |
| +# From the Intel website: |
| +# http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean-bin |
| +X86_IMG_URL = 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-17_r01.zip' |
| + |
| +# Android API level |
| +API_TARGET = 'android-17' |
| + |
| + |
| +def RunCommand(args): |
| + """Execute a command and return stdout and stderr. |
| + |
| + Args: |
| + args: list of the command and the args to the command. |
| + |
| + Returns: |
| + (output, stderr, rc): stdout and stderr and return code |
| + """ |
| + logging.info('Running command: %s' % args) |
| + proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| + output, stderr = proc.communicate() |
| + rc = proc.returncode |
| + return (str(output).strip(), stderr, rc) |
| + |
| + |
| +def CheckSDK(): |
| + """Check if SDK already installed. |
|
pasko-google - do not use
2013/03/14 15:46:28
s/SDK /SDK is/
navabi
2013/03/20 21:40:16
Done.
|
| + |
| + Returns: |
| + true if android_tools directory exists in current directory. |
| + """ |
| + return os.path.exists(os.path.join(os.getcwd(), 'android_tools')) |
| + |
| + |
| +def CheckX86Image(): |
| + """Check if Android system images have been installed. |
| + |
| + Returns: |
| + true if android_tools/sdk/system-images directory exists. |
| + """ |
| + return os.path.exists(os.path.join(os.getcwd(), 'android_tools', 'sdk', |
| + 'system-images', API_TARGET, 'x86')) |
| + |
| + |
| +def CheckKVM(): |
| + """Check if KVM is enabled. |
| + |
| + Returns: |
| + true if kvm-ok returns 0 (already enabled) |
| + """ |
| + (_, _, rc) = RunCommand(['kvm-ok']) |
| + return not rc |
| + |
| + |
| +def GetSDK(): |
| + """Download the SDK and unzip in android_tools directory. |
| + |
| + Returns: |
| + true if SDK successfully downloaded, false otherwise |
| + """ |
| + logging.info('Download Android SDK.') |
| + sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP) |
| + try: |
| + # RunCommand(['curl', '-o', '/tmp/sdk.zip', sdk_url]) |
| + (_, stderr, rc) = RunCommand(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/']) |
| + if rc: |
| + print stderr |
| + raise |
| + # 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 the current working directory. |
| + shutil.move(zip_dir, 'android_tools') |
| + except: |
| + logging.critical('ERROR: Could not download Android SDK.') |
| + return False |
| + return True |
| + |
| + |
| +def InstallKVM(): |
| + """Installs KVM packages.""" |
| + (output, stderr, rc) = RunCommand(['sudo', 'apt-get', 'install', 'kvm']) |
| + if rc: |
| + logging.critical('ERROR: Did not install KVM. Make sure Intel KVM is \ |
| +enabled in BIOS.') |
| + print '%s\n%s' % output, stderr |
| + (output, stderr, rc) = RunCommand(['sudo', 'modprobe', 'kvm-intel']) |
| + if rc: |
| + logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure \ |
| +Intel KVM is enabled in BIOS.') |
| + print '%s\n%s' % output, stderr |
| + # Now check to ensure KVM acceleration can be used. |
| + (output, stderr, rc) = RunCommand(['kvm-ok']) |
| + if rc: |
| + logging.critical('ERROR: Can not use KVM acceleration. Make sure Intel KVM \ |
| +is enabled in BIOS.') |
| + print '%s\n%s' % output, stderr |
| + |
| + |
| +def GetX86Image(): |
| + """Download x86 system image from Intel's website. |
| + |
| + Returns: |
| + true if x86 system image successfully downloaded, false otherwise |
| + """ |
| + logging.info('Download x86 system image directory into sdk directory.') |
| + try: |
| + RunCommand(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) |
| + (_, stderr, rc) = RunCommand(['unzip', '-o', '/tmp/x86_img.zip', '-d', |
| + '/tmp/']) |
| + if rc: |
| + print stderr |
| + raise |
| + sys_imgs = os.path.join(os.getcwd(), 'android_tools', 'sdk', |
| + 'system-images', API_TARGET, 'x86') |
| + shutil.move('/tmp/x86', sys_imgs) |
| + except: |
| + logging.critical('ERROR: Could not download x86 image.') |
| + return False |
| + return True |
| + |
| + |
| +def main(argv): |
| + # Run script from parent directory of chrome checkout, because we do not want |
| + # to put SDK and system images into chrome checkout. |
| + chrome_root = os.environ.get('CHROME_SRC') |
|
pasko-google - do not use
2013/03/14 15:46:28
Another possibility is to take the path to the scr
navabi
2013/03/20 21:40:16
Done. The code is already there below with new_cwd
|
| + new_cwd = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', |
| + '..', '..') |
| + if chrome_root: |
| + new_cwd = os.path.join(chrome_root, '..', '..') |
| + else: |
| + logging.critical('Need to run envsetup from src dir (i.e. \ |
|
pasko-google - do not use
2013/03/14 15:46:28
more pythonic style is instead of escaping to star
navabi
2013/03/20 21:40:16
Thanks. This has been removed because of the above
|
| +build/android/envsetup.sh <options>)') |
| + return 1 |
| + |
| + os.chdir(new_cwd) |
| + |
| + # Need to set ANDROID_SDK_ROOT to find AVD's in case it is set by envsetup.sh |
|
pasko-google - do not use
2013/03/14 15:46:28
should it be set in this script? Does the emulator
navabi
2013/03/20 21:40:16
Yes, the emulator refuses to work without it. The
|
| + emulator_sdk = os.path.join(new_cwd, 'android_tools', 'sdk') |
| + os.environ['ANDROID_SDK_ROOT'] = emulator_sdk |
| + |
| + # Parse aguments after the python script |
| + args = argv[1:] |
| + opt_parser = optparse.OptionParser(description='AVD script.') |
| + opt_parser.add_option('-n', '--num', dest='emulator_count', |
| + help='Number of emulators to launch.', |
| + type='int', default='1') |
| + opt_parser.add_option('--abi', default='arm', |
| + help='Platform of emulators to launch.') |
| + |
| + options, _ = opt_parser.parse_args(args) |
|
pasko-google - do not use
2013/03/14 15:46:28
better to avoid defining a new variable:
options,
navabi
2013/03/20 21:40:16
Done.
|
| + if options.abi == 'arm': |
| + options.abi = 'armeabi-v7a' |
| + |
| + logging.basicConfig(level=logging.INFO, |
| + format='# %(asctime)-15s: %(message)s') |
| + logging.root.setLevel(logging.INFO) |
| + |
| + # Calls below will download emulator SDK and/or system images only if needed. |
| + if CheckSDK(): |
| + logging.info('android_tools directory already exists (not downloading).') |
| + elif not GetSDK(): |
| + # Can not continue without downloading SDK. |
| + return 1 |
| + if CheckX86Image(): |
| + logging.info('system-images directory already exists.') |
| + else: |
| + GetX86Image() |
| + |
| + # Make sure KVM packages are installed and enabled. |
| + if CheckKVM(): |
|
pasko-google - do not use
2013/03/14 15:46:28
should have something more granular: fail early if
navabi
2013/03/20 21:40:16
Moved installation stuff to different file: instal
|
| + logging.info('KVM already installed and enabled.') |
| + else: |
| + InstallKVM() |
| + |
| + emulator.LaunchEmulators(options.emulator_count, options.abi, True) |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv)) |