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 def CheckSDK(): |
| 36 """Check if SDK is already installed. |
| 37 |
| 38 Returns: |
| 39 True if android_tools directory exists in current directory. |
| 40 """ |
| 41 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, |
| 42 'android_tools')) |
| 43 |
| 44 |
| 45 def CheckX86Image(): |
| 46 """Check if Android system images have been installed. |
| 47 |
| 48 Returns: |
| 49 True if android_tools/sdk/system-images directory exists. |
| 50 """ |
| 51 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, |
| 52 'android_tools', 'sdk', 'system-images', |
| 53 API_TARGET, 'x86')) |
| 54 |
| 55 |
| 56 def CheckKVM(): |
| 57 """Check if KVM is enabled. |
| 58 |
| 59 Returns: |
| 60 True if kvm-ok returns 0 (already enabled) |
| 61 """ |
| 62 rc = cmd_helper.RunCmd(['kvm-ok']) |
| 63 return not rc |
| 64 |
| 65 |
| 66 def GetSDK(): |
| 67 """Download the SDK and unzip in android_tools directory.""" |
| 68 logging.info('Download Android SDK.') |
| 69 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP) |
| 70 try: |
| 71 cmd_helper.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url]) |
| 72 print 'curled unzipping...' |
| 73 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/']) |
| 74 if rc: |
| 75 logging.critical('ERROR: could not download/unzip Android SDK.') |
| 76 raise |
| 77 # Get the name of the sub-directory that everything will be extracted to. |
| 78 dirname, _ = os.path.splitext(SDK_ZIP) |
| 79 zip_dir = '/tmp/%s' % dirname |
| 80 # Move the extracted directory to EMULATOR_SDK_ROOT |
| 81 dst = os.path.join(constants.EMULATOR_SDK_ROOT, 'android_tools') |
| 82 shutil.move(zip_dir, dst) |
| 83 finally: |
| 84 os.unlink('/tmp/sdk.zip') |
| 85 |
| 86 |
| 87 def InstallKVM(): |
| 88 """Installs KVM packages.""" |
| 89 rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm']) |
| 90 if rc: |
| 91 logging.critical('ERROR: Did not install KVM. Make sure hardware ' |
| 92 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' |
| 93 'AMD SVM).') |
| 94 # TODO(navabi): Use modprobe kvm-amd on AMD processors. |
| 95 rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel']) |
| 96 if rc: |
| 97 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure ' |
| 98 'hardware virtualization is enabled in BIOS.') |
| 99 # Now check to ensure KVM acceleration can be used. |
| 100 rc = cmd_helper.RunCmd(['kvm-ok']) |
| 101 if rc: |
| 102 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' |
| 103 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' |
| 104 'AMD SVM).') |
| 105 |
| 106 |
| 107 def GetX86Image(): |
| 108 """Download x86 system image from Intel's website.""" |
| 109 logging.info('Download x86 system image directory into sdk directory.') |
| 110 try: |
| 111 cmd_helper.RunCmd(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) |
| 112 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/x86_img.zip', '-d', '/tmp/']) |
| 113 if rc: |
| 114 logging.critical('ERROR: Could not download/unzip image zip.') |
| 115 raise |
| 116 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'android_tools', 'sdk', |
| 117 'system-images', API_TARGET, 'x86') |
| 118 shutil.move('/tmp/x86', sys_imgs) |
| 119 finally: |
| 120 os.unlink('/tmp/x86_img.zip') |
| 121 |
| 122 |
| 123 def main(argv): |
| 124 logging.basicConfig(level=logging.INFO, |
| 125 format='# %(asctime)-15s: %(message)s') |
| 126 run_tests_helper.SetLogLevel(verbose_count=1) |
| 127 |
| 128 # Calls below will download emulator SDK and/or system images only if needed. |
| 129 if CheckSDK(): |
| 130 logging.info('android_tools directory already exists (not downloading).') |
| 131 else: |
| 132 GetSDK() |
| 133 |
| 134 if CheckX86Image(): |
| 135 logging.info('system-images directory already exists.') |
| 136 else: |
| 137 GetX86Image() |
| 138 |
| 139 # Make sure KVM packages are installed and enabled. |
| 140 if CheckKVM(): |
| 141 logging.info('KVM already installed and enabled.') |
| 142 else: |
| 143 InstallKVM() |
| 144 |
| 145 |
| 146 if __name__ == '__main__': |
| 147 sys.exit(main(sys.argv)) |
OLD | NEW |