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

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

Issue 744753002: Reland of '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 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
11 import logging 11 import logging
12 import multiprocessing 12 import multiprocessing
13 import os 13 import os
14 import re 14 import re
15 import sys 15 import sys
16 import tempfile 16 import tempfile
17 import time 17 import time
18 import zipfile 18 import zipfile
19 19
20 import pylib.android_commands 20 import pylib.android_commands
21 from pylib import cmd_helper 21 from pylib import cmd_helper
22 from pylib.device import adb_wrapper 22 from pylib.device import adb_wrapper
23 from pylib.device import decorators 23 from pylib.device import decorators
24 from pylib.device import device_errors 24 from pylib.device import device_errors
25 from pylib.device.commands import install_commands 25 from pylib.device.commands import install_commands
26 from pylib.utils import apk_helper 26 from pylib.utils import apk_helper
27 from pylib.utils import device_temp_file
27 from pylib.utils import host_utils 28 from pylib.utils import host_utils
28 from pylib.utils import parallelizer 29 from pylib.utils import parallelizer
29 from pylib.utils import timeout_retry 30 from pylib.utils import timeout_retry
30 31
31 _DEFAULT_TIMEOUT = 30 32 _DEFAULT_TIMEOUT = 30
32 _DEFAULT_RETRIES = 3 33 _DEFAULT_RETRIES = 3
33 34
34 35
35 @decorators.WithExplicitTimeoutAndRetries( 36 @decorators.WithExplicitTimeoutAndRetries(
36 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) 37 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES)
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 DeviceUnreachableError on missing device. 423 DeviceUnreachableError on missing device.
423 """ 424 """
424 def env_quote(key, value): 425 def env_quote(key, value):
425 if not DeviceUtils._VALID_SHELL_VARIABLE.match(key): 426 if not DeviceUtils._VALID_SHELL_VARIABLE.match(key):
426 raise KeyError('Invalid shell variable name %r' % key) 427 raise KeyError('Invalid shell variable name %r' % key)
427 # using double quotes here to allow interpolation of shell variables 428 # using double quotes here to allow interpolation of shell variables
428 return '%s=%s' % (key, cmd_helper.DoubleQuote(value)) 429 return '%s=%s' % (key, cmd_helper.DoubleQuote(value))
429 430
430 if not isinstance(cmd, basestring): 431 if not isinstance(cmd, basestring):
431 cmd = ' '.join(cmd_helper.SingleQuote(s) for s in cmd) 432 cmd = ' '.join(cmd_helper.SingleQuote(s) for s in cmd)
432 if as_root and self.NeedsSU():
433 cmd = 'su -c %s' % cmd
434 if env: 433 if env:
435 env = ' '.join(env_quote(k, v) for k, v in env.iteritems()) 434 env = ' '.join(env_quote(k, v) for k, v in env.iteritems())
436 cmd = '%s %s' % (env, cmd) 435 cmd = '%s %s' % (env, cmd)
437 if cwd: 436 if cwd:
438 cmd = 'cd %s && %s' % (cmd_helper.SingleQuote(cwd), cmd) 437 cmd = 'cd %s && %s' % (cmd_helper.SingleQuote(cwd), cmd)
438 if as_root and self.NeedsSU():
439 # "su -c sh -c" allows using shell features in |cmd|
440 cmd = 'su -c sh -c %s' % cmd_helper.SingleQuote(cmd)
439 if timeout is None: 441 if timeout is None:
440 timeout = self._default_timeout 442 timeout = self._default_timeout
441 443
442 try: 444 try:
443 output = self.adb.Shell(cmd) 445 output = self.adb.Shell(cmd)
444 except device_errors.AdbShellCommandFailedError as e: 446 except device_errors.AdbShellCommandFailedError as e:
445 if check_return: 447 if check_return:
446 raise 448 raise
447 else: 449 else:
448 output = e.output 450 output = e.output
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 # the implementation switch, and if file not found should raise exception. 877 # the implementation switch, and if file not found should raise exception.
876 if as_root: 878 if as_root:
877 if not self.old_interface.CanAccessProtectedFileContents(): 879 if not self.old_interface.CanAccessProtectedFileContents():
878 raise device_errors.CommandFailedError( 880 raise device_errors.CommandFailedError(
879 'Cannot read from %s with root privileges.' % device_path) 881 'Cannot read from %s with root privileges.' % device_path)
880 return self.old_interface.GetProtectedFileContents(device_path) 882 return self.old_interface.GetProtectedFileContents(device_path)
881 else: 883 else:
882 return self.old_interface.GetFileContents(device_path) 884 return self.old_interface.GetFileContents(device_path)
883 885
884 @decorators.WithTimeoutAndRetriesFromInstance() 886 @decorators.WithTimeoutAndRetriesFromInstance()
885 def WriteFile(self, device_path, contents, as_root=False, timeout=None, 887 def WriteFile(self, device_path, contents, as_root=False, force_push=False,
886 retries=None): 888 timeout=None, retries=None):
887 """Writes |contents| to a file on the device. 889 """Writes |contents| to a file on the device.
888 890
889 Args: 891 Args:
890 device_path: A string containing the absolute path to the file to write 892 device_path: A string containing the absolute path to the file to write
891 on the device. 893 on the device.
892 contents: A string containing the data to write to the device. 894 contents: A string containing the data to write to the device.
893 as_root: A boolean indicating whether the write should be executed with 895 as_root: A boolean indicating whether the write should be executed with
894 root privileges. 896 root privileges (if available).
897 force_push: A boolean indicating whether to force the operation to be
898 performed by pushing a file to the device. The default is, when the
899 contents are short, to pass the contents using a shell script instead.
895 timeout: timeout in seconds 900 timeout: timeout in seconds
896 retries: number of retries 901 retries: number of retries
897 902
898 Raises: 903 Raises:
899 CommandFailedError if the file could not be written on the device. 904 CommandFailedError if the file could not be written on the device.
900 CommandTimeoutError on timeout. 905 CommandTimeoutError on timeout.
901 DeviceUnreachableError on missing device. 906 DeviceUnreachableError on missing device.
902 """ 907 """
903 if as_root: 908 if len(contents) < 512 and not force_push:
904 if not self.old_interface.CanAccessProtectedFileContents(): 909 cmd = 'echo -n %s > %s' % (cmd_helper.SingleQuote(contents),
905 raise device_errors.CommandFailedError( 910 cmd_helper.SingleQuote(device_path))
906 'Cannot write to %s with root privileges.' % device_path) 911 self.RunShellCommand(cmd, as_root=as_root, check_return=True)
907 self.old_interface.SetProtectedFileContents(device_path, contents)
908 else: 912 else:
909 self.old_interface.SetFileContents(device_path, contents) 913 with tempfile.NamedTemporaryFile() as host_temp:
910 914 host_temp.write(contents)
911 @decorators.WithTimeoutAndRetriesFromInstance() 915 host_temp.flush()
912 def WriteTextFile(self, device_path, text, as_root=False, timeout=None, 916 if as_root and self.NeedsSU():
913 retries=None): 917 with device_temp_file.DeviceTempFile(self) as device_temp:
914 """Writes |text| to a file on the device. 918 self.adb.Push(host_temp.name, device_temp.name)
915 919 # Here we need 'cp' rather than 'mv' because the temp and
916 Assuming that |text| is a small string, this is typically more efficient 920 # destination files might be on different file systems (e.g.
917 than |WriteFile|, as no files are pushed into the device. 921 # on internal storage and an external sd card)
918 922 self.RunShellCommand(['cp', device_temp.name, device_path],
919 Args: 923 as_root=True, check_return=True)
920 device_path: A string containing the absolute path to the file to write 924 else:
921 on the device. 925 self.adb.Push(host_temp.name, device_path)
922 text: A short string of text to write to the file on the device.
923 as_root: A boolean indicating whether the write should be executed with
924 root privileges.
925 timeout: timeout in seconds
926 retries: number of retries
927
928 Raises:
929 CommandFailedError if the file could not be written on the device.
930 CommandTimeoutError on timeout.
931 DeviceUnreachableError on missing device.
932 """
933 cmd = 'echo %s > %s' % (cmd_helper.SingleQuote(text),
934 cmd_helper.SingleQuote(device_path))
935 self.RunShellCommand(cmd, as_root=as_root, check_return=True)
936 926
937 @decorators.WithTimeoutAndRetriesFromInstance() 927 @decorators.WithTimeoutAndRetriesFromInstance()
938 def Ls(self, device_path, timeout=None, retries=None): 928 def Ls(self, device_path, timeout=None, retries=None):
939 """Lists the contents of a directory on the device. 929 """Lists the contents of a directory on the device.
940 930
941 Args: 931 Args:
942 device_path: A string containing the path of the directory on the device 932 device_path: A string containing the path of the directory on the device
943 to list. 933 to list.
944 timeout: timeout in seconds 934 timeout: timeout in seconds
945 retries: number of retries 935 retries: number of retries
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
1166 Returns: 1156 Returns:
1167 A Parallelizer operating over |devices|. 1157 A Parallelizer operating over |devices|.
1168 """ 1158 """
1169 if not devices or len(devices) == 0: 1159 if not devices or len(devices) == 0:
1170 devices = pylib.android_commands.GetAttachedDevices() 1160 devices = pylib.android_commands.GetAttachedDevices()
1171 parallelizer_type = (parallelizer.Parallelizer if async 1161 parallelizer_type = (parallelizer.Parallelizer if async
1172 else parallelizer.SyncParallelizer) 1162 else parallelizer.SyncParallelizer)
1173 return parallelizer_type([ 1163 return parallelizer_type([
1174 d if isinstance(d, DeviceUtils) else DeviceUtils(d) 1164 d if isinstance(d, DeviceUtils) else DeviceUtils(d)
1175 for d in devices]) 1165 for d in devices])
OLDNEW
« no previous file with comments | « build/android/pylib/device/commands/install_commands.py ('k') | build/android/pylib/device/device_utils_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698