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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 adb_wrapper.AdbWrapper.StartServer() | 84 adb_wrapper.AdbWrapper.StartServer() |
85 if not timeout_retry.WaitFor(adb_started, wait_period=1, max_tries=5): | 85 if not timeout_retry.WaitFor(adb_started, wait_period=1, max_tries=5): |
86 raise device_errors.CommandFailedError('Failed to start adb server') | 86 raise device_errors.CommandFailedError('Failed to start adb server') |
87 | 87 |
88 | 88 |
89 def _GetTimeStamp(): | 89 def _GetTimeStamp(): |
90 """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.""" |
91 return time.strftime('%Y%m%dT%H%M%S', time.localtime()) | 91 return time.strftime('%Y%m%dT%H%M%S', time.localtime()) |
92 | 92 |
93 | 93 |
| 94 def _JoinLines(lines): |
| 95 # makes sure that the last line is also terminated, and is more memory |
| 96 # efficient than first appending an end-line to each line and then joining |
| 97 # all of them together. |
| 98 return ''.join(s for line in lines for s in (line, '\n')) |
| 99 |
| 100 |
94 class DeviceUtils(object): | 101 class DeviceUtils(object): |
95 | 102 |
96 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') | 103 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') |
97 | 104 |
98 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, | 105 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, |
99 default_retries=_DEFAULT_RETRIES): | 106 default_retries=_DEFAULT_RETRIES): |
100 """DeviceUtils constructor. | 107 """DeviceUtils constructor. |
101 | 108 |
102 Args: | 109 Args: |
103 device: Either a device serial, an existing AdbWrapper instance, or an | 110 device: Either a device serial, an existing AdbWrapper instance, or an |
(...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
873 CommandFailedError on failure. | 880 CommandFailedError on failure. |
874 CommandTimeoutError on timeout. | 881 CommandTimeoutError on timeout. |
875 """ | 882 """ |
876 # Create the base dir if it doesn't exist already | 883 # Create the base dir if it doesn't exist already |
877 dirname = os.path.dirname(host_path) | 884 dirname = os.path.dirname(host_path) |
878 if dirname and not os.path.exists(dirname): | 885 if dirname and not os.path.exists(dirname): |
879 os.makedirs(dirname) | 886 os.makedirs(dirname) |
880 self.adb.Pull(device_path, host_path) | 887 self.adb.Pull(device_path, host_path) |
881 | 888 |
882 @decorators.WithTimeoutAndRetriesFromInstance() | 889 @decorators.WithTimeoutAndRetriesFromInstance() |
883 def ReadFile(self, device_path, as_root=False, timeout=None, retries=None): | 890 def ReadFile(self, device_path, as_root=False, |
| 891 timeout=None, retries=None): |
884 """Reads the contents of a file from the device. | 892 """Reads the contents of a file from the device. |
885 | 893 |
886 Args: | 894 Args: |
887 device_path: A string containing the absolute path of the file to read | 895 device_path: A string containing the absolute path of the file to read |
888 from the device. | 896 from the device. |
889 as_root: A boolean indicating whether the read should be executed with | 897 as_root: A boolean indicating whether the read should be executed with |
890 root privileges. | 898 root privileges. |
891 timeout: timeout in seconds | 899 timeout: timeout in seconds |
892 retries: number of retries | 900 retries: number of retries |
893 | 901 |
894 Returns: | 902 Returns: |
895 The contents of the file at |device_path| as a list of lines. | 903 The contents of |device_path| as a string. Contents are intepreted using |
| 904 universal newlines, so the caller will see them encoded as '\n'. Also, |
| 905 all lines will be terminated. |
896 | 906 |
897 Raises: | 907 Raises: |
898 CommandFailedError if the file can't be read. | 908 AdbCommandFailedError if the file can't be read. |
899 CommandTimeoutError on timeout. | 909 CommandTimeoutError on timeout. |
900 DeviceUnreachableError on missing device. | 910 DeviceUnreachableError on missing device. |
901 """ | 911 """ |
902 # TODO(jbudorick) Evaluate whether we want to return a list of lines after | 912 return _JoinLines(self.RunShellCommand( |
903 # the implementation switch, and if file not found should raise exception. | 913 ['cat', device_path], as_root=as_root, check_return=True)) |
904 if as_root: | |
905 if not self.old_interface.CanAccessProtectedFileContents(): | |
906 raise device_errors.CommandFailedError( | |
907 'Cannot read from %s with root privileges.' % device_path) | |
908 return self.old_interface.GetProtectedFileContents(device_path) | |
909 else: | |
910 return self.old_interface.GetFileContents(device_path) | |
911 | 914 |
912 @decorators.WithTimeoutAndRetriesFromInstance() | 915 @decorators.WithTimeoutAndRetriesFromInstance() |
913 def WriteFile(self, device_path, contents, as_root=False, force_push=False, | 916 def WriteFile(self, device_path, contents, as_root=False, force_push=False, |
914 timeout=None, retries=None): | 917 timeout=None, retries=None): |
915 """Writes |contents| to a file on the device. | 918 """Writes |contents| to a file on the device. |
916 | 919 |
917 Args: | 920 Args: |
918 device_path: A string containing the absolute path to the file to write | 921 device_path: A string containing the absolute path to the file to write |
919 on the device. | 922 on the device. |
920 contents: A string containing the data to write to the device. | 923 contents: A string containing the data to write to the device. |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1285 Returns: | 1288 Returns: |
1286 A Parallelizer operating over |devices|. | 1289 A Parallelizer operating over |devices|. |
1287 """ | 1290 """ |
1288 if not devices: | 1291 if not devices: |
1289 devices = adb_wrapper.AdbWrapper.GetDevices() | 1292 devices = adb_wrapper.AdbWrapper.GetDevices() |
1290 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1293 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1291 if async: | 1294 if async: |
1292 return parallelizer.Parallelizer(devices) | 1295 return parallelizer.Parallelizer(devices) |
1293 else: | 1296 else: |
1294 return parallelizer.SyncParallelizer(devices) | 1297 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |