Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(748)

Side by Side Diff: build/android/pylib/device/device_utils.py

Issue 794583004: Reland of Migrate DeviceUtils.ReadFile to adb_wrapper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: command line file should be read as_root Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
70 class DeviceUtils(object): 77 class DeviceUtils(object):
71 78
72 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') 79 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
73 80
74 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, 81 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT,
75 default_retries=_DEFAULT_RETRIES): 82 default_retries=_DEFAULT_RETRIES):
76 """DeviceUtils constructor. 83 """DeviceUtils constructor.
77 84
78 Args: 85 Args:
79 device: Either a device serial, an existing AdbWrapper instance, or an 86 device: Either a device serial, an existing AdbWrapper instance, or an
(...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 CommandFailedError on failure. 858 CommandFailedError on failure.
852 CommandTimeoutError on timeout. 859 CommandTimeoutError on timeout.
853 """ 860 """
854 # Create the base dir if it doesn't exist already 861 # Create the base dir if it doesn't exist already
855 dirname = os.path.dirname(host_path) 862 dirname = os.path.dirname(host_path)
856 if dirname and not os.path.exists(dirname): 863 if dirname and not os.path.exists(dirname):
857 os.makedirs(dirname) 864 os.makedirs(dirname)
858 self.adb.Pull(device_path, host_path) 865 self.adb.Pull(device_path, host_path)
859 866
860 @decorators.WithTimeoutAndRetriesFromInstance() 867 @decorators.WithTimeoutAndRetriesFromInstance()
861 def ReadFile(self, device_path, as_root=False, timeout=None, retries=None): 868 def ReadFile(self, device_path, as_root=False,
869 timeout=None, retries=None):
862 """Reads the contents of a file from the device. 870 """Reads the contents of a file from the device.
863 871
864 Args: 872 Args:
865 device_path: A string containing the absolute path of the file to read 873 device_path: A string containing the absolute path of the file to read
866 from the device. 874 from the device.
867 as_root: A boolean indicating whether the read should be executed with 875 as_root: A boolean indicating whether the read should be executed with
868 root privileges. 876 root privileges.
869 timeout: timeout in seconds 877 timeout: timeout in seconds
870 retries: number of retries 878 retries: number of retries
871 879
872 Returns: 880 Returns:
873 The contents of the file at |device_path| as a list of lines. 881 The contents of |device_path| as a string. Contents are intepreted using
882 universal newlines, so the caller will see them encoded as '\n'. Also,
883 all lines will be terminated.
874 884
875 Raises: 885 Raises:
876 CommandFailedError if the file can't be read. 886 AdbCommandFailedError if the file can't be read.
877 CommandTimeoutError on timeout. 887 CommandTimeoutError on timeout.
878 DeviceUnreachableError on missing device. 888 DeviceUnreachableError on missing device.
879 """ 889 """
880 # TODO(jbudorick) Evaluate whether we want to return a list of lines after 890 return _JoinLines(self.RunShellCommand(
881 # the implementation switch, and if file not found should raise exception. 891 ['cat', device_path], as_root=as_root, check_return=True))
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)
889 892
890 @decorators.WithTimeoutAndRetriesFromInstance() 893 @decorators.WithTimeoutAndRetriesFromInstance()
891 def WriteFile(self, device_path, contents, as_root=False, force_push=False, 894 def WriteFile(self, device_path, contents, as_root=False, force_push=False,
892 timeout=None, retries=None): 895 timeout=None, retries=None):
893 """Writes |contents| to a file on the device. 896 """Writes |contents| to a file on the device.
894 897
895 Args: 898 Args:
896 device_path: A string containing the absolute path to the file to write 899 device_path: A string containing the absolute path to the file to write
897 on the device. 900 on the device.
898 contents: A string containing the data to write to the device. 901 contents: A string containing the data to write to the device.
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 Returns: 1193 Returns:
1191 A Parallelizer operating over |devices|. 1194 A Parallelizer operating over |devices|.
1192 """ 1195 """
1193 if not devices: 1196 if not devices:
1194 devices = adb_wrapper.AdbWrapper.GetDevices() 1197 devices = adb_wrapper.AdbWrapper.GetDevices()
1195 devices = [d if isinstance(d, cls) else cls(d) for d in devices] 1198 devices = [d if isinstance(d, cls) else cls(d) for d in devices]
1196 if async: 1199 if async:
1197 return parallelizer.Parallelizer(devices) 1200 return parallelizer.Parallelizer(devices)
1198 else: 1201 else:
1199 return parallelizer.SyncParallelizer(devices) 1202 return parallelizer.SyncParallelizer(devices)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698