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

Unified Diff: build/android/pylib/device/device_utils.py

Issue 715853002: Migrate device utils WriteFile to AdbWrapper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: build/android/pylib/device/device_utils.py
diff --git a/build/android/pylib/device/device_utils.py b/build/android/pylib/device/device_utils.py
index b75a41a2f1a2401e98982ad044386ec517984fff..a5915845ba470e3ec4de1354e262b2769edf5fed 100644
--- a/build/android/pylib/device/device_utils.py
+++ b/build/android/pylib/device/device_utils.py
@@ -24,6 +24,7 @@ from pylib.device import decorators
from pylib.device import device_errors
from pylib.device.commands import install_commands
from pylib.utils import apk_helper
+from pylib.utils import device_temp_file
from pylib.utils import host_utils
from pylib.utils import parallelizer
from pylib.utils import timeout_retry
@@ -866,16 +867,19 @@ class DeviceUtils(object):
return self.old_interface.GetFileContents(device_path)
@decorators.WithTimeoutAndRetriesFromInstance()
- def WriteFile(self, device_path, contents, as_root=False, timeout=None,
- retries=None):
+ def WriteFile(self, device_path, contents, as_root=False, force_push=False,
+ timeout=None, retries=None):
"""Writes |contents| to a file on the device.
Args:
device_path: A string containing the absolute path to the file to write
- on the device.
+ on the device.
contents: A string containing the data to write to the device.
as_root: A boolean indicating whether the write should be executed with
- root privileges.
+ root privileges (if available).
+ force_push: A boolean indicating whether to force the operation to be
+ performed by pushing a file to the device. The default is, when the
+ contents are short, to pass the contents using a shell script instead.
timeout: timeout in seconds
retries: number of retries
@@ -884,39 +888,21 @@ class DeviceUtils(object):
CommandTimeoutError on timeout.
DeviceUnreachableError on missing device.
"""
- if as_root:
- if not self.old_interface.CanAccessProtectedFileContents():
- raise device_errors.CommandFailedError(
- 'Cannot write to %s with root privileges.' % device_path)
- self.old_interface.SetProtectedFileContents(device_path, contents)
+ if len(contents) < 512 and not force_push:
+ cmd = 'echo %s > %s' % (cmd_helper.SingleQuote(contents),
+ cmd_helper.SingleQuote(device_path))
+ self.RunShellCommand(cmd, as_root=as_root, check_return=True)
else:
- self.old_interface.SetFileContents(device_path, contents)
-
- @decorators.WithTimeoutAndRetriesFromInstance()
- def WriteTextFile(self, device_path, text, as_root=False, timeout=None,
- retries=None):
- """Writes |text| to a file on the device.
-
- Assuming that |text| is a small string, this is typically more efficient
- than |WriteFile|, as no files are pushed into the device.
-
- Args:
- device_path: A string containing the absolute path to the file to write
- on the device.
- text: A short string of text to write to the file on the device.
- as_root: A boolean indicating whether the write should be executed with
- root privileges.
- timeout: timeout in seconds
- retries: number of retries
-
- Raises:
- CommandFailedError if the file could not be written on the device.
- CommandTimeoutError on timeout.
- DeviceUnreachableError on missing device.
- """
- cmd = 'echo %s > %s' % (cmd_helper.SingleQuote(text),
- cmd_helper.SingleQuote(device_path))
- self.RunShellCommand(cmd, as_root=as_root, check_return=True)
+ with tempfile.NamedTemporaryFile() as host_temp:
+ host_temp.write(contents)
+ host_temp.flush()
+ if as_root and self.NeedsSU():
+ with device_temp_file.DeviceTempFile(self) as device_temp:
+ self.adb.Push(host_temp.name, device_temp.name)
+ self.RunShellCommand(['mv', device_temp.name, device_path],
jbudorick 2014/11/11 23:16:03 Is DeviceTempFile ok with its temporary file getti
perezju 2014/11/11 23:26:33 The way it's now implemented, it should be fine. B
+ as_root=True, check_return=True)
+ else:
+ self.adb.Push(host_temp.name, device_path)
@decorators.WithTimeoutAndRetriesFromInstance()
def Ls(self, device_path, timeout=None, retries=None):

Powered by Google App Engine
This is Rietveld 408576698