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