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 |
11 import logging | 11 import logging |
12 import multiprocessing | 12 import multiprocessing |
13 import os | 13 import os |
14 import re | 14 import re |
15 import sys | 15 import sys |
16 import tempfile | 16 import tempfile |
17 import time | 17 import time |
18 import zipfile | 18 import zipfile |
19 | 19 |
20 import pylib.android_commands | 20 import pylib.android_commands |
21 from pylib import cmd_helper | 21 from pylib import cmd_helper |
22 from pylib import constants | 22 from pylib import constants |
23 from pylib.device import adb_wrapper | 23 from pylib.device import adb_wrapper |
24 from pylib.device import decorators | 24 from pylib.device import decorators |
25 from pylib.device import device_errors | 25 from pylib.device import device_errors |
| 26 from pylib.device import intent |
26 from pylib.device.commands import install_commands | 27 from pylib.device.commands import install_commands |
27 from pylib.utils import apk_helper | 28 from pylib.utils import apk_helper |
28 from pylib.utils import device_temp_file | 29 from pylib.utils import device_temp_file |
29 from pylib.utils import host_utils | 30 from pylib.utils import host_utils |
30 from pylib.utils import md5sum | 31 from pylib.utils import md5sum |
31 from pylib.utils import parallelizer | 32 from pylib.utils import parallelizer |
32 from pylib.utils import timeout_retry | 33 from pylib.utils import timeout_retry |
| 34 from pylib.utils import zip_utils |
33 | 35 |
34 _DEFAULT_TIMEOUT = 30 | 36 _DEFAULT_TIMEOUT = 30 |
35 _DEFAULT_RETRIES = 3 | 37 _DEFAULT_RETRIES = 3 |
36 | 38 |
37 # A sentinel object for default values | 39 # A sentinel object for default values |
38 # TODO(jbudorick,perezju): revisit how default values are handled by | 40 # TODO(jbudorick,perezju): revisit how default values are handled by |
39 # the timeout_retry decorators. | 41 # the timeout_retry decorators. |
40 DEFAULT = object() | 42 DEFAULT = object() |
41 | 43 |
42 | 44 |
(...skipping 19 matching lines...) Expand all Loading... |
62 | 64 |
63 | 65 |
64 @decorators.WithExplicitTimeoutAndRetries( | 66 @decorators.WithExplicitTimeoutAndRetries( |
65 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | 67 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
66 def RestartServer(): | 68 def RestartServer(): |
67 """Restarts the adb server. | 69 """Restarts the adb server. |
68 | 70 |
69 Raises: | 71 Raises: |
70 CommandFailedError if we fail to kill or restart the server. | 72 CommandFailedError if we fail to kill or restart the server. |
71 """ | 73 """ |
72 pylib.android_commands.AndroidCommands().RestartAdbServer() | 74 def adb_killed(): |
| 75 return not adb_wrapper.AdbWrapper.IsServerOnline() |
| 76 |
| 77 def adb_started(): |
| 78 return adb_wrapper.AdbWrapper.IsServerOnline() |
| 79 |
| 80 adb_wrapper.AdbWrapper.KillServer() |
| 81 if not timeout_retry.WaitFor(adb_killed, wait_period=1, max_tries=5): |
| 82 # TODO(perezju): raise an exception after fixng http://crbug.com/442319 |
| 83 logging.warning('Failed to kill adb server') |
| 84 adb_wrapper.AdbWrapper.StartServer() |
| 85 if not timeout_retry.WaitFor(adb_started, wait_period=1, max_tries=5): |
| 86 raise device_errors.CommandFailedError('Failed to start adb server') |
73 | 87 |
74 | 88 |
75 def _GetTimeStamp(): | 89 def _GetTimeStamp(): |
76 """Return a basic ISO 8601 time stamp with the current local time.""" | 90 """Return a basic ISO 8601 time stamp with the current local time.""" |
77 return time.strftime('%Y%m%dT%H%M%S', time.localtime()) | 91 return time.strftime('%Y%m%dT%H%M%S', time.localtime()) |
78 | 92 |
79 | 93 |
80 class DeviceUtils(object): | 94 class DeviceUtils(object): |
81 | 95 |
82 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') | 96 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 """Return to the home screen. | 594 """Return to the home screen. |
581 | 595 |
582 Args: | 596 Args: |
583 timeout: timeout in seconds | 597 timeout: timeout in seconds |
584 retries: number of retries | 598 retries: number of retries |
585 | 599 |
586 Raises: | 600 Raises: |
587 CommandTimeoutError on timeout. | 601 CommandTimeoutError on timeout. |
588 DeviceUnreachableError on missing device. | 602 DeviceUnreachableError on missing device. |
589 """ | 603 """ |
590 self.old_interface.GoHome() | 604 self.StartActivity( |
| 605 intent.Intent(action='android.intent.action.MAIN', |
| 606 category='android.intent.category.HOME'), |
| 607 blocking=True) |
591 | 608 |
592 @decorators.WithTimeoutAndRetriesFromInstance() | 609 @decorators.WithTimeoutAndRetriesFromInstance() |
593 def ForceStop(self, package, timeout=None, retries=None): | 610 def ForceStop(self, package, timeout=None, retries=None): |
594 """Close the application. | 611 """Close the application. |
595 | 612 |
596 Args: | 613 Args: |
597 package: A string containing the name of the package to stop. | 614 package: A string containing the name of the package to stop. |
598 timeout: timeout in seconds | 615 timeout: timeout in seconds |
599 retries: number of retries | 616 retries: number of retries |
600 | 617 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 finally: | 825 finally: |
809 if zip_proc.is_alive(): | 826 if zip_proc.is_alive(): |
810 zip_proc.terminate() | 827 zip_proc.terminate() |
811 if self.IsOnline(): | 828 if self.IsOnline(): |
812 self.RunShellCommand(['rm', zip_on_device], check_return=True) | 829 self.RunShellCommand(['rm', zip_on_device], check_return=True) |
813 | 830 |
814 @staticmethod | 831 @staticmethod |
815 def _CreateDeviceZip(zip_path, host_device_tuples): | 832 def _CreateDeviceZip(zip_path, host_device_tuples): |
816 with zipfile.ZipFile(zip_path, 'w') as zip_file: | 833 with zipfile.ZipFile(zip_path, 'w') as zip_file: |
817 for host_path, device_path in host_device_tuples: | 834 for host_path, device_path in host_device_tuples: |
818 if os.path.isfile(host_path): | 835 zip_utils.WriteToZipFile(zip_file, host_path, device_path) |
819 zip_file.write(host_path, device_path, zipfile.ZIP_DEFLATED) | |
820 else: | |
821 for hd, _, files in os.walk(host_path): | |
822 dd = '%s/%s' % (device_path, os.path.relpath(host_path, hd)) | |
823 zip_file.write(hd, dd, zipfile.ZIP_STORED) | |
824 for f in files: | |
825 zip_file.write(os.path.join(hd, f), '%s/%s' % (dd, f), | |
826 zipfile.ZIP_DEFLATED) | |
827 | 836 |
828 @decorators.WithTimeoutAndRetriesFromInstance() | 837 @decorators.WithTimeoutAndRetriesFromInstance() |
829 def FileExists(self, device_path, timeout=None, retries=None): | 838 def FileExists(self, device_path, timeout=None, retries=None): |
830 """Checks whether the given file exists on the device. | 839 """Checks whether the given file exists on the device. |
831 | 840 |
832 Args: | 841 Args: |
833 device_path: A string containing the absolute path to the file on the | 842 device_path: A string containing the absolute path to the file on the |
834 device. | 843 device. |
835 timeout: timeout in seconds | 844 timeout: timeout in seconds |
836 retries: number of retries | 845 retries: number of retries |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1276 Returns: | 1285 Returns: |
1277 A Parallelizer operating over |devices|. | 1286 A Parallelizer operating over |devices|. |
1278 """ | 1287 """ |
1279 if not devices: | 1288 if not devices: |
1280 devices = adb_wrapper.AdbWrapper.GetDevices() | 1289 devices = adb_wrapper.AdbWrapper.GetDevices() |
1281 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1290 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1282 if async: | 1291 if async: |
1283 return parallelizer.Parallelizer(devices) | 1292 return parallelizer.Parallelizer(devices) |
1284 else: | 1293 else: |
1285 return parallelizer.SyncParallelizer(devices) | 1294 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |