OLD | NEW |
1 # Copyright 2015 Google Inc. All rights reserved. | 1 # Copyright 2015 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
6 # | 6 # |
7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
8 # | 8 # |
9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 try: | 320 try: |
321 self._adb_cmd.Push(cStringIO.StringIO(content), dest, mtime) | 321 self._adb_cmd.Push(cStringIO.StringIO(content), dest, mtime) |
322 return True | 322 return True |
323 except usb_exceptions.AdbCommandFailureException: | 323 except usb_exceptions.AdbCommandFailureException: |
324 break | 324 break |
325 except self._ERRORS as e: | 325 except self._ERRORS as e: |
326 if not self._Reset('(%s, %s): %s', dest, content, e): | 326 if not self._Reset('(%s, %s): %s', dest, content, e): |
327 break | 327 break |
328 return False | 328 return False |
329 | 329 |
330 def Reboot(self): | 330 def Reboot(self, wait=True): |
331 """Reboots the device. Waits for it to be rebooted but not fully | 331 """Reboots the device. Waits for it to be rebooted but not fully |
332 operational. | 332 operational. |
333 | 333 |
334 This causes the USB device to disapear momentarily and get a new port_path. | 334 This causes the USB device to disapear momentarily and get a new port_path. |
335 If the phone just booted up, this function will cause the caller to sleep. | 335 If the phone just booted up, this function will cause the caller to sleep. |
336 | 336 |
337 The caller will likely want to call self.Root() to switch adbd to root | 337 The caller will likely want to call self.Root() to switch adbd to root |
338 context if desired. | 338 context if desired. |
339 | 339 |
340 Returns True on success. | 340 Returns True on success. |
341 """ | 341 """ |
342 if self._adb_cmd: | 342 if self._adb_cmd: |
| 343 if not wait: |
| 344 return self._Reboot() |
| 345 |
343 # Get previous uptime to ensure the phone actually rebooted. | 346 # Get previous uptime to ensure the phone actually rebooted. |
344 previous_uptime = self.GetUptime() | 347 previous_uptime = self.GetUptime() |
345 if not previous_uptime: | 348 if not previous_uptime: |
346 return False | 349 return False |
347 if previous_uptime <= 30.: | 350 if previous_uptime <= 30.: |
348 # Wait for uptime to be high enough. Otherwise we can't know if the | 351 # Wait for uptime to be high enough. Otherwise we can't know if the |
349 # device rebooted or not. | 352 # device rebooted or not. |
350 time.sleep(31. - previous_uptime) | 353 time.sleep(31. - previous_uptime) |
351 previous_uptime = self.GetUptime() | 354 previous_uptime = self.GetUptime() |
352 if not previous_uptime: | 355 if not previous_uptime: |
(...skipping 27 matching lines...) Expand all Loading... |
380 # TODO(maruel): Wait for the remount operation to be completed. | 383 # TODO(maruel): Wait for the remount operation to be completed. |
381 _LOG.info('%s.Remount(): %s', self.port_path, out) | 384 _LOG.info('%s.Remount(): %s', self.port_path, out) |
382 return True | 385 return True |
383 except usb_exceptions.AdbCommandFailureException: | 386 except usb_exceptions.AdbCommandFailureException: |
384 break | 387 break |
385 except self._ERRORS as e: | 388 except self._ERRORS as e: |
386 if not self._Reset('(): %s', e): | 389 if not self._Reset('(): %s', e): |
387 break | 390 break |
388 return False | 391 return False |
389 | 392 |
| 393 def ResetHandle(self, new_endpoint=None): |
| 394 """Resets the handle used to communicate to the device. |
| 395 |
| 396 For USB connections, it resets the usb device. For TCP connections, |
| 397 it closes and reopens the TCP connection. |
| 398 |
| 399 Args: |
| 400 new_endpoint: If a TCP device, this will be used as the new endpoint in |
| 401 the TCP connection. |
| 402 Returns: |
| 403 True on success. |
| 404 """ |
| 405 if self._adb_cmd: |
| 406 self._adb_cmd.Close() |
| 407 self._adb_cmd = None |
| 408 if new_endpoint: |
| 409 self._serial = None |
| 410 self._handle.Reset(new_endpoint=new_endpoint) |
| 411 return self._Connect(False) |
| 412 |
390 def Shell(self, cmd, timeout_ms=None): | 413 def Shell(self, cmd, timeout_ms=None): |
391 """Runs a command on an Android device while swallowing exceptions. | 414 """Runs a command on an Android device while swallowing exceptions. |
392 | 415 |
393 Traps all kinds of USB errors so callers do not have to handle this. | 416 Traps all kinds of USB errors so callers do not have to handle this. |
394 | 417 |
395 Returns: | 418 Returns: |
396 tuple(stdout, exit_code) | 419 tuple(stdout, exit_code) |
397 - stdout is as unicode if it ran, None if an USB error occurred. | 420 - stdout is as unicode if it ran, None if an USB error occurred. |
398 - exit_code is set if ran. | 421 - exit_code is set if ran. |
399 """ | 422 """ |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 self.Close() | 873 self.Close() |
851 if not self._WaitUntilFound(use_serial=use_serial, timeout=timeout): | 874 if not self._WaitUntilFound(use_serial=use_serial, timeout=timeout): |
852 return False | 875 return False |
853 if not self._OpenHandle(): | 876 if not self._OpenHandle(): |
854 return False | 877 return False |
855 return self._Connect(use_serial=use_serial) | 878 return self._Connect(use_serial=use_serial) |
856 | 879 |
857 def __repr__(self): | 880 def __repr__(self): |
858 return '<Device %s %s>' % ( | 881 return '<Device %s %s>' % ( |
859 self.port_path, self.serial if self.is_valid else '(broken)') | 882 self.port_path, self.serial if self.is_valid else '(broken)') |
OLD | NEW |