| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | 65 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
| 66 def RestartServer(): | 66 def RestartServer(): |
| 67 """Restarts the adb server. | 67 """Restarts the adb server. |
| 68 | 68 |
| 69 Raises: | 69 Raises: |
| 70 CommandFailedError if we fail to kill or restart the server. | 70 CommandFailedError if we fail to kill or restart the server. |
| 71 """ | 71 """ |
| 72 pylib.android_commands.AndroidCommands().RestartAdbServer() | 72 pylib.android_commands.AndroidCommands().RestartAdbServer() |
| 73 | 73 |
| 74 | 74 |
| 75 def _JoinLines(lines): |
| 76 # makes sure that the last line is also terminated, and is more memory |
| 77 # efficient than first appending an end-line to each line and then joining |
| 78 # all of them together. |
| 79 return ''.join(s for line in lines for s in (line, '\n')) |
| 80 |
| 81 |
| 75 class DeviceUtils(object): | 82 class DeviceUtils(object): |
| 76 | 83 |
| 77 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') | 84 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') |
| 78 | 85 |
| 79 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, | 86 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, |
| 80 default_retries=_DEFAULT_RETRIES): | 87 default_retries=_DEFAULT_RETRIES): |
| 81 """DeviceUtils constructor. | 88 """DeviceUtils constructor. |
| 82 | 89 |
| 83 Args: | 90 Args: |
| 84 device: Either a device serial, an existing AdbWrapper instance, or an | 91 device: Either a device serial, an existing AdbWrapper instance, or an |
| (...skipping 779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 864 CommandFailedError on failure. | 871 CommandFailedError on failure. |
| 865 CommandTimeoutError on timeout. | 872 CommandTimeoutError on timeout. |
| 866 """ | 873 """ |
| 867 # Create the base dir if it doesn't exist already | 874 # Create the base dir if it doesn't exist already |
| 868 dirname = os.path.dirname(host_path) | 875 dirname = os.path.dirname(host_path) |
| 869 if dirname and not os.path.exists(dirname): | 876 if dirname and not os.path.exists(dirname): |
| 870 os.makedirs(dirname) | 877 os.makedirs(dirname) |
| 871 self.adb.Pull(device_path, host_path) | 878 self.adb.Pull(device_path, host_path) |
| 872 | 879 |
| 873 @decorators.WithTimeoutAndRetriesFromInstance() | 880 @decorators.WithTimeoutAndRetriesFromInstance() |
| 874 def ReadFile(self, device_path, as_root=False, timeout=None, retries=None): | 881 def ReadFile(self, device_path, as_root=False, |
| 882 timeout=None, retries=None): |
| 875 """Reads the contents of a file from the device. | 883 """Reads the contents of a file from the device. |
| 876 | 884 |
| 877 Args: | 885 Args: |
| 878 device_path: A string containing the absolute path of the file to read | 886 device_path: A string containing the absolute path of the file to read |
| 879 from the device. | 887 from the device. |
| 880 as_root: A boolean indicating whether the read should be executed with | 888 as_root: A boolean indicating whether the read should be executed with |
| 881 root privileges. | 889 root privileges. |
| 882 timeout: timeout in seconds | 890 timeout: timeout in seconds |
| 883 retries: number of retries | 891 retries: number of retries |
| 884 | 892 |
| 885 Returns: | 893 Returns: |
| 886 The contents of the file at |device_path| as a list of lines. | 894 The contents of |device_path| as a string. Contents are intepreted using |
| 895 universal newlines, so the caller will see them encoded as '\n'. Also, |
| 896 all lines will be terminated. |
| 887 | 897 |
| 888 Raises: | 898 Raises: |
| 889 CommandFailedError if the file can't be read. | 899 AdbCommandFailedError if the file can't be read. |
| 890 CommandTimeoutError on timeout. | 900 CommandTimeoutError on timeout. |
| 891 DeviceUnreachableError on missing device. | 901 DeviceUnreachableError on missing device. |
| 892 """ | 902 """ |
| 893 # TODO(jbudorick) Evaluate whether we want to return a list of lines after | 903 return _JoinLines(self.RunShellCommand( |
| 894 # the implementation switch, and if file not found should raise exception. | 904 ['cat', device_path], as_root=as_root, check_return=True)) |
| 895 if as_root: | |
| 896 if not self.old_interface.CanAccessProtectedFileContents(): | |
| 897 raise device_errors.CommandFailedError( | |
| 898 'Cannot read from %s with root privileges.' % device_path) | |
| 899 return self.old_interface.GetProtectedFileContents(device_path) | |
| 900 else: | |
| 901 return self.old_interface.GetFileContents(device_path) | |
| 902 | 905 |
| 903 @decorators.WithTimeoutAndRetriesFromInstance() | 906 @decorators.WithTimeoutAndRetriesFromInstance() |
| 904 def WriteFile(self, device_path, contents, as_root=False, force_push=False, | 907 def WriteFile(self, device_path, contents, as_root=False, force_push=False, |
| 905 timeout=None, retries=None): | 908 timeout=None, retries=None): |
| 906 """Writes |contents| to a file on the device. | 909 """Writes |contents| to a file on the device. |
| 907 | 910 |
| 908 Args: | 911 Args: |
| 909 device_path: A string containing the absolute path to the file to write | 912 device_path: A string containing the absolute path to the file to write |
| 910 on the device. | 913 on the device. |
| 911 contents: A string containing the data to write to the device. | 914 contents: A string containing the data to write to the device. |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1269 Returns: | 1272 Returns: |
| 1270 A Parallelizer operating over |devices|. | 1273 A Parallelizer operating over |devices|. |
| 1271 """ | 1274 """ |
| 1272 if not devices: | 1275 if not devices: |
| 1273 devices = adb_wrapper.AdbWrapper.GetDevices() | 1276 devices = adb_wrapper.AdbWrapper.GetDevices() |
| 1274 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1277 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
| 1275 if async: | 1278 if async: |
| 1276 return parallelizer.Parallelizer(devices) | 1279 return parallelizer.Parallelizer(devices) |
| 1277 else: | 1280 else: |
| 1278 return parallelizer.SyncParallelizer(devices) | 1281 return parallelizer.SyncParallelizer(devices) |
| OLD | NEW |