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([android_binary, 'list' ]) | |
navabi
2013/11/11 22:08:19
line length should be < 80 characters.
(except for
Andrew Hayden (chromium.org)
2013/11/12 14:42:16
I feel that the code lines that are > 80 character
| |
68 if exit_code != 0: | |
69 raise Exception('\'android list\' command failed') | |
70 for line in stdout.split('\n'): | |
71 if pattern.match(line): | |
72 return True | |
73 return False | |
74 except OSError: | |
75 logging.exception('Unable to execute \'android list\'') | |
76 return False | |
77 | |
78 | |
79 def CheckX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): | |
44 """Check if Android system images have been installed. | 80 """Check if Android system images have been installed. |
45 | 81 |
82 Args: | |
83 api_level: the Android API level to check for; defaults to the latest API. | |
84 | |
46 Returns: | 85 Returns: |
47 True if sdk/system-images/<API TARGET>/x86 exists inside EMULATOR_SDK_ROOT. | 86 True if sdk/system-images/android-<api_level>/x86 exists inside EMULATOR_SDK _ROOT. |
navabi
2013/11/11 22:08:19
line length here and other places.
Andrew Hayden (chromium.org)
2013/11/12 14:42:16
See above, repaired in comments and leaving the re
| |
48 """ | 87 """ |
88 api_target = 'android-%d' % api_level | |
49 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, | 89 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, |
50 'sdk', 'system-images', | 90 'sdk', 'system-images', |
51 API_TARGET, 'x86')) | 91 api_target, 'x86')) |
52 | 92 |
53 | 93 |
54 def CheckKVM(): | 94 def CheckKVM(): |
55 """Check if KVM is enabled. | 95 """Check if KVM is enabled. |
56 | 96 |
57 Returns: | 97 Returns: |
58 True if kvm-ok returns 0 (already enabled) | 98 True if kvm-ok returns 0 (already enabled) |
59 """ | 99 """ |
60 try: | 100 try: |
61 return not cmd_helper.RunCmd(['kvm-ok']) | 101 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 ' | 136 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure ' |
97 'hardware virtualization is enabled in BIOS.') | 137 'hardware virtualization is enabled in BIOS.') |
98 # Now check to ensure KVM acceleration can be used. | 138 # Now check to ensure KVM acceleration can be used. |
99 rc = cmd_helper.RunCmd(['kvm-ok']) | 139 rc = cmd_helper.RunCmd(['kvm-ok']) |
100 if rc: | 140 if rc: |
101 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' | 141 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' |
102 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' | 142 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' |
103 'AMD SVM).') | 143 'AMD SVM).') |
104 | 144 |
105 | 145 |
106 def GetX86Image(): | 146 def GetX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): |
107 """Download x86 system image from Intel's website.""" | 147 """Download x86 system image from Intel's website. |
148 | |
149 Args: | |
150 api_level: the Android API level to download for; defaults to the latest API . | |
151 """ | |
108 logging.info('Download x86 system image directory into sdk directory.') | 152 logging.info('Download x86 system image directory into sdk directory.') |
153 # TODO(andrewhayden): Use python tempfile lib instead | |
navabi
2013/11/11 22:08:19
I'm not sure you can use python's tempfile lib. I
Andrew Hayden (chromium.org)
2013/11/12 14:42:16
Thanks for the context!
I think the only thing th
| |
154 temp_file = '/tmp/x86_img_android-%d.zip' % api_level | |
155 if api_level not in X86_IMG_URLS: | |
156 raise Exception('ERROR: no URL known for x86 image for android-%s' % api_lev el) | |
109 try: | 157 try: |
110 cmd_helper.RunCmd(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) | 158 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/']) | 159 rc = cmd_helper.RunCmd(['unzip', '-o', temp_file, '-d', '/tmp/']) |
112 if rc: | 160 if rc: |
113 raise Exception('ERROR: Could not download/unzip image zip.') | 161 raise Exception('ERROR: Could not download/unzip image zip.') |
162 api_target = 'android-%d' % api_level | |
114 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk', | 163 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk', |
115 'system-images', API_TARGET, 'x86') | 164 'system-images', api_target, 'x86') |
165 logging.info('Deploying system image to %s' % sys_imgs) | |
116 shutil.move('/tmp/x86', sys_imgs) | 166 shutil.move('/tmp/x86', sys_imgs) |
117 finally: | 167 finally: |
118 os.unlink('/tmp/x86_img.zip') | 168 os.unlink(temp_file) |
169 | |
170 | |
171 def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): | |
172 """Update the SDK to include the platform specified. | |
173 | |
174 Args: | |
175 api_level: the Android API level to download | |
176 """ | |
177 android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk', 'tools', 'an droid') | |
178 pattern = re.compile('\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' % api_level) | |
179 # Example: | |
180 # 2- SDK Platform Android 4.3, API 18, revision 2 | |
181 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput([android_binary, 'list', 'sdk']) | |
182 if exit_code != 0: | |
183 raise Exception('\'android list sdk\' command return %d' % exit_code) | |
184 for line in stdout.split('\n'): | |
185 match = pattern.match(line) | |
186 if match: | |
187 index = match.group(1) | |
188 print('package %s corresponds to platform level %d' % (index, api_level)) | |
189 # update sdk --no-ui --filter $INDEX | |
190 update_command = [android_binary, | |
191 'update', 'sdk', '--no-ui', '--filter', index] | |
192 update_command_str = ' '.join(update_command) | |
193 logging.info('running update command: %s' % update_command_str) | |
194 update_process = pexpect.spawn(update_command_str) | |
195 # TODO(andrewhayden): Do we need to bug the user about this? | |
196 if update_process.expect('Do you accept the license') != 0: | |
197 raise Exception('License agreement check failed') | |
198 update_process.sendline('y') | |
199 if update_process.expect('Done. 1 package installed.') == 0: | |
200 print('Successfully installed platform for API level %d' % api_level) | |
201 return | |
202 else: | |
203 raise Exception('Failed to install platform update') | |
204 raise Exception('Could not find android-%d update for the SDK!' % api_level) | |
119 | 205 |
120 | 206 |
121 def main(argv): | 207 def main(argv): |
208 opt_parser = optparse.OptionParser(description='Install dependencies for runni ng the Android emulator') | |
209 opt_parser.add_option('--api-level', dest='api_level', | |
210 help='The API level (e.g., 19 for Android 4.4) to ensure is available', | |
211 type='int', default=DEFAULT_ANDROID_API_LEVEL) | |
212 opt_parser.add_option('-v', dest='verbose', action='store_true', | |
213 help='enable verbose logging') | |
214 options, _ = opt_parser.parse_args(argv[1:]) | |
215 | |
216 # run_tests_helper will set logging to INFO or DEBUG | |
217 # We achieve verbose output by configuring it with 2 (==DEBUG) | |
218 verbosity = 1 | |
219 if (options.verbose): | |
220 verbosity = 2 | |
122 logging.basicConfig(level=logging.INFO, | 221 logging.basicConfig(level=logging.INFO, |
123 format='# %(asctime)-15s: %(message)s') | 222 format='# %(asctime)-15s: %(message)s') |
124 run_tests_helper.SetLogLevel(verbose_count=1) | 223 run_tests_helper.SetLogLevel(verbose_count=verbosity) |
125 | 224 |
126 # Calls below will download emulator SDK and/or system images only if needed. | 225 # Calls below will download emulator SDK and/or system images only if needed. |
127 if CheckSDK(): | 226 if CheckSDK(): |
128 logging.info('android_emulator_sdk/ already exists, skipping download.') | 227 logging.info('android_emulator_sdk/ already exists, skipping download.') |
129 else: | 228 else: |
130 GetSDK() | 229 GetSDK() |
131 | 230 |
231 # Check target. The target has to be installed in order for the emulator to st art. | |
232 if CheckSDKPlatform(options.api_level): | |
233 logging.info('SDK platform android-%d already present, skipping.' % options. api_level) | |
234 else: | |
235 logging.info('SDK platform android-%d not present, installing.' % options.ap i_level) | |
236 GetSDKPlatform(options.api_level) | |
237 | |
132 # Download the x86 system image only if needed. | 238 # Download the x86 system image only if needed. |
133 if CheckX86Image(): | 239 if CheckX86Image(options.api_level): |
134 logging.info('The x86 image is already present, skipping download.') | 240 logging.info('x86 image for android-%d already present, skipping.' % options .api_level) |
135 else: | 241 else: |
136 GetX86Image() | 242 GetX86Image(options.api_level) |
137 | 243 |
138 # Make sure KVM packages are installed and enabled. | 244 # Make sure KVM packages are installed and enabled. |
139 if CheckKVM(): | 245 if CheckKVM(): |
140 logging.info('KVM already installed and enabled.') | 246 logging.info('KVM already installed and enabled.') |
141 else: | 247 else: |
142 InstallKVM() | 248 InstallKVM() |
143 | 249 |
144 | 250 |
145 if __name__ == '__main__': | 251 if __name__ == '__main__': |
146 sys.exit(main(sys.argv)) | 252 sys.exit(main(sys.argv)) |
OLD | NEW |