[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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. |
| 5 | |
| 6 | """Launches Android Virtual Devices with a set configuration for testing Chrome. |
| 7 | |
| 8 | The script will launch a specified number of Android Virtual Devices (AVD's). |
| 9 | """ |
| 10 | |
| 11 | |
| 12 | import install_emulator_deps |
| 13 | import logging |
| 14 | import optparse |
| 15 | import os |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 16 | import re |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 17 | import sys |
| 18 | |
jbudorick | 06162944 | 2015-09-03 18:00:57 | [diff] [blame] | 19 | from devil.utils import cmd_helper |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 20 | from pylib import constants |
| 21 | from pylib.utils import emulator |
| 22 | |
| 23 | |
| 24 | def main(argv): |
| 25 | # ANDROID_SDK_ROOT needs to be set to the location of the SDK used to launch |
| 26 | # the emulator to find the system images upon launch. |
[email protected] | 5844a6d | 2013-10-04 23:01:59 | [diff] [blame] | 27 | emulator_sdk = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk') |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 28 | os.environ['ANDROID_SDK_ROOT'] = emulator_sdk |
| 29 | |
| 30 | opt_parser = optparse.OptionParser(description='AVD script.') |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 31 | opt_parser.add_option('--name', help='Optinaly, name of existing AVD to ' |
| 32 | 'launch. If not specified, new AVD\'s will be created') |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 33 | opt_parser.add_option('-n', '--num', dest='emulator_count', |
[email protected] | 4dc4aea | 2013-04-10 01:55:31 | [diff] [blame] | 34 | help='Number of emulators to launch (default is 1).', |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 35 | type='int', default='1') |
[email protected] | 4dc4aea | 2013-04-10 01:55:31 | [diff] [blame] | 36 | opt_parser.add_option('--abi', default='x86', |
| 37 | help='Platform of emulators to launch (x86 default).') |
[email protected] | d5a6fd25 | 2013-11-20 10:39:48 | [diff] [blame] | 38 | opt_parser.add_option('--api-level', dest='api_level', |
| 39 | help='API level for the image, e.g. 19 for Android 4.4', |
| 40 | type='int', default=constants.ANDROID_SDK_VERSION) |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 41 | |
| 42 | options, _ = opt_parser.parse_args(argv[1:]) |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 43 | |
| 44 | logging.basicConfig(level=logging.INFO, |
| 45 | format='# %(asctime)-15s: %(message)s') |
| 46 | logging.root.setLevel(logging.INFO) |
| 47 | |
| 48 | # Check if KVM is enabled for x86 AVD's and check for x86 system images. |
[email protected] | d5a6fd25 | 2013-11-20 10:39:48 | [diff] [blame] | 49 | # TODO(andrewhayden) Since we can fix all of these with install_emulator_deps |
| 50 | # why don't we just run it? |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 51 | if options.abi == 'x86': |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 52 | if not install_emulator_deps.CheckKVM(): |
[email protected] | 4dc4aea | 2013-04-10 01:55:31 | [diff] [blame] | 53 | logging.critical('ERROR: KVM must be enabled in BIOS, and installed. ' |
| 54 | 'Enable KVM in BIOS and run install_emulator_deps.py') |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 55 | return 1 |
[email protected] | d5a6fd25 | 2013-11-20 10:39:48 | [diff] [blame] | 56 | elif not install_emulator_deps.CheckX86Image(options.api_level): |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 57 | logging.critical('ERROR: System image for x86 AVD not installed. Run ' |
| 58 | 'install_emulator_deps.py') |
| 59 | return 1 |
| 60 | |
| 61 | if not install_emulator_deps.CheckSDK(): |
| 62 | logging.critical('ERROR: Emulator SDK not installed. Run ' |
| 63 | 'install_emulator_deps.py.') |
| 64 | return 1 |
| 65 | |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 66 | # If AVD is specified, check that the SDK has the required target. If not, |
| 67 | # check that the SDK has the desired target for the temporary AVD's. |
[email protected] | 58f6bb9 | 2014-03-12 10:41:24 | [diff] [blame] | 68 | api_level = options.api_level |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 69 | if options.name: |
| 70 | android = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk', 'tools', |
| 71 | 'android') |
| 72 | avds_output = cmd_helper.GetCmdOutput([android, 'list', 'avd']) |
jbudorick | 0f77e96 | 2014-11-19 15:41:29 | [diff] [blame] | 73 | names = re.findall(r'Name: (\w+)', avds_output) |
| 74 | api_levels = re.findall(r'API level (\d+)', avds_output) |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 75 | try: |
| 76 | avd_index = names.index(options.name) |
| 77 | except ValueError: |
jbudorick | 58b4d36 | 2015-09-08 16:44:59 | [diff] [blame] | 78 | logging.critical('ERROR: Specified AVD %s does not exist.', options.name) |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 79 | return 1 |
[email protected] | 00dc3f895 | 2014-03-12 01:58:05 | [diff] [blame] | 80 | api_level = int(api_levels[avd_index]) |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 81 | |
| 82 | if not install_emulator_deps.CheckSDKPlatform(api_level): |
[email protected] | d5a6fd25 | 2013-11-20 10:39:48 | [diff] [blame] | 83 | logging.critical('ERROR: Emulator SDK missing required target for API %d. ' |
| 84 | 'Run install_emulator_deps.py.') |
| 85 | return 1 |
| 86 | |
[email protected] | 2ae433d | 2014-03-11 03:00:38 | [diff] [blame] | 87 | if options.name: |
| 88 | emulator.LaunchEmulator(options.name, options.abi) |
| 89 | else: |
| 90 | emulator.LaunchTempEmulators(options.emulator_count, options.abi, |
| 91 | options.api_level, True) |
| 92 | |
[email protected] | 8afcc5a | 2013-03-28 00:23:22 | [diff] [blame] | 93 | |
| 94 | |
| 95 | if __name__ == '__main__': |
| 96 | sys.exit(main(sys.argv)) |