| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | 49 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
| 50 def RestartServer(): | 50 def RestartServer(): |
| 51 """Restarts the adb server. | 51 """Restarts the adb server. |
| 52 | 52 |
| 53 Raises: | 53 Raises: |
| 54 CommandFailedError if we fail to kill or restart the server. | 54 CommandFailedError if we fail to kill or restart the server. |
| 55 """ | 55 """ |
| 56 pylib.android_commands.AndroidCommands().RestartAdbServer() | 56 pylib.android_commands.AndroidCommands().RestartAdbServer() |
| 57 | 57 |
| 58 | 58 |
| 59 def _JoinLines(lines): |
| 60 # makes sure that the last line is also terminated, and is more memory |
| 61 # efficient than first appending an end-line to each line and then joining |
| 62 # all of them together. |
| 63 return ''.join(s for line in lines for s in (line, '\n')) |
| 64 |
| 65 |
| 59 class DeviceUtils(object): | 66 class DeviceUtils(object): |
| 60 | 67 |
| 61 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') | 68 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') |
| 62 | 69 |
| 63 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, | 70 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, |
| 64 default_retries=_DEFAULT_RETRIES): | 71 default_retries=_DEFAULT_RETRIES): |
| 65 """DeviceUtils constructor. | 72 """DeviceUtils constructor. |
| 66 | 73 |
| 67 Args: | 74 Args: |
| 68 device: Either a device serial, an existing AdbWrapper instance, or an | 75 device: Either a device serial, an existing AdbWrapper instance, or an |
| (...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 850 CommandFailedError on failure. | 857 CommandFailedError on failure. |
| 851 CommandTimeoutError on timeout. | 858 CommandTimeoutError on timeout. |
| 852 """ | 859 """ |
| 853 try: | 860 try: |
| 854 self.old_interface.PullFileFromDevice(device_path, host_path) | 861 self.old_interface.PullFileFromDevice(device_path, host_path) |
| 855 except AssertionError as e: | 862 except AssertionError as e: |
| 856 raise device_errors.CommandFailedError( | 863 raise device_errors.CommandFailedError( |
| 857 str(e), str(self)), None, sys.exc_info()[2] | 864 str(e), str(self)), None, sys.exc_info()[2] |
| 858 | 865 |
| 859 @decorators.WithTimeoutAndRetriesFromInstance() | 866 @decorators.WithTimeoutAndRetriesFromInstance() |
| 860 def ReadFile(self, device_path, as_root=False, timeout=None, retries=None): | 867 def ReadFile(self, device_path, as_root=False, |
| 868 timeout=None, retries=None): |
| 861 """Reads the contents of a file from the device. | 869 """Reads the contents of a file from the device. |
| 862 | 870 |
| 863 Args: | 871 Args: |
| 864 device_path: A string containing the absolute path of the file to read | 872 device_path: A string containing the absolute path of the file to read |
| 865 from the device. | 873 from the device. |
| 866 as_root: A boolean indicating whether the read should be executed with | 874 as_root: A boolean indicating whether the read should be executed with |
| 867 root privileges. | 875 root privileges. |
| 868 timeout: timeout in seconds | 876 timeout: timeout in seconds |
| 869 retries: number of retries | 877 retries: number of retries |
| 870 | 878 |
| 871 Returns: | 879 Returns: |
| 872 The contents of the file at |device_path| as a list of lines. | 880 The contents of |device_path| as a string. Contents are intepreted using |
| 881 universal newlines, so the caller will see them encoded as '\n'. Also, |
| 882 all lines will be terminated. |
| 873 | 883 |
| 874 Raises: | 884 Raises: |
| 875 CommandFailedError if the file can't be read. | 885 AdbCommandFailedError if the file can't be read. |
| 876 CommandTimeoutError on timeout. | 886 CommandTimeoutError on timeout. |
| 877 DeviceUnreachableError on missing device. | 887 DeviceUnreachableError on missing device. |
| 878 """ | 888 """ |
| 879 # TODO(jbudorick) Evaluate whether we want to return a list of lines after | 889 return _JoinLines(self.RunShellCommand( |
| 880 # the implementation switch, and if file not found should raise exception. | 890 ['cat', device_path], as_root=as_root, check_return=True)) |
| 881 if as_root: | |
| 882 if not self.old_interface.CanAccessProtectedFileContents(): | |
| 883 raise device_errors.CommandFailedError( | |
| 884 'Cannot read from %s with root privileges.' % device_path) | |
| 885 return self.old_interface.GetProtectedFileContents(device_path) | |
| 886 else: | |
| 887 return self.old_interface.GetFileContents(device_path) | |
| 888 | 891 |
| 889 @decorators.WithTimeoutAndRetriesFromInstance() | 892 @decorators.WithTimeoutAndRetriesFromInstance() |
| 890 def WriteFile(self, device_path, contents, as_root=False, force_push=False, | 893 def WriteFile(self, device_path, contents, as_root=False, force_push=False, |
| 891 timeout=None, retries=None): | 894 timeout=None, retries=None): |
| 892 """Writes |contents| to a file on the device. | 895 """Writes |contents| to a file on the device. |
| 893 | 896 |
| 894 Args: | 897 Args: |
| 895 device_path: A string containing the absolute path to the file to write | 898 device_path: A string containing the absolute path to the file to write |
| 896 on the device. | 899 on the device. |
| 897 contents: A string containing the data to write to the device. | 900 contents: A string containing the data to write to the device. |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1161 Returns: | 1164 Returns: |
| 1162 A Parallelizer operating over |devices|. | 1165 A Parallelizer operating over |devices|. |
| 1163 """ | 1166 """ |
| 1164 if not devices or len(devices) == 0: | 1167 if not devices or len(devices) == 0: |
| 1165 devices = pylib.android_commands.GetAttachedDevices() | 1168 devices = pylib.android_commands.GetAttachedDevices() |
| 1166 parallelizer_type = (parallelizer.Parallelizer if async | 1169 parallelizer_type = (parallelizer.Parallelizer if async |
| 1167 else parallelizer.SyncParallelizer) | 1170 else parallelizer.SyncParallelizer) |
| 1168 return parallelizer_type([ | 1171 return parallelizer_type([ |
| 1169 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 1172 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
| 1170 for d in devices]) | 1173 for d in devices]) |
| OLD | NEW |