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 772 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 CommandFailedError on failure. | 883 CommandFailedError on failure. |
877 CommandTimeoutError on timeout. | 884 CommandTimeoutError on timeout. |
878 """ | 885 """ |
879 # Create the base dir if it doesn't exist already | 886 # Create the base dir if it doesn't exist already |
880 dirname = os.path.dirname(host_path) | 887 dirname = os.path.dirname(host_path) |
881 if dirname and not os.path.exists(dirname): | 888 if dirname and not os.path.exists(dirname): |
882 os.makedirs(dirname) | 889 os.makedirs(dirname) |
883 self.adb.Pull(device_path, host_path) | 890 self.adb.Pull(device_path, host_path) |
884 | 891 |
885 @decorators.WithTimeoutAndRetriesFromInstance() | 892 @decorators.WithTimeoutAndRetriesFromInstance() |
886 def ReadFile(self, device_path, as_root=False, timeout=None, retries=None): | 893 def ReadFile(self, device_path, as_root=False, |
| 894 timeout=None, retries=None): |
887 """Reads the contents of a file from the device. | 895 """Reads the contents of a file from the device. |
888 | 896 |
889 Args: | 897 Args: |
890 device_path: A string containing the absolute path of the file to read | 898 device_path: A string containing the absolute path of the file to read |
891 from the device. | 899 from the device. |
892 as_root: A boolean indicating whether the read should be executed with | 900 as_root: A boolean indicating whether the read should be executed with |
893 root privileges. | 901 root privileges. |
894 timeout: timeout in seconds | 902 timeout: timeout in seconds |
895 retries: number of retries | 903 retries: number of retries |
896 | 904 |
897 Returns: | 905 Returns: |
898 The contents of the file at |device_path| as a list of lines. | 906 The contents of |device_path| as a string. Contents are intepreted using |
| 907 universal newlines, so the caller will see them encoded as '\n'. Also, |
| 908 all lines will be terminated. |
899 | 909 |
900 Raises: | 910 Raises: |
901 CommandFailedError if the file can't be read. | 911 AdbCommandFailedError if the file can't be read. |
902 CommandTimeoutError on timeout. | 912 CommandTimeoutError on timeout. |
903 DeviceUnreachableError on missing device. | 913 DeviceUnreachableError on missing device. |
904 """ | 914 """ |
905 # TODO(jbudorick) Evaluate whether we want to return a list of lines after | 915 return _JoinLines(self.RunShellCommand( |
906 # the implementation switch, and if file not found should raise exception. | 916 ['cat', device_path], as_root=as_root, check_return=True)) |
907 if as_root: | |
908 if not self.old_interface.CanAccessProtectedFileContents(): | |
909 raise device_errors.CommandFailedError( | |
910 'Cannot read from %s with root privileges.' % device_path) | |
911 return self.old_interface.GetProtectedFileContents(device_path) | |
912 else: | |
913 return self.old_interface.GetFileContents(device_path) | |
914 | 917 |
915 @decorators.WithTimeoutAndRetriesFromInstance() | 918 @decorators.WithTimeoutAndRetriesFromInstance() |
916 def WriteFile(self, device_path, contents, as_root=False, force_push=False, | 919 def WriteFile(self, device_path, contents, as_root=False, force_push=False, |
917 timeout=None, retries=None): | 920 timeout=None, retries=None): |
918 """Writes |contents| to a file on the device. | 921 """Writes |contents| to a file on the device. |
919 | 922 |
920 Args: | 923 Args: |
921 device_path: A string containing the absolute path to the file to write | 924 device_path: A string containing the absolute path to the file to write |
922 on the device. | 925 on the device. |
923 contents: A string containing the data to write to the device. | 926 contents: A string containing the data to write to the device. |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1288 Returns: | 1291 Returns: |
1289 A Parallelizer operating over |devices|. | 1292 A Parallelizer operating over |devices|. |
1290 """ | 1293 """ |
1291 if not devices: | 1294 if not devices: |
1292 devices = adb_wrapper.AdbWrapper.GetDevices() | 1295 devices = adb_wrapper.AdbWrapper.GetDevices() |
1293 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1296 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1294 if async: | 1297 if async: |
1295 return parallelizer.Parallelizer(devices) | 1298 return parallelizer.Parallelizer(devices) |
1296 else: | 1299 else: |
1297 return parallelizer.SyncParallelizer(devices) | 1300 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |