Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: appengine/third_party/python-adb/adb/adb_commands_safe.py

Issue 1424923006: Small fixes as found in staging. (Closed) Base URL: git@github.com:luci/luci-py.git@4_more_functionality
Patch Set: . Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 327 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 338
339 i = 0 339 i = 0
340 for i in self._Loop(): 340 for i in self._Loop():
341 if not self._Reconnect(True): 341 if not self._Reconnect(True):
342 continue 342 continue
343 uptime = self.GetUptime() 343 uptime = self.GetUptime()
344 if uptime and uptime < previous_uptime: 344 if uptime and uptime < previous_uptime:
345 return True 345 return True
346 time.sleep(0.1) 346 time.sleep(0.1)
347 _LOG.error( 347 _LOG.error(
348 '%s.Reboot(): Failed to id after %d tries', self.port_path, i+1) 348 '%s.Reboot(): Failed to reboot after %d tries', self.port_path, i+1)
349 return False 349 return False
350 350
351 def Remount(self): 351 def Remount(self):
352 """Remount / as read-write.""" 352 """Remount / as read-write."""
353 if self._adb_cmd: 353 if self._adb_cmd:
354 for _ in self._Loop(): 354 for _ in self._Loop():
355 try: 355 try:
356 out = self._adb_cmd.Remount() 356 out = self._adb_cmd.Remount()
357 # TODO(maruel): Wait for the remount operation to be completed. 357 # TODO(maruel): Wait for the remount operation to be completed.
358 _LOG.info('%s.Remount(): %s', self.port_path, out) 358 _LOG.info('%s.Remount(): %s', self.port_path, out)
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 if exit_code != 0 or not out: 486 if exit_code != 0 or not out:
487 return None 487 return None
488 return out.startswith('uid=0(root)') 488 return out.startswith('uid=0(root)')
489 489
490 # Protected methods. 490 # Protected methods.
491 491
492 def _Reboot(self): 492 def _Reboot(self):
493 """Reboots the phone.""" 493 """Reboots the phone."""
494 i = 0 494 i = 0
495 for i in self._Loop(): 495 for i in self._Loop():
496 # TODO(maruel): It looks like it's possible that the device restart so
497 # fast that it throws an exception at the USB level. In that case there's
498 # no way to know if the command worked too fast or something went wrong
499 # and the command didn't go through.
496 try: 500 try:
497 out = self._adb_cmd.Reboot() 501 out = self._adb_cmd.Reboot()
498 except self._ERRORS as e: 502 except self._ERRORS as e:
499 if not self._Reset('(): %s', e, use_serial=True): 503 if not self._Reset('(): %s', e, use_serial=True):
500 break 504 break
501 continue 505 continue
502 506
503 _LOG.info('%s._Reboot(): %r', self.port_path, out) 507 _LOG.info('%s._Reboot(): %r', self.port_path, out)
504 if out == '': 508 if out == '':
505 # reboot doesn't reply anything. 509 # reboot doesn't reply anything.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 self._port_path = self._handle.port_path_str 581 self._port_path = self._handle.port_path_str
578 else: 582 else:
579 self._handle = common.UsbHandle.Find( 583 self._handle = common.UsbHandle.Find(
580 adb_commands.DeviceIsAvailable, port_path=self.port_path, 584 adb_commands.DeviceIsAvailable, port_path=self.port_path,
581 timeout_ms=self._default_timeout_ms) 585 timeout_ms=self._default_timeout_ms)
582 _LOG.info( 586 _LOG.info(
583 '%s._Find(%s) %s = %s', 587 '%s._Find(%s) %s = %s',
584 previous_port_path, use_serial, self._serial, 588 previous_port_path, use_serial, self._serial,
585 self.port_path if self._handle else 'None') 589 self.port_path if self._handle else 'None')
586 except (common.usb1.USBError, usb_exceptions.DeviceNotFoundError) as e: 590 except (common.usb1.USBError, usb_exceptions.DeviceNotFoundError) as e:
587 _LOG.info( 591 _LOG.debug(
588 '%s._Find(%s) %s : %s', self.port_path, use_serial, self._serial, e) 592 '%s._Find(%s) %s : %s', self.port_path, use_serial, self._serial, e)
593 return bool(self._handle)
589 594
590 def _WaitUntilFound(self, use_serial): 595 def _WaitUntilFound(self, use_serial):
591 """Loops until the device is found on the USB bus. 596 """Loops until the device is found on the USB bus.
592 597
593 The handle is left unopened. 598 The handle is left unopened.
594 599
595 This function should normally be called when either adbd or the phone is 600 This function should normally be called when either adbd or the phone is
596 rebooting. 601 rebooting.
597 """ 602 """
598 assert not self._handle 603 assert not self._handle
599 _LOG.debug('%s._WaitUntilFound(%s)', self.port_path, use_serial) 604 _LOG.debug('%s._WaitUntilFound(%s)', self.port_path, use_serial)
600 i = 0 605 i = 0
601 for i in self._Loop(): 606 for i in self._Loop():
602 try: 607 if self._Find(use_serial=use_serial):
603 self._Find(use_serial=use_serial)
604 return 608 return
605 except usb_exceptions.DeviceNotFoundError as e:
606 _LOG.info(
607 '%s._WaitUntilFound(): Retrying _Find() due to %s',
608 self.port_path, e)
609 _LOG.warning( 609 _LOG.warning(
610 '%s._WaitUntilFound() gave up after %d tries', self.port_path, i+1) 610 '%s._WaitUntilFound() gave up after %d tries', self.port_path, i+1)
611 611
612 def _OpenHandle(self): 612 def _OpenHandle(self):
613 """Opens the unopened self._handle.""" 613 """Opens the unopened self._handle."""
614 #_LOG.debug('%s._OpenHandle()', self.port_path) 614 #_LOG.debug('%s._OpenHandle()', self.port_path)
615 if self._handle: 615 if self._handle:
616 assert not self._handle.is_open 616 assert not self._handle.is_open
617 i = 0 617 i = 0
618 for i in self._Loop(): 618 for i in self._Loop():
619 try: 619 try:
620 # If this succeeds, this initializes self._handle._handle, which is a 620 # If this succeeds, this initializes self._handle._handle, which is a
621 # usb1.USBDeviceHandle. 621 # usb1.USBDeviceHandle.
622 self._handle.Open() 622 self._handle.Open()
623 break 623 break
624 except common.usb1.USBErrorNoDevice as e: 624 except common.usb1.USBErrorNoDevice as e:
625 _LOG.warning( 625 _LOG.warning(
626 '%s._OpenHandle(): USBErrorNoDevice: %s', self.port_path, e) 626 '%s._OpenHandle(): USBErrorNoDevice: %s', self.port_path, e)
627 # Do not kill adb, it just means the USB host is likely resetting and 627 # Do not kill adb, it just means the USB host is likely resetting and
628 # the device is temporarily unavailable. We can't use 628 # the device is temporarily unavailable. We can't use
629 # handle.serial_number since this communicates with the device. 629 # handle.serial_number since this communicates with the device.
630 except common.usb1.USBErrorBusy as e: 630 except common.usb1.USBErrorBusy as e:
631 _LOG.warning('%s._OpenHandle(): USBErrorBusy: %s', self.port_path, e) 631 _LOG.warning('%s._OpenHandle(): USBErrorBusy: %s', self.port_path, e)
632 KillADB() 632 KillADB()
633 except common.usb1.USBErrorAccess as e: 633 except common.usb1.USBErrorAccess as e:
634 # Do not try to use serial_number, since we can't even access the 634 # Do not try to use serial_number, since we can't even access the
635 # port. 635 # port.
636 _LOG.warning( 636 _LOG.warning(
637 '%s._OpenHandle(): Try rebooting the host: %s', self.port_path, e) 637 '%s._OpenHandle(): Try rebooting the host: %s', self.port_path, e)
638 self.Close()
ghost stip (do not use) 2015/11/03 18:50:46 *sigh* context manager, man
M-A Ruel 2015/11/04 18:39:10 It's in a loop, I changed the break at line 623 to
638 break 639 break
639 else: 640 else:
640 _LOG.error( 641 _LOG.error(
641 '%s._OpenHandle(): Failed after %d tries', self.port_path, i+1) 642 '%s._OpenHandle(): Failed after %d tries', self.port_path, i+1)
642 self.Close() 643 self.Close()
643 return bool(self._handle) 644 return bool(self._handle)
644 645
645 def _Connect(self, use_serial): 646 def _Connect(self, use_serial):
646 """Initializes self._adb_cmd from the opened self._handle. 647 """Initializes self._adb_cmd from the opened self._handle.
647 648
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 """ 736 """
736 self.Close() 737 self.Close()
737 self._WaitUntilFound(use_serial=use_serial) 738 self._WaitUntilFound(use_serial=use_serial)
738 if not self._OpenHandle(): 739 if not self._OpenHandle():
739 return False 740 return False
740 return self._Connect(use_serial=use_serial) 741 return self._Connect(use_serial=use_serial)
741 742
742 def __repr__(self): 743 def __repr__(self):
743 return '<Device %s %s>' % ( 744 return '<Device %s %s>' % (
744 self.port_path, self.serial if self.is_valid else '(broken)') 745 self.port_path, self.serial if self.is_valid else '(broken)')
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698