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(os.getcwd(), 'android_tools')) | |
frankf
2013/03/27 05:16:08
can this be relative to __FILE__ instead of getcwd
navabi
2013/03/27 17:13:36
Done.
| |
42 | |
43 | |
44 def CheckX86Image(): | |
45 """Check if Android system images have been installed. | |
46 | |
47 Returns: | |
48 true if android_tools/sdk/system-images directory exists. | |
49 """ | |
50 return os.path.exists(os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
51 'system-images', 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 rc = cmd_helper.RunCommand(['kvm-ok']) | |
61 return not rc | |
62 | |
63 | |
64 def GetSDK(): | |
65 """Download the SDK and unzip in android_tools directory. | |
66 | |
67 Returns: | |
68 true if SDK successfully downloaded, false otherwise | |
frankf
2013/03/27 05:16:08
nit: True
navabi
2013/03/27 17:13:36
Done.
| |
69 """ | |
70 logging.info('Download Android SDK.') | |
71 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP) | |
72 try: | |
73 cmd_helper.RunCommand(['curl', '-o', '/tmp/sdk.zip', sdk_url]) | |
74 rc = cmd_helper.RunCommand(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/']) | |
75 if rc: | |
76 logging.critical('ERROR: could not download/unzip Android SDK.') | |
77 raise | |
78 # Get the name of the sub-directory that everything will be extracted to. | |
79 dirname, _ = os.path.splitext(SDK_ZIP) | |
80 zip_dir = '/tmp/%s' % dirname | |
81 # Move the extracted directory to the current working directory. | |
82 shutil.move(zip_dir, 'android_tools') | |
83 except: | |
84 logging.critical('ERROR: Could not download Android SDK.') | |
85 return False | |
frankf
2013/03/27 05:16:08
It's pythonic to raise exceptions instead of retur
navabi
2013/03/27 17:13:36
Done.
| |
86 finally: | |
87 os.unlink('/tmp/sdk.zip') | |
88 return True | |
89 | |
90 | |
91 def InstallKVM(): | |
92 """Installs KVM packages.""" | |
93 rc = cmd_helper.RunCommand(['sudo', 'apt-get', 'install', 'kvm']) | |
94 if rc: | |
95 logging.critical('ERROR: Did not install KVM. Make sure hardware ' | |
96 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' | |
97 'AMD SVM).') | |
98 # TODO(navabi): Use modprobe kvm-amd on AMD processors. | |
99 rc = cmd_helper.RunCommand(['sudo', 'modprobe', 'kvm-intel']) | |
100 if rc: | |
101 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure ' | |
102 'hardware virtualization is enabled in BIOS.') | |
103 # Now check to ensure KVM acceleration can be used. | |
104 rc = cmd_helper.RunCommand(['kvm-ok']) | |
105 if rc: | |
106 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' | |
107 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' | |
108 'AMD SVM).') | |
109 | |
110 | |
111 def GetX86Image(): | |
112 """Download x86 system image from Intel's website. | |
113 | |
114 Returns: | |
115 true if x86 system image successfully downloaded, false otherwise | |
116 """ | |
117 logging.info('Download x86 system image directory into sdk directory.') | |
118 try: | |
119 cmd_helper.RunCommand(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) | |
120 rc = cmd_helper.RunCommand(['unzip', '-o', '/tmp/x86_img.zip', '-d', | |
121 '/tmp/']) | |
122 if rc: | |
123 logging.critical('ERROR: Could not download/unzip image zip.') | |
124 raise | |
125 sys_imgs = os.path.join(os.getcwd(), 'android_tools', 'sdk', | |
126 'system-images', API_TARGET, 'x86') | |
127 shutil.move('/tmp/x86', sys_imgs) | |
128 except: | |
129 logging.critical('ERROR: Could not download x86 image.') | |
130 return False | |
131 finally: | |
132 os.unlink('/tmp/x86_img.zip') | |
133 return True | |
134 | |
135 | |
136 def main(argv): | |
137 # Run script from parent directory of chrome checkout, because we do not want | |
138 # to put SDK and system images into chrome checkout. | |
139 new_cwd = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', | |
140 '..', '..') | |
141 os.chdir(new_cwd) | |
142 | |
143 # The location of the SDK used to launch the emulator | |
144 emulator_sdk = os.path.join(new_cwd, 'android_tools', 'sdk') | |
145 os.environ['ANDROID_SDK_ROOT'] = emulator_sdk | |
146 | |
147 logging.basicConfig(level=logging.INFO, | |
148 format='# %(asctime)-15s: %(message)s') | |
149 run_tests_helper.SetLogLevel(verbose_count=1) | |
150 | |
151 # Calls below will download emulator SDK and/or system images only if needed. | |
152 if CheckSDK(): | |
153 logging.info('android_tools directory already exists (not downloading).') | |
154 elif not GetSDK(): | |
155 # Can not continue without downloading SDK. | |
156 return 1 | |
157 if CheckX86Image(): | |
158 logging.info('system-images directory already exists.') | |
159 else: | |
160 GetX86Image() | |
161 | |
162 # Make sure KVM packages are installed and enabled. | |
163 if CheckKVM(): | |
164 logging.info('KVM already installed and enabled.') | |
165 else: | |
166 InstallKVM() | |
167 | |
168 | |
169 if __name__ == '__main__': | |
170 sys.exit(main(sys.argv)) | |
OLD | NEW |