- add sources.
[platform/framework/web/crosswalk.git] / src / build / android / install_emulator_deps.py
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 sys
17
18 from pylib import cmd_helper
19 from pylib import constants
20 from pylib.utils import run_tests_helper
21
22 # From the Android Developer's website.
23 SDK_BASE_URL = 'http://dl.google.com/android/adt'
24 SDK_ZIP = 'adt-bundle-linux-x86_64-20130729.zip'
25
26 # Android x86 system image from the Intel website:
27 # http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean-bin
28 X86_IMG_URL = 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-18_r01.zip'
29
30 # Android API level
31 API_TARGET = 'android-%s' % constants.ANDROID_SDK_VERSION
32
33
34 def CheckSDK():
35   """Check if SDK is already installed.
36
37   Returns:
38     True if the emulator SDK directory (src/android_emulator_sdk/) exists.
39   """
40   return os.path.exists(constants.EMULATOR_SDK_ROOT)
41
42
43 def CheckX86Image():
44   """Check if Android system images have been installed.
45
46   Returns:
47     True if sdk/system-images/<API TARGET>/x86 exists inside EMULATOR_SDK_ROOT.
48   """
49   return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT,
50                                      'sdk', 'system-images',
51                                      API_TARGET, 'x86'))
52
53
54 def CheckKVM():
55   """Check if KVM is enabled.
56
57   Returns:
58     True if kvm-ok returns 0 (already enabled)
59   """
60   try:
61     return not cmd_helper.RunCmd(['kvm-ok'])
62   except OSError:
63     logging.info('kvm-ok not installed')
64     return False
65
66
67 def GetSDK():
68   """Download the SDK and unzip it into EMULATOR_SDK_ROOT."""
69   logging.info('Download Android SDK.')
70   sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP)
71   try:
72     cmd_helper.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url])
73     print 'curled unzipping...'
74     rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/'])
75     if rc:
76       raise Exception('ERROR: could not download/unzip Android SDK.')
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     shutil.move(zip_dir, constants.EMULATOR_SDK_ROOT)
82   finally:
83     os.unlink('/tmp/sdk.zip')
84
85
86 def InstallKVM():
87   """Installs KVM packages."""
88   rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
89   if rc:
90     logging.critical('ERROR: Did not install KVM. Make sure hardware '
91                      'virtualization is enabled in BIOS (i.e. Intel VT-x or '
92                      'AMD SVM).')
93   # TODO(navabi): Use modprobe kvm-amd on AMD processors.
94   rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
95   if rc:
96     logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure '
97                      'hardware virtualization is enabled in BIOS.')
98   # Now check to ensure KVM acceleration can be used.
99   rc = cmd_helper.RunCmd(['kvm-ok'])
100   if rc:
101     logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
102                      'virtualization is enabled in BIOS (i.e. Intel VT-x or '
103                      'AMD SVM).')
104
105
106 def GetX86Image():
107   """Download x86 system image from Intel's website."""
108   logging.info('Download x86 system image directory into sdk directory.')
109   try:
110     cmd_helper.RunCmd(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL])
111     rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/x86_img.zip', '-d', '/tmp/'])
112     if rc:
113       raise Exception('ERROR: Could not download/unzip image zip.')
114     sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk',
115                             'system-images', API_TARGET, 'x86')
116     shutil.move('/tmp/x86', sys_imgs)
117   finally:
118     os.unlink('/tmp/x86_img.zip')
119
120
121 def main(argv):
122   logging.basicConfig(level=logging.INFO,
123                       format='# %(asctime)-15s: %(message)s')
124   run_tests_helper.SetLogLevel(verbose_count=1)
125
126   # Calls below will download emulator SDK and/or system images only if needed.
127   if CheckSDK():
128     logging.info('android_emulator_sdk/ already exists, skipping download.')
129   else:
130     GetSDK()
131
132   # Download the x86 system image only if needed.
133   if CheckX86Image():
134     logging.info('The x86 image is already present, skipping download.')
135   else:
136     GetX86Image()
137
138   # Make sure KVM packages are installed and enabled.
139   if CheckKVM():
140     logging.info('KVM already installed and enabled.')
141   else:
142     InstallKVM()
143
144
145 if __name__ == '__main__':
146   sys.exit(main(sys.argv))