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