Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 """Installs deps for using SDK emulator for testing. | |
| 7 | |
| 8 The script will download the SDK and system images, if they are not present, and | |
| 9 install and enable KVM, if virtualization has been enabled in the BIOS. | |
| 10 """ | |
| 11 | |
| 12 | |
| 13 import logging | |
| 14 import os | |
| 15 import shutil | |
| 16 import subprocess | |
| 17 import sys | |
| 18 | |
| 19 from pylib import cmd_helper | |
| 20 from pylib import constants | |
| 21 from pylib.utils import run_tests_helper | |
| 22 | |
| 23 # From the Android Developer's website. | |
| 24 SDK_BASE_URL = 'http://dl.google.com/android/adt' | |
| 25 SDK_ZIP = 'adt-bundle-linux-x86_64-20130219.zip' | |
| 26 | |
| 27 # Android x86 system image from the Intel website: | |
| 28 # http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean -bin | |
| 29 X86_IMG_URL = 'http://download-software.intel.com/sites/landingpage/android/sysi mg_x86-17_r01.zip' | |
| 30 | |
| 31 # Android API level | |
| 32 API_TARGET = 'android-%s' % constants.ANDROID_SDK_VERSION | |
| 33 | |
| 34 | |
| 35 class EmulatorInstallException(Exception): | |
| 36 """Emulator failed to launch.""" | |
| 37 pass | |
| 38 | |
| 39 def CheckSDK(): | |
| 40 """Check if SDK is already installed. | |
| 41 | |
| 42 Returns: | |
| 43 True if android_tools directory exists in current directory. | |
| 44 """ | |
| 45 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, | |
| 46 'android_tools')) | |
| 47 | |
| 48 | |
| 49 def CheckX86Image(): | |
| 50 """Check if Android system images have been installed. | |
| 51 | |
| 52 Returns: | |
| 53 True if android_tools/sdk/system-images directory exists. | |
| 54 """ | |
| 55 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, | |
| 56 'android_tools', 'sdk', 'system-images', | |
| 57 API_TARGET, 'x86')) | |
| 58 | |
| 59 | |
| 60 def CheckKVM(): | |
| 61 """Check if KVM is enabled. | |
| 62 | |
| 63 Returns: | |
| 64 True if kvm-ok returns 0 (already enabled) | |
| 65 """ | |
| 66 rc = cmd_helper.RunCmd(['kvm-ok']) | |
| 67 return not rc | |
| 68 | |
| 69 | |
| 70 def GetSDK(): | |
| 71 """Download the SDK and unzip in android_tools directory. | |
| 72 | |
| 73 Raises: | |
| 74 EmulatorInstallException if fails | |
| 75 """ | |
| 76 logging.info('Download Android SDK.') | |
| 77 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP) | |
| 78 try: | |
| 79 cmd_helper.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url]) | |
| 80 print 'curled unzipping...' | |
| 81 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/']) | |
| 82 if rc: | |
| 83 logging.critical('ERROR: could not download/unzip Android SDK.') | |
| 84 raise | |
| 85 # Get the name of the sub-directory that everything will be extracted to. | |
| 86 dirname, _ = os.path.splitext(SDK_ZIP) | |
| 87 zip_dir = '/tmp/%s' % dirname | |
| 88 # Move the extracted directory to EMULATOR_SDK_ROOT | |
| 89 dst = os.path.join(constants.EMULATOR_SDK_ROOT, 'android_tools') | |
| 90 shutil.move(zip_dir, dst) | |
| 91 except: | |
|
frankf
2013/03/27 18:12:14
This is dangerous as it hides any exceptions inclu
navabi
2013/03/27 19:07:44
Done.
| |
| 92 raise EmulatorInstallException('Could not download Android SDK.') | |
| 93 finally: | |
| 94 os.unlink('/tmp/sdk.zip') | |
| 95 | |
| 96 | |
| 97 def InstallKVM(): | |
| 98 """Installs KVM packages.""" | |
| 99 rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm']) | |
| 100 if rc: | |
| 101 logging.critical('ERROR: Did not install KVM. Make sure hardware ' | |
| 102 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' | |
| 103 'AMD SVM).') | |
| 104 # TODO(navabi): Use modprobe kvm-amd on AMD processors. | |
| 105 rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel']) | |
| 106 if rc: | |
| 107 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure ' | |
| 108 'hardware virtualization is enabled in BIOS.') | |
| 109 # Now check to ensure KVM acceleration can be used. | |
| 110 rc = cmd_helper.RunCmd(['kvm-ok']) | |
| 111 if rc: | |
| 112 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' | |
| 113 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' | |
| 114 'AMD SVM).') | |
| 115 | |
| 116 | |
| 117 def GetX86Image(): | |
| 118 """Download x86 system image from Intel's website. | |
| 119 | |
| 120 Raises:: | |
| 121 EmulatorInstallException if fails | |
| 122 """ | |
| 123 logging.info('Download x86 system image directory into sdk directory.') | |
| 124 try: | |
| 125 cmd_helper.RunCmd(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) | |
| 126 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/x86_img.zip', '-d', '/tmp/']) | |
| 127 if rc: | |
| 128 logging.critical('ERROR: Could not download/unzip image zip.') | |
| 129 raise | |
| 130 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'android_tools', 'sdk', | |
| 131 'system-images', API_TARGET, 'x86') | |
| 132 shutil.move('/tmp/x86', sys_imgs) | |
| 133 except: | |
| 134 raise EmulatorInstallException('Could not download x86 system image.') | |
| 135 finally: | |
| 136 os.unlink('/tmp/x86_img.zip') | |
| 137 | |
| 138 | |
| 139 def main(argv): | |
| 140 logging.basicConfig(level=logging.INFO, | |
| 141 format='# %(asctime)-15s: %(message)s') | |
| 142 run_tests_helper.SetLogLevel(verbose_count=1) | |
| 143 | |
| 144 # Calls below will download emulator SDK and/or system images only if needed. | |
| 145 if CheckSDK(): | |
| 146 logging.info('android_tools directory already exists (not downloading).') | |
| 147 else: | |
| 148 GetSDK() | |
| 149 | |
| 150 if CheckX86Image(): | |
| 151 logging.info('system-images directory already exists.') | |
| 152 else: | |
| 153 GetX86Image() | |
| 154 | |
| 155 # Make sure KVM packages are installed and enabled. | |
| 156 if CheckKVM(): | |
| 157 logging.info('KVM already installed and enabled.') | |
| 158 else: | |
| 159 InstallKVM() | |
| 160 | |
| 161 | |
| 162 if __name__ == '__main__': | |
| 163 sys.exit(main(sys.argv)) | |
| OLD | NEW |