OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Installs deps for using SDK emulator for testing. | 6 """Installs deps for using SDK emulator for testing. |
7 | 7 |
8 The script will download the SDK and system images, if they are not present, and | 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. | 9 install and enable KVM, if virtualization has been enabled in the BIOS. |
10 """ | 10 """ |
11 | 11 |
12 | 12 |
13 import logging | 13 import logging |
14 import optparse | |
14 import os | 15 import os |
16 import re | |
15 import shutil | 17 import shutil |
16 import sys | 18 import sys |
17 | 19 |
18 from pylib import cmd_helper | 20 from pylib import cmd_helper |
19 from pylib import constants | 21 from pylib import constants |
22 from pylib import pexpect | |
20 from pylib.utils import run_tests_helper | 23 from pylib.utils import run_tests_helper |
21 | 24 |
25 # Android API level | |
26 DEFAULT_ANDROID_API_LEVEL = constants.ANDROID_SDK_VERSION | |
27 | |
22 # From the Android Developer's website. | 28 # From the Android Developer's website. |
29 # Keep this up to date; the user can install older API levels as necessary. | |
23 SDK_BASE_URL = 'http://dl.google.com/android/adt' | 30 SDK_BASE_URL = 'http://dl.google.com/android/adt' |
24 SDK_ZIP = 'adt-bundle-linux-x86_64-20130729.zip' | 31 SDK_ZIP = 'adt-bundle-linux-x86_64-20131030.zip' |
25 | 32 |
26 # Android x86 system image from the Intel website: | 33 # 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 | 34 # 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/sysi mg_x86-18_r01.zip' | 35 # These don't exist prior to Android-15. |
29 | 36 # As of 08 Nov 2013, Android-19 is not yet available either. |
30 # Android API level | 37 X86_IMG_URLS = { |
31 API_TARGET = 'android-%s' % constants.ANDROID_SDK_VERSION | 38 15: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-1 5_r01.zip', |
32 | 39 16: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-1 6_r01.zip', |
40 17: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-1 7_r01.zip', | |
41 18: 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-1 8_r01.zip'} | |
33 | 42 |
34 def CheckSDK(): | 43 def CheckSDK(): |
35 """Check if SDK is already installed. | 44 """Check if SDK is already installed. |
36 | 45 |
37 Returns: | 46 Returns: |
38 True if the emulator SDK directory (src/android_emulator_sdk/) exists. | 47 True if the emulator SDK directory (src/android_emulator_sdk/) exists. |
39 """ | 48 """ |
40 return os.path.exists(constants.EMULATOR_SDK_ROOT) | 49 return os.path.exists(constants.EMULATOR_SDK_ROOT) |
41 | 50 |
42 | 51 |
43 def CheckX86Image(): | 52 def CheckSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): |
53 """Check if the "SDK Platform" for the specified API level is installed. | |
54 This is necessary in order for the emulator to run when the target | |
55 is specified. | |
56 | |
57 Args: | |
58 api_level: the Android API level to check; defaults to the latest API. | |
59 | |
60 Returns: | |
61 True if the platform is already installed. | |
62 """ | |
63 android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, | |
64 'sdk', 'tools', 'android') | |
65 pattern = re.compile('id: [0-9]+ or "android-%d"' % api_level) | |
66 try: | |
67 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput( | |
68 [android_binary, 'list']) | |
69 if exit_code != 0: | |
70 raise Exception('\'android list\' command failed') | |
71 for line in stdout.split('\n'): | |
72 if pattern.match(line): | |
73 return True | |
74 return False | |
75 except OSError: | |
76 logging.exception('Unable to execute \'android list\'') | |
77 return False | |
78 | |
79 | |
80 def CheckX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): | |
44 """Check if Android system images have been installed. | 81 """Check if Android system images have been installed. |
45 | 82 |
83 Args: | |
84 api_level: the Android API level to check for; defaults to the latest API. | |
85 | |
46 Returns: | 86 Returns: |
47 True if sdk/system-images/<API TARGET>/x86 exists inside EMULATOR_SDK_ROOT. | 87 True if sdk/system-images/android-<api_level>/x86 exists inside |
88 EMULATOR_SDK_ROOT. | |
48 """ | 89 """ |
90 api_target = 'android-%d' % api_level | |
49 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, | 91 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, |
50 'sdk', 'system-images', | 92 'sdk', 'system-images', |
51 API_TARGET, 'x86')) | 93 api_target, 'x86')) |
52 | 94 |
53 | 95 |
54 def CheckKVM(): | 96 def CheckKVM(): |
55 """Check if KVM is enabled. | 97 """Check if KVM is enabled. |
56 | 98 |
57 Returns: | 99 Returns: |
58 True if kvm-ok returns 0 (already enabled) | 100 True if kvm-ok returns 0 (already enabled) |
59 """ | 101 """ |
60 try: | 102 try: |
61 return not cmd_helper.RunCmd(['kvm-ok']) | 103 return not cmd_helper.RunCmd(['kvm-ok']) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure ' | 138 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure ' |
97 'hardware virtualization is enabled in BIOS.') | 139 'hardware virtualization is enabled in BIOS.') |
98 # Now check to ensure KVM acceleration can be used. | 140 # Now check to ensure KVM acceleration can be used. |
99 rc = cmd_helper.RunCmd(['kvm-ok']) | 141 rc = cmd_helper.RunCmd(['kvm-ok']) |
100 if rc: | 142 if rc: |
101 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' | 143 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' |
102 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' | 144 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' |
103 'AMD SVM).') | 145 'AMD SVM).') |
104 | 146 |
105 | 147 |
106 def GetX86Image(): | 148 def GetX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): |
107 """Download x86 system image from Intel's website.""" | 149 """Download x86 system image from Intel's website. |
150 | |
151 Args: | |
152 api_level: the Android API level to download for. | |
153 """ | |
108 logging.info('Download x86 system image directory into sdk directory.') | 154 logging.info('Download x86 system image directory into sdk directory.') |
155 # TODO(andrewhayden): Use python tempfile lib instead | |
156 temp_file = '/tmp/x86_img_android-%d.zip' % api_level | |
157 if api_level not in X86_IMG_URLS: | |
158 raise Exception('ERROR: no URL known for x86 image for android-%s' % | |
159 api_level) | |
M-A Ruel
2013/12/04 15:37:52
nit: it should print:
'These api levels are curren
| |
109 try: | 160 try: |
110 cmd_helper.RunCmd(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) | 161 cmd_helper.RunCmd(['curl', '-o', temp_file, X86_IMG_URLS[api_level]]) |
111 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/x86_img.zip', '-d', '/tmp/']) | 162 rc = cmd_helper.RunCmd(['unzip', '-o', temp_file, '-d', '/tmp/']) |
112 if rc: | 163 if rc: |
113 raise Exception('ERROR: Could not download/unzip image zip.') | 164 raise Exception('ERROR: Could not download/unzip image zip.') |
165 api_target = 'android-%d' % api_level | |
114 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk', | 166 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk', |
115 'system-images', API_TARGET, 'x86') | 167 'system-images', api_target, 'x86') |
168 logging.info('Deploying system image to %s' % sys_imgs) | |
116 shutil.move('/tmp/x86', sys_imgs) | 169 shutil.move('/tmp/x86', sys_imgs) |
117 finally: | 170 finally: |
118 os.unlink('/tmp/x86_img.zip') | 171 os.unlink(temp_file) |
172 | |
173 | |
174 def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): | |
175 """Update the SDK to include the platform specified. | |
176 | |
177 Args: | |
178 api_level: the Android API level to download | |
179 """ | |
180 android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, | |
181 'sdk', 'tools', 'android') | |
182 pattern = re.compile('\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' % | |
183 api_level) | |
184 # Example: | |
185 # 2- SDK Platform Android 4.3, API 18, revision 2 | |
186 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput( | |
187 [android_binary, 'list', 'sdk']) | |
188 if exit_code != 0: | |
189 raise Exception('\'android list sdk\' command return %d' % exit_code) | |
190 for line in stdout.split('\n'): | |
191 match = pattern.match(line) | |
192 if match: | |
193 index = match.group(1) | |
194 print('package %s corresponds to platform level %d' % (index, api_level)) | |
195 # update sdk --no-ui --filter $INDEX | |
196 update_command = [android_binary, | |
197 'update', 'sdk', '--no-ui', '--filter', index] | |
198 update_command_str = ' '.join(update_command) | |
199 logging.info('running update command: %s' % update_command_str) | |
200 update_process = pexpect.spawn(update_command_str) | |
201 # TODO(andrewhayden): Do we need to bug the user about this? | |
202 if update_process.expect('Do you accept the license') != 0: | |
203 raise Exception('License agreement check failed') | |
204 update_process.sendline('y') | |
205 if update_process.expect('Done. 1 package installed.') == 0: | |
206 print('Successfully installed platform for API level %d' % api_level) | |
207 return | |
208 else: | |
209 raise Exception('Failed to install platform update') | |
210 raise Exception('Could not find android-%d update for the SDK!' % api_level) | |
119 | 211 |
120 | 212 |
121 def main(argv): | 213 def main(argv): |
214 opt_parser = optparse.OptionParser( | |
215 description='Install dependencies for running the Android emulator') | |
216 opt_parser.add_option('--api-level', dest='api_level', | |
217 help='The API level (e.g., 19 for Android 4.4) to ensure is available', | |
218 type='int', default=DEFAULT_ANDROID_API_LEVEL) | |
219 opt_parser.add_option('-v', dest='verbose', action='store_true', | |
220 help='enable verbose logging') | |
221 options, _ = opt_parser.parse_args(argv[1:]) | |
222 | |
223 # run_tests_helper will set logging to INFO or DEBUG | |
224 # We achieve verbose output by configuring it with 2 (==DEBUG) | |
225 verbosity = 1 | |
226 if (options.verbose): | |
227 verbosity = 2 | |
122 logging.basicConfig(level=logging.INFO, | 228 logging.basicConfig(level=logging.INFO, |
123 format='# %(asctime)-15s: %(message)s') | 229 format='# %(asctime)-15s: %(message)s') |
124 run_tests_helper.SetLogLevel(verbose_count=1) | 230 run_tests_helper.SetLogLevel(verbose_count=verbosity) |
125 | 231 |
126 # Calls below will download emulator SDK and/or system images only if needed. | 232 # Calls below will download emulator SDK and/or system images only if needed. |
127 if CheckSDK(): | 233 if CheckSDK(): |
128 logging.info('android_emulator_sdk/ already exists, skipping download.') | 234 logging.info('android_emulator_sdk/ already exists, skipping download.') |
129 else: | 235 else: |
130 GetSDK() | 236 GetSDK() |
131 | 237 |
238 # Check target. The target has to be installed in order to run the emulator. | |
239 if CheckSDKPlatform(options.api_level): | |
240 logging.info('SDK platform android-%d already present, skipping.' % | |
241 options.api_level) | |
242 else: | |
243 logging.info('SDK platform android-%d not present, installing.' % | |
244 options.api_level) | |
245 GetSDKPlatform(options.api_level) | |
246 | |
132 # Download the x86 system image only if needed. | 247 # Download the x86 system image only if needed. |
133 if CheckX86Image(): | 248 if CheckX86Image(options.api_level): |
134 logging.info('The x86 image is already present, skipping download.') | 249 logging.info('x86 image for android-%d already present, skipping.' % |
250 options.api_level) | |
135 else: | 251 else: |
136 GetX86Image() | 252 GetX86Image(options.api_level) |
137 | 253 |
138 # Make sure KVM packages are installed and enabled. | 254 # Make sure KVM packages are installed and enabled. |
139 if CheckKVM(): | 255 if CheckKVM(): |
140 logging.info('KVM already installed and enabled.') | 256 logging.info('KVM already installed and enabled.') |
141 else: | 257 else: |
142 InstallKVM() | 258 InstallKVM() |
143 | 259 |
144 | 260 |
145 if __name__ == '__main__': | 261 if __name__ == '__main__': |
146 sys.exit(main(sys.argv)) | 262 sys.exit(main(sys.argv)) |
OLD | NEW |