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 = self.GetPids(process_name, multiple_instances=True) |
| 740 pids = [pid for instances in pids.values() for pid in instances] | |
| 740 if not pids: | 741 if not pids: |
| 741 if quiet: | 742 if quiet: |
| 742 return 0 | 743 return 0 |
| 743 else: | 744 else: |
| 744 raise device_errors.CommandFailedError( | 745 raise device_errors.CommandFailedError( |
| 745 'No process "%s"' % process_name, str(self)) | 746 'No process "%s"' % process_name, str(self)) |
| 746 | 747 |
| 747 cmd = ['kill', '-%d' % signum] + pids.values() | 748 cmd = ['kill', '-%d' % signum] + pids |
| 748 self.RunShellCommand(cmd, as_root=as_root, check_return=True) | 749 self.RunShellCommand(cmd, as_root=as_root, check_return=True) |
| 749 | 750 |
| 750 if blocking: | 751 if blocking: |
| 751 # TODO(perezu): use timeout_retry.WaitFor | 752 # TODO(perezu): use timeout_retry.WaitFor |
| 752 wait_period = 0.1 | 753 wait_period = 0.1 |
| 753 while self.GetPids(process_name): | 754 while self.GetPids(process_name, multiple_instances=True): |
| 754 time.sleep(wait_period) | 755 time.sleep(wait_period) |
| 755 | 756 |
| 756 return len(pids) | 757 return len(pids) |
| 757 | 758 |
| 758 @decorators.WithTimeoutAndRetriesFromInstance() | 759 @decorators.WithTimeoutAndRetriesFromInstance() |
| 759 def StartActivity(self, intent_obj, blocking=False, trace_file_name=None, | 760 def StartActivity(self, intent_obj, blocking=False, trace_file_name=None, |
| 760 force_stop=False, timeout=None, retries=None): | 761 force_stop=False, timeout=None, retries=None): |
| 761 """Start package's activity on the device. | 762 """Start package's activity on the device. |
| 762 | 763 |
| 763 Args: | 764 Args: |
| (...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1558 | 1559 |
| 1559 Returns: | 1560 Returns: |
| 1560 The device's main ABI name. | 1561 The device's main ABI name. |
| 1561 | 1562 |
| 1562 Raises: | 1563 Raises: |
| 1563 CommandTimeoutError on timeout. | 1564 CommandTimeoutError on timeout. |
| 1564 """ | 1565 """ |
| 1565 return self.GetProp('ro.product.cpu.abi') | 1566 return self.GetProp('ro.product.cpu.abi') |
| 1566 | 1567 |
| 1567 @decorators.WithTimeoutAndRetriesFromInstance() | 1568 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1568 def GetPids(self, process_name, timeout=None, retries=None): | 1569 def GetPids(self, process_name, timeout=None, retries=None, |
| 1570 multiple_instances=False): | |
|
perezju
2015/07/24 14:03:10
I don't think John will like adding an extra optio
Sami
2015/07/24 14:36:40
Yeah I wasn't super happy about the flag either. T
| |
| 1569 """Returns the PIDs of processes with the given name. | 1571 """Returns the PIDs of processes with the given name. |
| 1570 | 1572 |
| 1571 Note that the |process_name| is often the package name. | 1573 Note that the |process_name| is often the package name. |
| 1572 | 1574 |
| 1573 Args: | 1575 Args: |
| 1574 process_name: A string containing the process name to get the PIDs for. | 1576 process_name: A string containing the process name to get the PIDs for. |
| 1575 timeout: timeout in seconds | 1577 timeout: timeout in seconds |
| 1576 retries: number of retries | 1578 retries: number of retries |
| 1579 multiple_instances: Whether or not multiple instances of the same process | |
| 1580 binary are supported. If True, the returned dict will have a list of | |
| 1581 PIDs for each found process. If False, only a single matching PID | |
| 1582 will be returned for each process. | |
| 1577 | 1583 |
| 1578 Returns: | 1584 Returns: |
| 1579 A dict mapping process name to PID for each process that contained the | 1585 A dict mapping process name to PID (or list of PIDs) for each process |
| 1580 provided |process_name|. | 1586 that contained the provided |process_name|. |
| 1581 | 1587 |
| 1582 Raises: | 1588 Raises: |
| 1583 CommandTimeoutError on timeout. | 1589 CommandTimeoutError on timeout. |
| 1584 DeviceUnreachableError on missing device. | 1590 DeviceUnreachableError on missing device. |
| 1585 """ | 1591 """ |
| 1586 procs_pids = {} | 1592 procs_pids = collections.defaultdict(list) if multiple_instances else {} |
| 1587 try: | 1593 try: |
| 1588 ps_output = self._RunPipedShellCommand( | 1594 ps_output = self._RunPipedShellCommand( |
| 1589 'ps | grep -F %s' % cmd_helper.SingleQuote(process_name)) | 1595 'ps | grep -F %s' % cmd_helper.SingleQuote(process_name)) |
| 1590 except device_errors.AdbShellCommandFailedError as e: | 1596 except device_errors.AdbShellCommandFailedError as e: |
| 1591 if e.status and isinstance(e.status, list) and not e.status[0]: | 1597 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 | 1598 # If ps succeeded but grep failed, there were no processes with the |
| 1593 # given name. | 1599 # given name. |
| 1594 return procs_pids | 1600 return procs_pids |
| 1595 else: | 1601 else: |
| 1596 raise | 1602 raise |
| 1597 | 1603 |
| 1598 for line in ps_output: | 1604 for line in ps_output: |
| 1599 try: | 1605 try: |
| 1600 ps_data = line.split() | 1606 ps_data = line.split() |
| 1601 if process_name in ps_data[-1]: | 1607 if process_name in ps_data[-1]: |
| 1602 procs_pids[ps_data[-1]] = ps_data[1] | 1608 binary_name = ps_data[-1] |
| 1609 if multiple_instances: | |
| 1610 procs_pids[binary_name].append(ps_data[1]) | |
| 1611 else: | |
| 1612 procs_pids[binary_name] = ps_data[1] | |
| 1603 except IndexError: | 1613 except IndexError: |
| 1604 pass | 1614 pass |
| 1605 return procs_pids | 1615 return procs_pids |
| 1606 | 1616 |
| 1607 @decorators.WithTimeoutAndRetriesFromInstance() | 1617 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1608 def TakeScreenshot(self, host_path=None, timeout=None, retries=None): | 1618 def TakeScreenshot(self, host_path=None, timeout=None, retries=None): |
| 1609 """Takes a screenshot of the device. | 1619 """Takes a screenshot of the device. |
| 1610 | 1620 |
| 1611 Args: | 1621 Args: |
| 1612 host_path: A string containing the path on the host to save the | 1622 host_path: A string containing the path on the host to save the |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1745 def HealthyDevices(cls): | 1755 def HealthyDevices(cls): |
| 1746 blacklist = device_blacklist.ReadBlacklist() | 1756 blacklist = device_blacklist.ReadBlacklist() |
| 1747 def blacklisted(adb): | 1757 def blacklisted(adb): |
| 1748 if adb.GetDeviceSerial() in blacklist: | 1758 if adb.GetDeviceSerial() in blacklist: |
| 1749 logging.warning('Device %s is blacklisted.', adb.GetDeviceSerial()) | 1759 logging.warning('Device %s is blacklisted.', adb.GetDeviceSerial()) |
| 1750 return True | 1760 return True |
| 1751 return False | 1761 return False |
| 1752 | 1762 |
| 1753 return [cls(adb) for adb in adb_wrapper.AdbWrapper.Devices() | 1763 return [cls(adb) for adb in adb_wrapper.AdbWrapper.Devices() |
| 1754 if not blacklisted(adb)] | 1764 if not blacklisted(adb)] |
| OLD | NEW |