summaryrefslogtreecommitdiffstats
path: root/build/android/install_emulator_deps.py
blob: 08f0f368a4087fc63d3cec9e60a2aba7fe160356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Installs deps for using SDK emulator for testing.

The script will download the SDK and system images, if they are not present, and
install and enable KVM, if virtualization has been enabled in the BIOS.
"""


import logging
import os
import shutil
import sys

from pylib import cmd_helper
from pylib import constants
from pylib.utils import run_tests_helper

# From the Android Developer's website.
SDK_BASE_URL = 'http://dl.google.com/android/adt'
SDK_ZIP = 'adt-bundle-linux-x86_64-20130729.zip'

# Android x86 system image from the Intel website:
# http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean-bin
X86_IMG_URL = 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-18_r01.zip'

# Android API level
API_TARGET = 'android-%s' % constants.ANDROID_SDK_VERSION


def CheckSDK():
  """Check if SDK is already installed.

  Returns:
    True if the emulator SDK directory (src/android_emulator_sdk/) exists.
  """
  return os.path.exists(constants.EMULATOR_SDK_ROOT)


def CheckX86Image():
  """Check if Android system images have been installed.

  Returns:
    True if sdk/system-images/<API TARGET>/x86 exists inside EMULATOR_SDK_ROOT.
  """
  return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT,
                                     'sdk', 'system-images',
                                     API_TARGET, 'x86'))


def CheckKVM():
  """Check if KVM is enabled.

  Returns:
    True if kvm-ok returns 0 (already enabled)
  """
  try:
    return not cmd_helper.RunCmd(['kvm-ok'])
  except OSError:
    logging.info('kvm-ok not installed')
    return False


def GetSDK():
  """Download the SDK and unzip it into EMULATOR_SDK_ROOT."""
  logging.info('Download Android SDK.')
  sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP)
  try:
    cmd_helper.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url])
    print 'curled unzipping...'
    rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/'])
    if rc:
      raise Exception('ERROR: could not download/unzip Android SDK.')
    # Get the name of the sub-directory that everything will be extracted to.
    dirname, _ = os.path.splitext(SDK_ZIP)
    zip_dir = '/tmp/%s' % dirname
    # Move the extracted directory to EMULATOR_SDK_ROOT
    shutil.move(zip_dir, constants.EMULATOR_SDK_ROOT)
  finally:
    os.unlink('/tmp/sdk.zip')


def InstallKVM():
  """Installs KVM packages."""
  rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
  if rc:
    logging.critical('ERROR: Did not install KVM. Make sure hardware '
                     'virtualization is enabled in BIOS (i.e. Intel VT-x or '
                     'AMD SVM).')
  # TODO(navabi): Use modprobe kvm-amd on AMD processors.
  rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
  if rc:
    logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure '
                     'hardware virtualization is enabled in BIOS.')
  # Now check to ensure KVM acceleration can be used.
  rc = cmd_helper.RunCmd(['kvm-ok'])
  if rc:
    logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
                     'virtualization is enabled in BIOS (i.e. Intel VT-x or '
                     'AMD SVM).')


def GetX86Image():
  """Download x86 system image from Intel's website."""
  logging.info('Download x86 system image directory into sdk directory.')
  try:
    cmd_helper.RunCmd(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL])
    rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/x86_img.zip', '-d', '/tmp/'])
    if rc:
      raise Exception('ERROR: Could not download/unzip image zip.')
    sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk',
                            'system-images', API_TARGET, 'x86')
    shutil.move('/tmp/x86', sys_imgs)
  finally:
    os.unlink('/tmp/x86_img.zip')


def main(argv):
  logging.basicConfig(level=logging.INFO,
                      format='# %(asctime)-15s: %(message)s')
  run_tests_helper.SetLogLevel(verbose_count=1)

  # Calls below will download emulator SDK and/or system images only if needed.
  if CheckSDK():
    logging.info('android_emulator_sdk/ already exists, skipping download.')
  else:
    GetSDK()

  # Download the x86 system image only if needed.
  if CheckX86Image():
    logging.info('The x86 image is already present, skipping download.')
  else:
    GetX86Image()

  # Make sure KVM packages are installed and enabled.
  if CheckKVM():
    logging.info('KVM already installed and enabled.')
  else:
    InstallKVM()


if __name__ == '__main__':
  sys.exit(main(sys.argv))