OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Provides an interface to start and stop Android emulator. | 7 """Provides an interface to start and stop Android emulator. |
8 | 8 |
9 Assumes system environment ANDROID_NDK_ROOT has been set. | 9 Assumes system environment ANDROID_NDK_ROOT has been set. |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 return | 45 return |
46 for emu_name in emulators: | 46 for emu_name in emulators: |
47 cmd_helper.GetCmdOutput(['adb', '-s', emu_name, 'emu', 'kill']) | 47 cmd_helper.GetCmdOutput(['adb', '-s', emu_name, 'emu', 'kill']) |
48 logging.info('Emulator killing is async; give a few seconds for all to die.') | 48 logging.info('Emulator killing is async; give a few seconds for all to die.') |
49 for i in range(5): | 49 for i in range(5): |
50 if not android_commands.GetEmulators(): | 50 if not android_commands.GetEmulators(): |
51 return | 51 return |
52 time.sleep(1) | 52 time.sleep(1) |
53 | 53 |
54 | 54 |
| 55 def DeleteAllTempAVDs(): |
| 56 """Delete all temporary AVDs which are created for tests. |
| 57 |
| 58 If the test exits abnormally and some temporary AVDs created when testing may |
| 59 be left in the system. Clean these AVDs. |
| 60 """ |
| 61 avds = android_commands.GetAVDs() |
| 62 if not avds: |
| 63 return |
| 64 for avd_name in avds: |
| 65 if 'run_tests_avd' in avd_name: |
| 66 cmd = ['android', '-s', 'delete', 'avd', '--name', avd_name] |
| 67 cmd_helper.GetCmdOutput(cmd) |
| 68 logging.info('Delete AVD %s' % avd_name) |
| 69 |
| 70 |
55 class PortPool(object): | 71 class PortPool(object): |
56 """Pool for emulator port starting position that changes over time.""" | 72 """Pool for emulator port starting position that changes over time.""" |
57 _port_min = 5554 | 73 _port_min = 5554 |
58 _port_max = 5585 | 74 _port_max = 5585 |
59 _port_current_index = 0 | 75 _port_current_index = 0 |
60 | 76 |
61 @classmethod | 77 @classmethod |
62 def port_range(cls): | 78 def port_range(cls): |
63 """Return a range of valid ports for emulator use. | 79 """Return a range of valid ports for emulator use. |
64 | 80 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 if new_avd_name: | 158 if new_avd_name: |
143 self.default_avd = False | 159 self.default_avd = False |
144 self.avd = self._CreateAVD(new_avd_name) | 160 self.avd = self._CreateAVD(new_avd_name) |
145 | 161 |
146 def _DeviceName(self): | 162 def _DeviceName(self): |
147 """Return our device name.""" | 163 """Return our device name.""" |
148 port = _GetAvailablePort() | 164 port = _GetAvailablePort() |
149 return ('emulator-%d' % port, port) | 165 return ('emulator-%d' % port, port) |
150 | 166 |
151 def _CreateAVD(self, avd_name): | 167 def _CreateAVD(self, avd_name): |
| 168 """Creates an AVD with the given name. |
| 169 |
| 170 Return avd_name. |
| 171 """ |
152 avd_command = [ | 172 avd_command = [ |
153 self.android, | 173 self.android, |
154 '--silent', | 174 '--silent', |
155 'create', 'avd', | 175 'create', 'avd', |
156 '--name', avd_name, | 176 '--name', avd_name, |
157 '--abi', self.abi, | 177 '--abi', self.abi, |
158 '--target', 'android-15', | 178 '--target', 'android-15', |
159 '-c', '64M', | 179 '-c', '64M', |
160 '--force', | 180 '--force', |
161 ] | 181 ] |
162 avd_process = subprocess.Popen(args=avd_command, | 182 avd_process = subprocess.Popen(args=avd_command, |
163 stdin=subprocess.PIPE, | 183 stdin=subprocess.PIPE, |
164 stdout=subprocess.PIPE, | 184 stdout=subprocess.PIPE, |
165 stderr=subprocess.STDOUT) | 185 stderr=subprocess.STDOUT) |
166 avd_process.stdin.write('no\n') | 186 avd_process.stdin.write('no\n') |
167 avd_process.wait() | 187 avd_process.wait() |
168 logging.info('Create AVD command: %s', ' '.join(avd_command)) | 188 logging.info('Create AVD command: %s', ' '.join(avd_command)) |
169 return self.avd | 189 return avd_name |
170 | 190 |
171 def _DeleteAVD(self): | 191 def _DeleteAVD(self): |
| 192 """Delete the AVD of this emulator.""" |
172 avd_command = [ | 193 avd_command = [ |
173 self.android, | 194 self.android, |
174 '--silent', | 195 '--silent', |
175 'delete', | 196 'delete', |
176 'avd', | 197 'avd', |
177 '--name', self.avd, | 198 '--name', self.avd, |
178 ] | 199 ] |
179 avd_process = subprocess.Popen(args=avd_command, | 200 avd_process = subprocess.Popen(args=avd_command, |
180 stdout=subprocess.PIPE, | 201 stdout=subprocess.PIPE, |
181 stderr=subprocess.STDOUT) | 202 stderr=subprocess.STDOUT) |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 """Install a handler to kill the emulator when we exit unexpectedly.""" | 310 """Install a handler to kill the emulator when we exit unexpectedly.""" |
290 for sig in self._SIGNALS: | 311 for sig in self._SIGNALS: |
291 signal.signal(sig, self._ShutdownOnSignal) | 312 signal.signal(sig, self._ShutdownOnSignal) |
292 | 313 |
293 def main(argv): | 314 def main(argv): |
294 Emulator(None, True).Launch(True) | 315 Emulator(None, True).Launch(True) |
295 | 316 |
296 | 317 |
297 if __name__ == '__main__': | 318 if __name__ == '__main__': |
298 main(sys.argv) | 319 main(sys.argv) |
OLD | NEW |