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=W0613 | 9 # pylint: disable=W0613 |
10 | 10 |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 blocking: A boolean indicating whether we should wait until all processes | 357 blocking: A boolean indicating whether we should wait until all processes |
358 with the given |process_name| are dead. | 358 with the given |process_name| are dead. |
359 timeout: timeout in seconds | 359 timeout: timeout in seconds |
360 retries: number of retries | 360 retries: number of retries |
361 | 361 |
362 Raises: | 362 Raises: |
363 CommandFailedError if no process was killed. | 363 CommandFailedError if no process was killed. |
364 CommandTimeoutError on timeout. | 364 CommandTimeoutError on timeout. |
365 DeviceUnreachableError on missing device. | 365 DeviceUnreachableError on missing device. |
366 """ | 366 """ |
367 pids = self.old_interface.ExtractPid(process_name) | 367 pids = self._GetPidsImpl(process_name) |
368 if len(pids) == 0: | 368 if not pids: |
369 raise device_errors.CommandFailedError( | 369 raise device_errors.CommandFailedError( |
370 'No process "%s"' % process_name, device=str(self)) | 370 'No process "%s"' % process_name, device=str(self)) |
371 | 371 |
| 372 cmd = 'kill -%d %s' % (signum, ' '.join(pids.values())) |
| 373 self._RunShellCommandImpl(cmd, as_root=as_root) |
| 374 |
372 if blocking: | 375 if blocking: |
373 total_killed = self.old_interface.KillAllBlocking( | 376 wait_period = 0.1 |
374 process_name, signum=signum, with_su=as_root, timeout_sec=timeout) | 377 while self._GetPidsImpl(process_name): |
375 else: | 378 time.sleep(wait_period) |
376 total_killed = self.old_interface.KillAll( | 379 |
377 process_name, signum=signum, with_su=as_root) | 380 return len(pids) |
378 if total_killed == 0: | |
379 raise device_errors.CommandFailedError( | |
380 'Failed to kill "%s"' % process_name, device=str(self)) | |
381 | 381 |
382 @decorators.WithTimeoutAndRetriesFromInstance() | 382 @decorators.WithTimeoutAndRetriesFromInstance() |
383 def StartActivity(self, intent, blocking=False, trace_file_name=None, | 383 def StartActivity(self, intent, blocking=False, trace_file_name=None, |
384 force_stop=False, timeout=None, retries=None): | 384 force_stop=False, timeout=None, retries=None): |
385 """Start package's activity on the device. | 385 """Start package's activity on the device. |
386 | 386 |
387 Args: | 387 Args: |
388 intent: An Intent to send. | 388 intent: An Intent to send. |
389 blocking: A boolean indicating whether we should wait for the activity to | 389 blocking: A boolean indicating whether we should wait for the activity to |
390 finish launching. | 390 finish launching. |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 retries: number of retries | 715 retries: number of retries |
716 | 716 |
717 Returns: | 717 Returns: |
718 A dict mapping process name to PID for each process that contained the | 718 A dict mapping process name to PID for each process that contained the |
719 provided |process_name|. | 719 provided |process_name|. |
720 | 720 |
721 Raises: | 721 Raises: |
722 CommandTimeoutError on timeout. | 722 CommandTimeoutError on timeout. |
723 DeviceUnreachableError on missing device. | 723 DeviceUnreachableError on missing device. |
724 """ | 724 """ |
| 725 return self._GetPidsImpl(process_name) |
| 726 |
| 727 def _GetPidsImpl(self, process_name): |
| 728 """Implementation of GetPids. |
| 729 |
| 730 This is split from GetPids to allow other DeviceUtils methods to call |
| 731 GetPids without spawning a new timeout thread. |
| 732 |
| 733 Args: |
| 734 process_name: A string containing the process name to get the PIDs for. |
| 735 |
| 736 Returns: |
| 737 A dict mapping process name to PID for each process that contained the |
| 738 provided |process_name|. |
| 739 |
| 740 Raises: |
| 741 DeviceUnreachableError on missing device. |
| 742 """ |
725 procs_pids = {} | 743 procs_pids = {} |
726 for line in self._RunShellCommandImpl('ps'): | 744 for line in self._RunShellCommandImpl('ps'): |
727 try: | 745 try: |
728 ps_data = line.split() | 746 ps_data = line.split() |
729 if process_name in ps_data[-1]: | 747 if process_name in ps_data[-1]: |
730 procs_pids[ps_data[-1]] = ps_data[1] | 748 procs_pids[ps_data[-1]] = ps_data[1] |
731 except IndexError: | 749 except IndexError: |
732 pass | 750 pass |
733 return procs_pids | 751 return procs_pids |
734 | 752 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 A Parallelizer operating over |devices|. | 833 A Parallelizer operating over |devices|. |
816 """ | 834 """ |
817 if not devices or len(devices) == 0: | 835 if not devices or len(devices) == 0: |
818 devices = pylib.android_commands.GetAttachedDevices() | 836 devices = pylib.android_commands.GetAttachedDevices() |
819 parallelizer_type = (parallelizer.Parallelizer if async | 837 parallelizer_type = (parallelizer.Parallelizer if async |
820 else parallelizer.SyncParallelizer) | 838 else parallelizer.SyncParallelizer) |
821 return parallelizer_type([ | 839 return parallelizer_type([ |
822 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 840 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
823 for d in devices]) | 841 for d in devices]) |
824 | 842 |
OLD | NEW |