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
8The script will launch a specified number of Android Virtual Devices (AVD's).
9"""
10
11
12import install_emulator_deps
13import logging
14import optparse
15import os
16import re
17import sys
18
19from pylib import cmd_helper
20from pylib import constants
21from pylib.utils import emulator
22
23
24def 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.
27  emulator_sdk = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk')
28  os.environ['ANDROID_SDK_ROOT'] = emulator_sdk
29
30  opt_parser = optparse.OptionParser(description='AVD script.')
31  opt_parser.add_option('--name', help='Optinaly, name of existing AVD to '
32                        'launch. If not specified, new AVD\'s will be created')
33  opt_parser.add_option('-n', '--num', dest='emulator_count',
34                        help='Number of emulators to launch (default is 1).',
35                        type='int', default='1')
36  opt_parser.add_option('--abi', default='x86',
37                        help='Platform of emulators to launch (x86 default).')
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)
41
42  options, _ = opt_parser.parse_args(argv[1:])
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.
49  # TODO(andrewhayden) Since we can fix all of these with install_emulator_deps
50  # why don't we just run it?
51  if options.abi == 'x86':
52    if not install_emulator_deps.CheckKVM():
53      logging.critical('ERROR: KVM must be enabled in BIOS, and installed. '
54                       'Enable KVM in BIOS and run install_emulator_deps.py')
55      return 1
56    elif not install_emulator_deps.CheckX86Image(options.api_level):
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
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.
68  api_level = options.api_level
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'])
73    names = re.findall('Name: (\w+)', avds_output)
74    api_levels = re.findall('API level (\d+)', avds_output)
75    try:
76      avd_index = names.index(options.name)
77    except ValueError:
78      logging.critical('ERROR: Specified AVD %s does not exist.' % options.name)
79      return 1
80    api_level = int(api_levels[avd_index])
81
82  if not install_emulator_deps.CheckSDKPlatform(api_level):
83    logging.critical('ERROR: Emulator SDK missing required target for API %d. '
84                     'Run install_emulator_deps.py.')
85    return 1
86
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
93
94
95if __name__ == '__main__':
96  sys.exit(main(sys.argv))
97