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