| 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 | |
| 28 from pylib.utils import host_utils | 27 from pylib.utils import host_utils |
| 29 from pylib.utils import parallelizer | 28 from pylib.utils import parallelizer |
| 30 from pylib.utils import timeout_retry | 29 from pylib.utils import timeout_retry |
| 31 | 30 |
| 32 _DEFAULT_TIMEOUT = 30 | 31 _DEFAULT_TIMEOUT = 30 |
| 33 _DEFAULT_RETRIES = 3 | 32 _DEFAULT_RETRIES = 3 |
| 34 | 33 |
| 35 | 34 |
| 36 @decorators.WithExplicitTimeoutAndRetries( | 35 @decorators.WithExplicitTimeoutAndRetries( |
| 37 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | 36 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 DeviceUnreachableError on missing device. | 422 DeviceUnreachableError on missing device. |
| 424 """ | 423 """ |
| 425 def env_quote(key, value): | 424 def env_quote(key, value): |
| 426 if not DeviceUtils._VALID_SHELL_VARIABLE.match(key): | 425 if not DeviceUtils._VALID_SHELL_VARIABLE.match(key): |
| 427 raise KeyError('Invalid shell variable name %r' % key) | 426 raise KeyError('Invalid shell variable name %r' % key) |
| 428 # using double quotes here to allow interpolation of shell variables | 427 # using double quotes here to allow interpolation of shell variables |
| 429 return '%s=%s' % (key, cmd_helper.DoubleQuote(value)) | 428 return '%s=%s' % (key, cmd_helper.DoubleQuote(value)) |
| 430 | 429 |
| 431 if not isinstance(cmd, basestring): | 430 if not isinstance(cmd, basestring): |
| 432 cmd = ' '.join(cmd_helper.SingleQuote(s) for s in cmd) | 431 cmd = ' '.join(cmd_helper.SingleQuote(s) for s in cmd) |
| 432 if as_root and self.NeedsSU(): |
| 433 cmd = 'su -c %s' % cmd |
| 433 if env: | 434 if env: |
| 434 env = ' '.join(env_quote(k, v) for k, v in env.iteritems()) | 435 env = ' '.join(env_quote(k, v) for k, v in env.iteritems()) |
| 435 cmd = '%s %s' % (env, cmd) | 436 cmd = '%s %s' % (env, cmd) |
| 436 if cwd: | 437 if cwd: |
| 437 cmd = 'cd %s && %s' % (cmd_helper.SingleQuote(cwd), cmd) | 438 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) | |
| 441 if timeout is None: | 439 if timeout is None: |
| 442 timeout = self._default_timeout | 440 timeout = self._default_timeout |
| 443 | 441 |
| 444 try: | 442 try: |
| 445 output = self.adb.Shell(cmd) | 443 output = self.adb.Shell(cmd) |
| 446 except device_errors.AdbShellCommandFailedError as e: | 444 except device_errors.AdbShellCommandFailedError as e: |
| 447 if check_return: | 445 if check_return: |
| 448 raise | 446 raise |
| 449 else: | 447 else: |
| 450 output = e.output | 448 output = e.output |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 861 # the implementation switch, and if file not found should raise exception. | 859 # the implementation switch, and if file not found should raise exception. |
| 862 if as_root: | 860 if as_root: |
| 863 if not self.old_interface.CanAccessProtectedFileContents(): | 861 if not self.old_interface.CanAccessProtectedFileContents(): |
| 864 raise device_errors.CommandFailedError( | 862 raise device_errors.CommandFailedError( |
| 865 'Cannot read from %s with root privileges.' % device_path) | 863 'Cannot read from %s with root privileges.' % device_path) |
| 866 return self.old_interface.GetProtectedFileContents(device_path) | 864 return self.old_interface.GetProtectedFileContents(device_path) |
| 867 else: | 865 else: |
| 868 return self.old_interface.GetFileContents(device_path) | 866 return self.old_interface.GetFileContents(device_path) |
| 869 | 867 |
| 870 @decorators.WithTimeoutAndRetriesFromInstance() | 868 @decorators.WithTimeoutAndRetriesFromInstance() |
| 871 def WriteFile(self, device_path, contents, as_root=False, force_push=False, | 869 def WriteFile(self, device_path, contents, as_root=False, timeout=None, |
| 872 timeout=None, retries=None): | 870 retries=None): |
| 873 """Writes |contents| to a file on the device. | 871 """Writes |contents| to a file on the device. |
| 874 | 872 |
| 875 Args: | 873 Args: |
| 876 device_path: A string containing the absolute path to the file to write | 874 device_path: A string containing the absolute path to the file to write |
| 877 on the device. | 875 on the device. |
| 878 contents: A string containing the data to write to the device. | 876 contents: A string containing the data to write to the device. |
| 879 as_root: A boolean indicating whether the write should be executed with | 877 as_root: A boolean indicating whether the write should be executed with |
| 880 root privileges (if available). | 878 root privileges. |
| 881 force_push: A boolean indicating whether to force the operation to be | |
| 882 performed by pushing a file to the device. The default is, when the | |
| 883 contents are short, to pass the contents using a shell script instead. | |
| 884 timeout: timeout in seconds | 879 timeout: timeout in seconds |
| 885 retries: number of retries | 880 retries: number of retries |
| 886 | 881 |
| 887 Raises: | 882 Raises: |
| 888 CommandFailedError if the file could not be written on the device. | 883 CommandFailedError if the file could not be written on the device. |
| 889 CommandTimeoutError on timeout. | 884 CommandTimeoutError on timeout. |
| 890 DeviceUnreachableError on missing device. | 885 DeviceUnreachableError on missing device. |
| 891 """ | 886 """ |
| 892 if len(contents) < 512 and not force_push: | 887 if as_root: |
| 893 cmd = 'echo -n %s > %s' % (cmd_helper.SingleQuote(contents), | 888 if not self.old_interface.CanAccessProtectedFileContents(): |
| 894 cmd_helper.SingleQuote(device_path)) | 889 raise device_errors.CommandFailedError( |
| 895 self.RunShellCommand(cmd, as_root=as_root, check_return=True) | 890 'Cannot write to %s with root privileges.' % device_path) |
| 891 self.old_interface.SetProtectedFileContents(device_path, contents) |
| 896 else: | 892 else: |
| 897 with tempfile.NamedTemporaryFile() as host_temp: | 893 self.old_interface.SetFileContents(device_path, contents) |
| 898 host_temp.write(contents) | 894 |
| 899 host_temp.flush() | 895 @decorators.WithTimeoutAndRetriesFromInstance() |
| 900 if as_root and self.NeedsSU(): | 896 def WriteTextFile(self, device_path, text, as_root=False, timeout=None, |
| 901 with device_temp_file.DeviceTempFile(self) as device_temp: | 897 retries=None): |
| 902 self.adb.Push(host_temp.name, device_temp.name) | 898 """Writes |text| to a file on the device. |
| 903 # Here we need 'cp' rather than 'mv' because the temp and | 899 |
| 904 # destination files might be on different file systems (e.g. | 900 Assuming that |text| is a small string, this is typically more efficient |
| 905 # on internal storage and an external sd card) | 901 than |WriteFile|, as no files are pushed into the device. |
| 906 self.RunShellCommand(['cp', device_temp.name, device_path], | 902 |
| 907 as_root=True, check_return=True) | 903 Args: |
| 908 else: | 904 device_path: A string containing the absolute path to the file to write |
| 909 self.adb.Push(host_temp.name, device_path) | 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) |
| 910 | 920 |
| 911 @decorators.WithTimeoutAndRetriesFromInstance() | 921 @decorators.WithTimeoutAndRetriesFromInstance() |
| 912 def Ls(self, device_path, timeout=None, retries=None): | 922 def Ls(self, device_path, timeout=None, retries=None): |
| 913 """Lists the contents of a directory on the device. | 923 """Lists the contents of a directory on the device. |
| 914 | 924 |
| 915 Args: | 925 Args: |
| 916 device_path: A string containing the path of the directory on the device | 926 device_path: A string containing the path of the directory on the device |
| 917 to list. | 927 to list. |
| 918 timeout: timeout in seconds | 928 timeout: timeout in seconds |
| 919 retries: number of retries | 929 retries: number of retries |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1140 Returns: | 1150 Returns: |
| 1141 A Parallelizer operating over |devices|. | 1151 A Parallelizer operating over |devices|. |
| 1142 """ | 1152 """ |
| 1143 if not devices or len(devices) == 0: | 1153 if not devices or len(devices) == 0: |
| 1144 devices = pylib.android_commands.GetAttachedDevices() | 1154 devices = pylib.android_commands.GetAttachedDevices() |
| 1145 parallelizer_type = (parallelizer.Parallelizer if async | 1155 parallelizer_type = (parallelizer.Parallelizer if async |
| 1146 else parallelizer.SyncParallelizer) | 1156 else parallelizer.SyncParallelizer) |
| 1147 return parallelizer_type([ | 1157 return parallelizer_type([ |
| 1148 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 1158 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
| 1149 for d in devices]) | 1159 for d in devices]) |
| OLD | NEW |