Chromium Code Reviews| 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 |
| 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 822 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 859 # the implementation switch, and if file not found should raise exception. | 860 # the implementation switch, and if file not found should raise exception. |
| 860 if as_root: | 861 if as_root: |
| 861 if not self.old_interface.CanAccessProtectedFileContents(): | 862 if not self.old_interface.CanAccessProtectedFileContents(): |
| 862 raise device_errors.CommandFailedError( | 863 raise device_errors.CommandFailedError( |
| 863 'Cannot read from %s with root privileges.' % device_path) | 864 'Cannot read from %s with root privileges.' % device_path) |
| 864 return self.old_interface.GetProtectedFileContents(device_path) | 865 return self.old_interface.GetProtectedFileContents(device_path) |
| 865 else: | 866 else: |
| 866 return self.old_interface.GetFileContents(device_path) | 867 return self.old_interface.GetFileContents(device_path) |
| 867 | 868 |
| 868 @decorators.WithTimeoutAndRetriesFromInstance() | 869 @decorators.WithTimeoutAndRetriesFromInstance() |
| 869 def WriteFile(self, device_path, contents, as_root=False, timeout=None, | 870 def WriteFile(self, device_path, contents, as_root=False, force_push=False, |
| 870 retries=None): | 871 timeout=None, retries=None): |
| 871 """Writes |contents| to a file on the device. | 872 """Writes |contents| to a file on the device. |
| 872 | 873 |
| 873 Args: | 874 Args: |
| 874 device_path: A string containing the absolute path to the file to write | 875 device_path: A string containing the absolute path to the file to write |
| 875 on the device. | 876 on the device. |
| 876 contents: A string containing the data to write to the device. | 877 contents: A string containing the data to write to the device. |
| 877 as_root: A boolean indicating whether the write should be executed with | 878 as_root: A boolean indicating whether the write should be executed with |
| 878 root privileges. | 879 root privileges (if available). |
| 880 force_push: A boolean indicating whether to force the operation to be | |
| 881 performed by pushing a file to the device. The default is, when the | |
| 882 contents are short, to pass the contents using a shell script instead. | |
| 879 timeout: timeout in seconds | 883 timeout: timeout in seconds |
| 880 retries: number of retries | 884 retries: number of retries |
| 881 | 885 |
| 882 Raises: | 886 Raises: |
| 883 CommandFailedError if the file could not be written on the device. | 887 CommandFailedError if the file could not be written on the device. |
| 884 CommandTimeoutError on timeout. | 888 CommandTimeoutError on timeout. |
| 885 DeviceUnreachableError on missing device. | 889 DeviceUnreachableError on missing device. |
| 886 """ | 890 """ |
| 887 if as_root: | 891 if len(contents) < 512 and not force_push: |
| 888 if not self.old_interface.CanAccessProtectedFileContents(): | 892 cmd = 'echo %s > %s' % (cmd_helper.SingleQuote(contents), |
| 889 raise device_errors.CommandFailedError( | 893 cmd_helper.SingleQuote(device_path)) |
| 890 'Cannot write to %s with root privileges.' % device_path) | 894 self.RunShellCommand(cmd, as_root=as_root, check_return=True) |
| 891 self.old_interface.SetProtectedFileContents(device_path, contents) | |
| 892 else: | 895 else: |
| 893 self.old_interface.SetFileContents(device_path, contents) | 896 with tempfile.NamedTemporaryFile() as host_temp: |
| 894 | 897 host_temp.write(contents) |
| 895 @decorators.WithTimeoutAndRetriesFromInstance() | 898 host_temp.flush() |
| 896 def WriteTextFile(self, device_path, text, as_root=False, timeout=None, | 899 if as_root and self.NeedsSU(): |
| 897 retries=None): | 900 with device_temp_file.DeviceTempFile(self) as device_temp: |
| 898 """Writes |text| to a file on the device. | 901 self.adb.Push(host_temp.name, device_temp.name) |
| 899 | 902 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
| |
| 900 Assuming that |text| is a small string, this is typically more efficient | 903 as_root=True, check_return=True) |
| 901 than |WriteFile|, as no files are pushed into the device. | 904 else: |
| 902 | 905 self.adb.Push(host_temp.name, device_path) |
| 903 Args: | |
| 904 device_path: A string containing the absolute path to the file to write | |
| 905 on the device. | |
| 906 text: A short string of text to write to the file on the device. | |
| 907 as_root: A boolean indicating whether the write should be executed with | |
| 908 root privileges. | |
| 909 timeout: timeout in seconds | |
| 910 retries: number of retries | |
| 911 | |
| 912 Raises: | |
| 913 CommandFailedError if the file could not be written on the device. | |
| 914 CommandTimeoutError on timeout. | |
| 915 DeviceUnreachableError on missing device. | |
| 916 """ | |
| 917 cmd = 'echo %s > %s' % (cmd_helper.SingleQuote(text), | |
| 918 cmd_helper.SingleQuote(device_path)) | |
| 919 self.RunShellCommand(cmd, as_root=as_root, check_return=True) | |
| 920 | 906 |
| 921 @decorators.WithTimeoutAndRetriesFromInstance() | 907 @decorators.WithTimeoutAndRetriesFromInstance() |
| 922 def Ls(self, device_path, timeout=None, retries=None): | 908 def Ls(self, device_path, timeout=None, retries=None): |
| 923 """Lists the contents of a directory on the device. | 909 """Lists the contents of a directory on the device. |
| 924 | 910 |
| 925 Args: | 911 Args: |
| 926 device_path: A string containing the path of the directory on the device | 912 device_path: A string containing the path of the directory on the device |
| 927 to list. | 913 to list. |
| 928 timeout: timeout in seconds | 914 timeout: timeout in seconds |
| 929 retries: number of retries | 915 retries: number of retries |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1150 Returns: | 1136 Returns: |
| 1151 A Parallelizer operating over |devices|. | 1137 A Parallelizer operating over |devices|. |
| 1152 """ | 1138 """ |
| 1153 if not devices or len(devices) == 0: | 1139 if not devices or len(devices) == 0: |
| 1154 devices = pylib.android_commands.GetAttachedDevices() | 1140 devices = pylib.android_commands.GetAttachedDevices() |
| 1155 parallelizer_type = (parallelizer.Parallelizer if async | 1141 parallelizer_type = (parallelizer.Parallelizer if async |
| 1156 else parallelizer.SyncParallelizer) | 1142 else parallelizer.SyncParallelizer) |
| 1157 return parallelizer_type([ | 1143 return parallelizer_type([ |
| 1158 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 1144 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
| 1159 for d in devices]) | 1145 for d in devices]) |
| OLD | NEW |