Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Provides a variety of device interactions based on adb. | 5 """Provides a variety of device interactions based on adb. |
| 6 | 6 |
| 7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
| 8 """ | 8 """ |
| 9 # pylint: disable=unused-argument | 9 # pylint: disable=unused-argument |
| 10 | 10 |
| (...skipping 718 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 729 retries: number of retries | 729 retries: number of retries |
| 730 | 730 |
| 731 Returns: | 731 Returns: |
| 732 The number of processes attempted to kill. | 732 The number of processes attempted to kill. |
| 733 | 733 |
| 734 Raises: | 734 Raises: |
| 735 CommandFailedError if no process was killed and |quiet| is False. | 735 CommandFailedError if no process was killed and |quiet| is False. |
| 736 CommandTimeoutError on timeout. | 736 CommandTimeoutError on timeout. |
| 737 DeviceUnreachableError on missing device. | 737 DeviceUnreachableError on missing device. |
| 738 """ | 738 """ |
| 739 pids = self.GetPids(process_name) | 739 pids = [pid for (_, pid) in self._GetProcessPidPairs(process_name)] |
| 740 if not pids: | 740 if not pids: |
| 741 if quiet: | 741 if quiet: |
| 742 return 0 | 742 return 0 |
| 743 else: | 743 else: |
| 744 raise device_errors.CommandFailedError( | 744 raise device_errors.CommandFailedError( |
| 745 'No process "%s"' % process_name, str(self)) | 745 'No process "%s"' % process_name, str(self)) |
| 746 | 746 |
| 747 cmd = ['kill', '-%d' % signum] + pids.values() | 747 cmd = ['kill', '-%d' % signum] + pids |
| 748 self.RunShellCommand(cmd, as_root=as_root, check_return=True) | 748 self.RunShellCommand(cmd, as_root=as_root, check_return=True) |
| 749 | 749 |
| 750 if blocking: | 750 if blocking: |
| 751 # TODO(perezu): use timeout_retry.WaitFor | 751 # TODO(perezu): use timeout_retry.WaitFor |
| 752 wait_period = 0.1 | 752 wait_period = 0.1 |
| 753 while self.GetPids(process_name): | 753 while self._GetProcessPidPairs(process_name): |
| 754 time.sleep(wait_period) | 754 time.sleep(wait_period) |
| 755 | 755 |
| 756 return len(pids) | 756 return len(pids) |
| 757 | 757 |
| 758 @decorators.WithTimeoutAndRetriesFromInstance() | 758 @decorators.WithTimeoutAndRetriesFromInstance() |
| 759 def StartActivity(self, intent_obj, blocking=False, trace_file_name=None, | 759 def StartActivity(self, intent_obj, blocking=False, trace_file_name=None, |
| 760 force_stop=False, timeout=None, retries=None): | 760 force_stop=False, timeout=None, retries=None): |
| 761 """Start package's activity on the device. | 761 """Start package's activity on the device. |
| 762 | 762 |
| 763 Args: | 763 Args: |
| (...skipping 793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1557 retries: number of retries | 1557 retries: number of retries |
| 1558 | 1558 |
| 1559 Returns: | 1559 Returns: |
| 1560 The device's main ABI name. | 1560 The device's main ABI name. |
| 1561 | 1561 |
| 1562 Raises: | 1562 Raises: |
| 1563 CommandTimeoutError on timeout. | 1563 CommandTimeoutError on timeout. |
| 1564 """ | 1564 """ |
| 1565 return self.GetProp('ro.product.cpu.abi') | 1565 return self.GetProp('ro.product.cpu.abi') |
| 1566 | 1566 |
| 1567 @decorators.WithTimeoutAndRetriesFromInstance() | 1567 def _GetProcessPidPairs(self, process_name): |
|
jbudorick
2015/07/24 17:10:56
I think I'd prefer making a breaking change to the
| |
| 1568 def GetPids(self, process_name, timeout=None, retries=None): | 1568 """Returns a list of (process, pid) pairs for all matching processes. |
| 1569 """Returns the PIDs of processes with the given name. | |
| 1570 | 1569 |
| 1571 Note that the |process_name| is often the package name. | 1570 Note that the |process_name| is often the package name. |
| 1572 | 1571 |
| 1573 Args: | 1572 Args: |
| 1574 process_name: A string containing the process name to get the PIDs for. | 1573 process_name: A string containing the process name to get the PIDs for. |
| 1575 timeout: timeout in seconds | |
| 1576 retries: number of retries | |
| 1577 | 1574 |
| 1578 Returns: | 1575 Returns: |
| 1579 A dict mapping process name to PID for each process that contained the | 1576 A list with (process, pid) pairs for all processes that contained the |
| 1580 provided |process_name|. | 1577 provided |process_name|. |
| 1581 | 1578 |
| 1582 Raises: | 1579 Raises: |
| 1583 CommandTimeoutError on timeout. | 1580 CommandTimeoutError on timeout. |
| 1584 DeviceUnreachableError on missing device. | 1581 DeviceUnreachableError on missing device. |
| 1585 """ | 1582 """ |
| 1586 procs_pids = {} | 1583 procs_pids = [] |
| 1587 try: | 1584 try: |
| 1588 ps_output = self._RunPipedShellCommand( | 1585 ps_output = self._RunPipedShellCommand( |
| 1589 'ps | grep -F %s' % cmd_helper.SingleQuote(process_name)) | 1586 'ps | grep -F %s' % cmd_helper.SingleQuote(process_name)) |
| 1590 except device_errors.AdbShellCommandFailedError as e: | 1587 except device_errors.AdbShellCommandFailedError as e: |
| 1591 if e.status and isinstance(e.status, list) and not e.status[0]: | 1588 if e.status and isinstance(e.status, list) and not e.status[0]: |
| 1592 # If ps succeeded but grep failed, there were no processes with the | 1589 # If ps succeeded but grep failed, there were no processes with the |
| 1593 # given name. | 1590 # given name. |
| 1594 return procs_pids | 1591 return procs_pids |
| 1595 else: | 1592 else: |
| 1596 raise | 1593 raise |
| 1597 | 1594 |
| 1598 for line in ps_output: | 1595 for line in ps_output: |
| 1599 try: | 1596 try: |
| 1600 ps_data = line.split() | 1597 ps_data = line.split() |
| 1601 if process_name in ps_data[-1]: | 1598 if process_name in ps_data[-1]: |
| 1602 procs_pids[ps_data[-1]] = ps_data[1] | 1599 pid, process = ps_data[1], ps_data[-1] |
| 1600 procs_pids.append((process, pid)) | |
| 1603 except IndexError: | 1601 except IndexError: |
| 1604 pass | 1602 pass |
| 1605 return procs_pids | 1603 return procs_pids |
| 1606 | 1604 |
| 1607 @decorators.WithTimeoutAndRetriesFromInstance() | 1605 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1606 def GetPids(self, process_name, timeout=None, retries=None): | |
| 1607 """Returns the PIDs of processes with the given name. | |
| 1608 | |
| 1609 Note that the |process_name| is often the package name. | |
| 1610 | |
| 1611 If there are multiple instances of the same process, only one of the PIDs | |
| 1612 will be returned. | |
| 1613 | |
| 1614 Args: | |
| 1615 process_name: A string containing the process name to get the PIDs for. | |
| 1616 timeout: timeout in seconds | |
| 1617 retries: number of retries | |
| 1618 | |
| 1619 Returns: | |
| 1620 A dict mapping process name to PID for each process that contained the | |
| 1621 provided |process_name|. | |
| 1622 | |
| 1623 Raises: | |
| 1624 CommandTimeoutError on timeout. | |
| 1625 DeviceUnreachableError on missing device. | |
| 1626 """ | |
| 1627 return dict(self._GetProcessPidPairs(process_name)) | |
| 1628 | |
| 1629 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 1608 def TakeScreenshot(self, host_path=None, timeout=None, retries=None): | 1630 def TakeScreenshot(self, host_path=None, timeout=None, retries=None): |
| 1609 """Takes a screenshot of the device. | 1631 """Takes a screenshot of the device. |
| 1610 | 1632 |
| 1611 Args: | 1633 Args: |
| 1612 host_path: A string containing the path on the host to save the | 1634 host_path: A string containing the path on the host to save the |
| 1613 screenshot to. If None, a file name in the current | 1635 screenshot to. If None, a file name in the current |
| 1614 directory will be generated. | 1636 directory will be generated. |
| 1615 timeout: timeout in seconds | 1637 timeout: timeout in seconds |
| 1616 retries: number of retries | 1638 retries: number of retries |
| 1617 | 1639 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1745 def HealthyDevices(cls): | 1767 def HealthyDevices(cls): |
| 1746 blacklist = device_blacklist.ReadBlacklist() | 1768 blacklist = device_blacklist.ReadBlacklist() |
| 1747 def blacklisted(adb): | 1769 def blacklisted(adb): |
| 1748 if adb.GetDeviceSerial() in blacklist: | 1770 if adb.GetDeviceSerial() in blacklist: |
| 1749 logging.warning('Device %s is blacklisted.', adb.GetDeviceSerial()) | 1771 logging.warning('Device %s is blacklisted.', adb.GetDeviceSerial()) |
| 1750 return True | 1772 return True |
| 1751 return False | 1773 return False |
| 1752 | 1774 |
| 1753 return [cls(adb) for adb in adb_wrapper.AdbWrapper.Devices() | 1775 return [cls(adb) for adb in adb_wrapper.AdbWrapper.Devices() |
| 1754 if not blacklisted(adb)] | 1776 if not blacklisted(adb)] |
| OLD | NEW |