Index: build/android/avd.py |
diff --git a/build/android/avd.py b/build/android/avd.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..04988b98648d9a11d40776fcf16e717520833c97 |
--- /dev/null |
+++ b/build/android/avd.py |
@@ -0,0 +1,219 @@ |
+#!/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. |
+ |
+"""Provides utilities for testing Chrome on Android on AVD's (i.e. emulators). |
+ |
+The purpose of this script is to automate everything from downloading the SDK, |
+system images and KVM packages to creating and running emulators (i.e. Android |
+Virutal Devices) with GPU acceleration and KVM support. |
+""" |
+ |
+ |
+import getpass |
+import logging |
+import optparse |
+import os |
+import pexpect |
+import subprocess |
+import shutil |
+import sys |
+ |
+ |
+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): stdout and stderr |
+ """ |
+ logging.info('Running command: %s' % args) |
+ proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
+ output, stderr = proc.communicate() |
+ return (str(output).strip(), stderr) |
+ |
+ |
+def CheckSDK(): |
+ """Check if SDK already installed. |
+ |
+ Returns: |
+ true if android_tools directory exists in current directory. |
+ """ |
+ return os.path.exists(os.path.join(os.getcwd(), 'android_tools')) |
+ |
+ |
+def CheckImages(): |
+ """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')) |
+ |
+ |
+def GetSDK(): |
+ """Checks out android_tools from the chromium git repo.""" |
+ if CheckSDK(): |
+ logging.info('android_tools directory already exists (not checking out).') |
+ else: |
+ user = getpass.getuser() |
+ repo_url = 'ssh://%s@gerrit.chromium.org:29418/android_tools.git' % user |
+ logging.info('Checking out SDK: git clone %s ...', repo_url) |
+ (output, stderr) = RunCommand(['git', 'clone', repo_url]) |
+ if stderr: |
+ logging.critical('ERROR: unable to checkout android_tools: %s', output) |
+ logging.info('Successfully checked out SDK. Open tools/android and \ |
+download system images.') |
+ |
+ |
+def InstallKVM(): |
+ """Installs KVM packages.""" |
+ (output, stderr) = RunCommand(['sudo', 'apt-get', 'install', 'kvm']) |
+ if stderr: |
+ logging.critical('ERROR: Did not install KVM. Make sure Intel KVM is \ |
+enabled in BIOS.') |
+ print output |
+ (output, stderr) = RunCommand(['sudo', 'modprobe', 'kvm-intel']) |
+ if stderr: |
+ logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure \ |
+Intel KVM is enabled in BIOS.') |
+ print output |
+ # Now check to ensure KVM acceleration can be used. |
+ (output, stderr) = RunCommand(['kvm-ok']) |
+ if stderr: |
+ logging.critical('ERROR: Can not use KVM acceleration. Make sure Intel KVM \ |
+is enabled in BIOS.') |
+ print output |
+ |
+ |
+def CopySystemImages(): |
+ """Copies system images from navabi shared directory into sdk.""" |
navabi
2013/03/05 08:09:49
Put this in public place instead of my shared dire
|
+ if CheckImages(): |
+ logging.info('system-images directory already exists.') |
+ else: |
+ logging.info('Copying system-images directory into sdk directory') |
+ # TODO(navabi): Put in public place so non-googlers can download |
+ shutil.copytree('/home/navabi/www/system-images', |
+ os.path.join(os.getcwd(), |
+ 'android_tools/sdk/system-images')) |
+ |
+ |
+def CreateAVD(avd_name, abi): |
+ """Creates and calls a bash script to call android create avd.""" |
+ logging.info('Creating AVD with name %s...', avd_name) |
+ android_bin = os.path.join(os.getcwd(), 'android_tools', 'sdk', 'tools', |
+ 'android') |
+ avd_dir = os.path.join(os.getcwd(), 'avds', avd_name) |
+ |
+ create_cmd = ' '.join([android_bin, 'create', 'avd', '-n', avd_name, '-t', |
+ '1', '--abi', abi, '-p', avd_dir, '--force']) |
+ logging.info('Running command: %s' % create_cmd) |
+ |
+ create = pexpect.spawn(create_cmd) |
+ # TODO(navabi): use expect and sendline to specify all custom options |
+ # create.interact() |
+ # Read and print the Android version info |
+ print create.readline() |
+ |
+ # Define answers to AVD hardware customization options. Empty string indicates |
+ # default option is used. |
+ answers = ['yes', '', '', '', '', '', '', '128', '', '', '', '', '', '', '', |
+ '', '', '', '', '', '', '', '', 'yes', '', '', '', '', '', '', '', |
+ '', '', '', '128', '', '', '', '', '', '', '', '', '', '', ''] |
+ |
+ for answer in answers: |
+ create.expect(r'\]') |
+ create.sendline(answer) |
+ # print create.before + ']' |
+ |
+ create.expect(pexpect.EOF) |
+ # Print hardware configuration for created AVD |
+ print create.before |
+ |
+ |
+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') |
+ new_cwd = os.path.join(os.getcwd(), '..', '..', '..', '..') |
navabi
2013/03/06 01:30:27
Fix: This should be set not relative to os.getcwd(
|
+ if chrome_root: |
+ new_cwd = os.path.join(chrome_root, '..', '..') |
+ else: |
+ logging.warning('CHROME_SRC not set (run envsetup.sh). Using relative path \ |
+from script location instead.') |
+ |
+ os.chdir(new_cwd) |
+ |
+ # Need to set ANDROID_SDK_ROOT to find AVD's in case it is set by envsetup.sh |
+ emulator_sdk = os.path.join(new_cwd, 'android_tools', 'sdk') |
+ os.environ['ANDROID_SDK_ROOT'] = "%s:$PATH" % emulator_sdk |
+ |
+ # Parse aguments after the python script |
+ args = argv[1:] |
+ opt_parser = optparse.OptionParser(description='AVD script.') |
+ |
+ opt_parser.add_option('--get-sdk', action='store_true', default=False, |
+ help='download the sdk and install images') |
+ opt_parser.add_option('--download-images', action='store_true', default=False, |
+ help='install Android system images') |
+ |
+ opt_parser.add_option('--install-kvm', action='store_true', default=False, |
+ help='install Intel KVM assuming enabled in BIOS.') |
+ |
+ opt_parser.add_option('--new-avd', help='create new AVD with given name.') |
+ opt_parser.add_option('--abi', help='specify abi version for AVD') |
+ |
+ opt_parser.add_option('--list-avds', action='store_true', default=False, |
+ help='list AVDs created by this script.') |
+ |
+ opt_parser.add_option('--start-avd', |
+ help='start an AVD that has been created.') |
+ |
+ options, _ = opt_parser.parse_args(args) |
+ |
+ logging.basicConfig(level=logging.INFO, |
+ format='# %(asctime)-15s: %(message)s') |
+ logging.root.setLevel(logging.INFO) |
+ |
+ if options.get_sdk: |
+ # If we are downloading the SDK, also go ahead and install system images |
+ GetSDK() |
+ CopySystemImages() |
+ elif options.install_kvm: |
+ InstallKVM() |
+ elif options.download_images: |
+ CopySystemImages() |
+ elif options.list_avds: |
+ (output, _) = RunCommand(['ls', 'avds/']) |
+ print output |
+ elif options.new_avd: |
+ if not CheckSDK(): |
+ logging.error('Can not create AVD. First get sdk (i.e. --get-sdk).') |
+ elif not CheckImages(): |
+ logging.error('Can not create AVD. First download system images (i.e. \ |
+--download-images).') |
+ elif options.new_avd is None: |
+ logging.error('Please specify name of AVD (e.g. --create-avd myavd).') |
+ elif options.abi != 'arm' and options.abi != 'x86': |
+ logging.error('Specify --abi=ABI (Valid ABIs: arm, x86)') |
+ else: |
+ if options.abi == 'arm': |
+ options.abi = 'armeabi-v7a' |
+ CreateAVD(options.new_avd, options.abi) |
+ elif options.start_avd: |
+ emulator_bin = os.path.join(os.getcwd(), 'android_tools', 'sdk', 'tools', |
+ 'emulator') |
+ (output, stderr) = RunCommand([emulator_bin, '-avd', options.start_avd, |
+ '-gpu', 'on', '-qemu', '-m', '128']) |
+ print stderr |
+ print output |
+ else: |
+ logging.info('Run script with --help for options.') |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(sys.argv)) |