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 import argparse 12 import logging 13 import os 14 import re 15 import sys 16 17 import devil_chromium 18 import install_emulator_deps 19 20 from devil.utils import cmd_helper 21 from pylib import constants 22 from pylib.utils import emulator 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. 27 emulator_sdk = constants.ANDROID_SDK_ROOT 28 os.environ['ANDROID_SDK_ROOT'] = emulator_sdk 29 30 arg_parser = argparse.ArgumentParser(description='AVD script.') 31 sub_parsers = arg_parser.add_subparsers(title='subparser', dest='command') 32 sub_parsers.add_parser( 33 'kill', help='Shutdown all existing emulators') 34 sub_parsers.add_parser( 35 'delete', help='Deleting all the avd files') 36 wait_parser = sub_parsers.add_parser( 37 'wait', help='Wait for emulators to finish booting') 38 wait_parser.add_argument('-n', '--num', dest='wait_num', 39 help='Number of emulators to wait for', type=int, 40 default=1) 41 run_parser = sub_parsers.add_parser('run', help='Run emulators') 42 run_parser.add_argument('--name', help='Optinaly, name of existing AVD to ' 43 'launch. If not specified, AVD\'s will be created') 44 run_parser.add_argument('-n', '--num', dest='emulator_count', 45 help='Number of emulators to launch (default is 1).', 46 type=int, default='1') 47 run_parser.add_argument('--abi', default='x86', 48 help='Platform of emulators to launch (x86 default)') 49 run_parser.add_argument('--api-level', dest='api_level', 50 help='API level for the image', 51 type=int, default=constants.ANDROID_SDK_VERSION) 52 run_parser.add_argument('--sdcard-size', dest='sdcard_size', 53 default=emulator.DEFAULT_SDCARD_SIZE, 54 help='Set sdcard size of the emulators' 55 ' e.g. --sdcard-size=512M') 56 run_parser.add_argument('--partition-size', dest='partition_size', 57 default=emulator.DEFAULT_STORAGE_SIZE, 58 help='Default internal storage size' 59 ' e.g. --partition-size=1024M') 60 run_parser.add_argument('--launch-without-kill', action='store_false', 61 dest='kill_and_launch', default=True, 62 help='Kill all emulators at launch') 63 run_parser.add_argument('--enable-kvm', action='store_true', 64 dest='enable_kvm', default=False, 65 help='Enable kvm for faster x86 emulator run') 66 run_parser.add_argument('--headless', action='store_true', 67 dest='headless', default=False, 68 help='Launch an emulator with no UI.') 69 70 arguments = arg_parser.parse_args(argv[1:]) 71 72 logging.root.setLevel(logging.INFO) 73 74 devil_chromium.Initialize() 75 76 if arguments.command == 'kill': 77 logging.info('Killing all existing emulator and existing the program') 78 emulator.KillAllEmulators() 79 elif arguments.command == 'delete': 80 emulator.DeleteAllTempAVDs() 81 elif arguments.command == 'wait': 82 emulator.WaitForEmulatorLaunch(arguments.wait_num) 83 else: 84 # Check if SDK exist in ANDROID_SDK_ROOT 85 if not install_emulator_deps.CheckSDK(): 86 raise Exception('Emulator SDK not installed in %s' 87 % constants.ANDROID_SDK_ROOT) 88 89 # Check if KVM is enabled for x86 AVD 90 if arguments.abi == 'x86': 91 if not install_emulator_deps.CheckKVM(): 92 logging.warning('KVM is not installed or enabled') 93 arguments.enable_kvm = False 94 95 # Check if targeted system image exist 96 if not install_emulator_deps.CheckSystemImage(arguments.abi, 97 arguments.api_level): 98 logging.critical('ERROR: System image for %s AVD not installed. Run ' 99 'install_emulator_deps.py', arguments.abi) 100 return 1 101 102 # If AVD is specified, check that the SDK has the required target. If not, 103 # check that the SDK has the desired target for the temporary AVD's. 104 api_level = arguments.api_level 105 if arguments.name: 106 android = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 107 'android') 108 avds_output = cmd_helper.GetCmdOutput([android, 'list', 'avd']) 109 names = re.findall(r'Name: (\w+)', avds_output) 110 api_levels = re.findall(r'API level (\d+)', avds_output) 111 try: 112 avd_index = names.index(arguments.name) 113 except ValueError: 114 logging.critical('ERROR: Specified AVD %s does not exist.', 115 arguments.name) 116 return 1 117 api_level = int(api_levels[avd_index]) 118 119 if not install_emulator_deps.CheckSDKPlatform(api_level): 120 logging.critical('ERROR: Emulator SDK missing required target for API %d.' 121 ' Run install_emulator_deps.py.') 122 return 1 123 124 if arguments.name: 125 emulator.LaunchEmulator( 126 arguments.name, 127 arguments.abi, 128 enable_kvm=arguments.enable_kvm, 129 kill_and_launch=arguments.reset_and_launch, 130 sdcard_size=arguments.sdcard_size, 131 storage_size=arguments.partition_size, 132 headless=arguments.headless 133 ) 134 else: 135 emulator.LaunchTempEmulators( 136 arguments.emulator_count, 137 arguments.abi, 138 arguments.api_level, 139 enable_kvm=arguments.enable_kvm, 140 kill_and_launch=arguments.kill_and_launch, 141 sdcard_size=arguments.sdcard_size, 142 storage_size=arguments.partition_size, 143 wait_for_boot=True, 144 headless=arguments.headless 145 ) 146 logging.info('Emulator launch completed') 147 return 0 148 149 if __name__ == '__main__': 150 sys.exit(main(sys.argv)) 151