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 """ |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 18: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-18_r01.zi p', | 42 18: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-18_r01.zi p', |
43 19: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-19_r01.zi p'} | 43 19: 'https://software.intel.com/sites/landingpage/android/sysimg_x86-19_r01.zi p'} |
44 #pylint: enable=line-too-long | 44 #pylint: enable=line-too-long |
45 | 45 |
46 def CheckSDK(): | 46 def CheckSDK(): |
47 """Check if SDK is already installed. | 47 """Check if SDK is already installed. |
48 | 48 |
49 Returns: | 49 Returns: |
50 True if the emulator SDK directory (src/android_emulator_sdk/) exists. | 50 True if the emulator SDK directory (src/android_emulator_sdk/) exists. |
51 """ | 51 """ |
52 return os.path.exists(constants.EMULATOR_SDK_ROOT) | 52 return os.path.exists(constants.ANDROID_SDK_ROOT) |
mikecase (-- gone --)
2015/09/12 00:10:16
Same comment as before.
You will have to rebase t
Yoland Yan(Google)
2015/09/12 01:42:38
Done
| |
53 | 53 |
54 | 54 |
55 def CheckSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): | 55 def CheckSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): |
56 """Check if the "SDK Platform" for the specified API level is installed. | 56 """Check if the "SDK Platform" for the specified API level is installed. |
57 This is necessary in order for the emulator to run when the target | 57 This is necessary in order for the emulator to run when the target |
58 is specified. | 58 is specified. |
59 | 59 |
60 Args: | 60 Args: |
61 api_level: the Android API level to check; defaults to the latest API. | 61 api_level: the Android API level to check; defaults to the latest API. |
62 | 62 |
63 Returns: | 63 Returns: |
64 True if the platform is already installed. | 64 True if the platform is already installed. |
65 """ | 65 """ |
66 android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, | 66 android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android') |
67 'sdk', 'tools', 'android') | |
68 pattern = re.compile('id: [0-9]+ or "android-%d"' % api_level) | 67 pattern = re.compile('id: [0-9]+ or "android-%d"' % api_level) |
69 try: | 68 try: |
70 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput( | 69 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput( |
71 [android_binary, 'list']) | 70 [android_binary, 'list']) |
72 if exit_code != 0: | 71 if exit_code != 0: |
73 raise Exception('\'android list\' command failed') | 72 raise Exception('\'android list\' command failed') |
74 for line in stdout.split('\n'): | 73 for line in stdout.split('\n'): |
75 if pattern.match(line): | 74 if pattern.match(line): |
76 return True | 75 return True |
77 return False | 76 return False |
78 except OSError: | 77 except OSError: |
79 logging.exception('Unable to execute \'android list\'') | 78 logging.exception('Unable to execute \'android list\'') |
80 return False | 79 return False |
81 | 80 |
82 | 81 |
83 def CheckX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): | 82 def CheckX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): |
84 """Check if Android system images have been installed. | 83 """Check if Android system images have been installed. |
85 | 84 |
86 Args: | 85 Args: |
87 api_level: the Android API level to check for; defaults to the latest API. | 86 api_level: the Android API level to check for; defaults to the latest API. |
88 | 87 |
89 Returns: | 88 Returns: |
90 True if sdk/system-images/android-<api_level>/x86 exists inside | 89 True if sdk/system-images/android-<api_level>/default/x86 exists inside |
91 EMULATOR_SDK_ROOT. | 90 ANDROID_SDK_ROOT. |
92 """ | 91 """ |
93 api_target = 'android-%d' % api_level | 92 api_target = 'android-%d' % api_level |
94 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, | 93 return os.path.exists(os.path.join(constants.ANDROID_SDK_ROOT, |
95 'sdk', 'system-images', | 94 'system-images', api_target, 'default', |
96 api_target, 'x86')) | 95 'x86')) |
97 | 96 |
98 | 97 |
99 def CheckKVM(): | 98 def CheckKVM(): |
100 """Quickly check whether KVM is enabled. | 99 """Quickly check whether KVM is enabled. |
101 | 100 |
102 Returns: | 101 Returns: |
103 True iff /dev/kvm exists (Linux only). | 102 True iff /dev/kvm exists (Linux only). |
104 """ | 103 """ |
105 return os.path.exists('/dev/kvm') | 104 return os.path.exists('/dev/kvm') |
106 | 105 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 'AMD SVM).') | 159 'AMD SVM).') |
161 | 160 |
162 | 161 |
163 def GetX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): | 162 def GetX86Image(api_level=DEFAULT_ANDROID_API_LEVEL): |
164 """Download x86 system image from Intel's website. | 163 """Download x86 system image from Intel's website. |
165 | 164 |
166 Args: | 165 Args: |
167 api_level: the Android API level to download for. | 166 api_level: the Android API level to download for. |
168 """ | 167 """ |
169 logging.info('Download x86 system image directory into sdk directory.') | 168 logging.info('Download x86 system image directory into sdk directory.') |
170 # TODO(andrewhayden): Use python tempfile lib instead | 169 |
171 temp_file = '/tmp/x86_img_android-%d.zip' % api_level | 170 android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android') |
172 if api_level not in X86_IMG_URLS: | 171 |
173 raise Exception('ERROR: no URL known for x86 image for android-%s' % | 172 pattern = re.compile( |
mikecase (-- gone --)
2015/09/12 00:10:16
I would change the name from "pattern" to somethin
Yoland Yan(Google)
2015/09/12 01:42:38
Done
| |
174 api_level) | 173 r'\s*([0-9]+)- Intel x86 Atom System Image, Android API %d.*' % api_level) |
175 try: | 174 tmp_cmd = [android_binary, 'list', 'sdk', '--all'] |
mikecase (-- gone --)
2015/09/12 00:10:16
I would change the name to something more specific
Yoland Yan(Google)
2015/09/12 01:42:38
Done
| |
176 cmd_helper.RunCmd(['curl', '-o', temp_file, X86_IMG_URLS[api_level]]) | 175 |
177 rc = cmd_helper.RunCmd(['unzip', '-o', temp_file, '-d', '/tmp/']) | 176 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(tmp_cmd) |
178 if rc: | 177 |
179 raise Exception('ERROR: Could not download/unzip image zip.') | 178 if exit_code != 0: |
180 api_target = 'android-%d' % api_level | 179 raise Exception('\'android list sdk --all\' command return %d' % exit_code) |
181 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk', | 180 for line in stdout.split('\n'): |
182 'system-images', api_target, 'x86') | 181 match = pattern.match(line) |
183 logging.info('Deploying system image to %s', sys_imgs) | 182 if match: |
184 shutil.move('/tmp/x86', sys_imgs) | 183 index = match.group(1) |
185 finally: | 184 print 'package %s corresponds to x86 system image with api level %d' |
mikecase (-- gone --)
2015/09/12 00:10:16
use python logging module
Yoland Yan(Google)
2015/09/12 01:42:38
Done
| |
186 os.unlink(temp_file) | 185 % (index, api_level) |
186 update_command = [android_binary, 'update', 'sdk', '--no-ui', '--all', | |
187 '--filter', index] | |
188 update_command_str = ' '.join(update_command) | |
189 logging.info('running update command: %s', update_command_str) | |
190 update_process = pexpect.spawn(update_command_str) | |
191 | |
192 if update_process.expect('Do you accept the license') != 0: | |
193 raise Exception('License agreement check failed') | |
194 update_process.sendline('y') | |
195 if update_process.expect('Done. 1 package installed.', timeout=60) == 0: | |
196 print 'Successfully installed x86 system image for API level %d' | |
mikecase (-- gone --)
2015/09/12 00:10:16
I would use the python logging module instead of p
Yoland Yan(Google)
2015/09/12 01:42:38
Done
| |
197 % api_level | |
198 return | |
199 else: | |
200 raise Exception('Failed to install platform update') | |
201 raise Exception('Could not find android-%d update for the SDK!' % api_level) | |
187 | 202 |
mikecase (-- gone --)
2015/09/12 00:10:16
This code you added is extremely similar to the be
Yoland Yan(Google)
2015/09/12 01:42:38
Done
| |
188 | 203 |
189 def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): | 204 def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL): |
190 """Update the SDK to include the platform specified. | 205 """Update the SDK to include the platform specified. |
191 | 206 |
192 Args: | 207 Args: |
193 api_level: the Android API level to download | 208 api_level: the Android API level to download |
194 """ | 209 """ |
195 android_binary = os.path.join(constants.EMULATOR_SDK_ROOT, | 210 android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android') |
196 'sdk', 'tools', 'android') | |
197 pattern = re.compile( | 211 pattern = re.compile( |
198 r'\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' % api_level) | 212 r'\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' % api_level) |
199 # Example: | 213 # Example: |
200 # 2- SDK Platform Android 4.3, API 18, revision 2 | 214 # 2- SDK Platform Android 4.3, API 18, revision 2 |
201 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput( | 215 exit_code, stdout = cmd_helper.GetCmdStatusAndOutput( |
202 [android_binary, 'list', 'sdk']) | 216 [android_binary, 'list', 'sdk', '--all']) |
203 if exit_code != 0: | 217 if exit_code != 0: |
204 raise Exception('\'android list sdk\' command return %d' % exit_code) | 218 raise Exception('\'android list sdk\' command return %d' % exit_code) |
205 for line in stdout.split('\n'): | 219 for line in stdout.split('\n'): |
206 match = pattern.match(line) | 220 match = pattern.match(line) |
207 if match: | 221 if match: |
208 index = match.group(1) | 222 index = match.group(1) |
209 print 'package %s corresponds to platform level %d' % (index, api_level) | 223 print 'package %s corresponds to platform level %d' % (index, api_level) |
210 # update sdk --no-ui --filter $INDEX | |
211 update_command = [android_binary, | 224 update_command = [android_binary, |
212 'update', 'sdk', '--no-ui', '--filter', index] | 225 'update', 'sdk', '--no-ui', '--all', '--filter', index] |
213 update_command_str = ' '.join(update_command) | 226 update_command_str = ' '.join(update_command) |
214 logging.info('running update command: %s', update_command_str) | 227 logging.info('running update command: %s', update_command_str) |
215 update_process = pexpect.spawn(update_command_str) | 228 update_process = pexpect.spawn(update_command_str) |
216 # TODO(andrewhayden): Do we need to bug the user about this? | 229 # TODO(andrewhayden): Do we need to bug the user about this? |
217 if update_process.expect('Do you accept the license') != 0: | 230 if update_process.expect('Do you accept the license') != 0: |
218 raise Exception('License agreement check failed') | 231 raise Exception('License agreement check failed') |
219 update_process.sendline('y') | 232 update_process.sendline('y') |
220 if update_process.expect('Done. 1 package installed.') == 0: | 233 if update_process.expect('Done. 1 package installed.', timeout=60) == 0: |
Yoland Yan(Google)
2015/09/11 19:17:22
It takes a long time to download SDK Platform
| |
221 print 'Successfully installed platform for API level %d' % api_level | 234 print 'Successfully installed platform for API level %d' % api_level |
222 return | 235 return |
223 else: | 236 else: |
224 raise Exception('Failed to install platform update') | 237 raise Exception('Failed to install platform update') |
225 raise Exception('Could not find android-%d update for the SDK!' % api_level) | 238 raise Exception('Could not find android-%d update for the SDK!' % api_level) |
226 | 239 |
227 | 240 |
228 def main(argv): | 241 def main(argv): |
229 opt_parser = optparse.OptionParser( | 242 opt_parser = optparse.OptionParser( |
230 description='Install dependencies for running the Android emulator') | 243 description='Install dependencies for running the Android emulator') |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 | 281 |
269 # Make sure KVM packages are installed and enabled. | 282 # Make sure KVM packages are installed and enabled. |
270 if CheckKVM(): | 283 if CheckKVM(): |
271 logging.info('KVM already installed and enabled.') | 284 logging.info('KVM already installed and enabled.') |
272 else: | 285 else: |
273 InstallKVM() | 286 InstallKVM() |
274 | 287 |
275 | 288 |
276 if __name__ == '__main__': | 289 if __name__ == '__main__': |
277 sys.exit(main(sys.argv)) | 290 sys.exit(main(sys.argv)) |
OLD | NEW |