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 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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| | |
Sami
2014/11/12 19:45:38
Random thought: subprocess lets you use shell=True
| |
440 cmd = 'su -c sh -c %s' % cmd_helper.SingleQuote(cmd) | |
perezju
2014/11/12 19:30:41
I also moved the root check to the end so that, e.
| |
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 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
859 # the implementation switch, and if file not found should raise exception. | 861 # the implementation switch, and if file not found should raise exception. |
860 if as_root: | 862 if as_root: |
861 if not self.old_interface.CanAccessProtectedFileContents(): | 863 if not self.old_interface.CanAccessProtectedFileContents(): |
862 raise device_errors.CommandFailedError( | 864 raise device_errors.CommandFailedError( |
863 'Cannot read from %s with root privileges.' % device_path) | 865 'Cannot read from %s with root privileges.' % device_path) |
864 return self.old_interface.GetProtectedFileContents(device_path) | 866 return self.old_interface.GetProtectedFileContents(device_path) |
865 else: | 867 else: |
866 return self.old_interface.GetFileContents(device_path) | 868 return self.old_interface.GetFileContents(device_path) |
867 | 869 |
868 @decorators.WithTimeoutAndRetriesFromInstance() | 870 @decorators.WithTimeoutAndRetriesFromInstance() |
869 def WriteFile(self, device_path, contents, as_root=False, timeout=None, | 871 def WriteFile(self, device_path, contents, as_root=False, force_push=False, |
870 retries=None): | 872 timeout=None, retries=None): |
871 """Writes |contents| to a file on the device. | 873 """Writes |contents| to a file on the device. |
872 | 874 |
873 Args: | 875 Args: |
874 device_path: A string containing the absolute path to the file to write | 876 device_path: A string containing the absolute path to the file to write |
875 on the device. | 877 on the device. |
876 contents: A string containing the data to write to the device. | 878 contents: A string containing the data to write to the device. |
877 as_root: A boolean indicating whether the write should be executed with | 879 as_root: A boolean indicating whether the write should be executed with |
878 root privileges. | 880 root privileges (if available). |
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. | |
879 timeout: timeout in seconds | 884 timeout: timeout in seconds |
880 retries: number of retries | 885 retries: number of retries |
881 | 886 |
882 Raises: | 887 Raises: |
883 CommandFailedError if the file could not be written on the device. | 888 CommandFailedError if the file could not be written on the device. |
884 CommandTimeoutError on timeout. | 889 CommandTimeoutError on timeout. |
885 DeviceUnreachableError on missing device. | 890 DeviceUnreachableError on missing device. |
886 """ | 891 """ |
887 if as_root: | 892 if len(contents) < 512 and not force_push: |
888 if not self.old_interface.CanAccessProtectedFileContents(): | 893 cmd = 'echo -n %s > %s' % (cmd_helper.SingleQuote(contents), |
889 raise device_errors.CommandFailedError( | 894 cmd_helper.SingleQuote(device_path)) |
890 'Cannot write to %s with root privileges.' % device_path) | 895 self.RunShellCommand(cmd, as_root=as_root, check_return=True) |
perezju
2014/11/12 19:30:41
Now RunShellCommand will "do the right thing", so
| |
891 self.old_interface.SetProtectedFileContents(device_path, contents) | |
892 else: | 896 else: |
893 self.old_interface.SetFileContents(device_path, contents) | 897 with tempfile.NamedTemporaryFile() as host_temp: |
894 | 898 host_temp.write(contents) |
895 @decorators.WithTimeoutAndRetriesFromInstance() | 899 host_temp.flush() |
896 def WriteTextFile(self, device_path, text, as_root=False, timeout=None, | 900 if as_root and self.NeedsSU(): |
897 retries=None): | 901 with device_temp_file.DeviceTempFile(self) as device_temp: |
898 """Writes |text| to a file on the device. | 902 self.adb.Push(host_temp.name, device_temp.name) |
899 | 903 # Here we need 'cp' rather than 'mv' because the temp and |
900 Assuming that |text| is a small string, this is typically more efficient | 904 # destination files might be on different file systems (e.g. |
901 than |WriteFile|, as no files are pushed into the device. | 905 # on internal storage and an external sd card) |
902 | 906 self.RunShellCommand(['cp', device_temp.name, device_path], |
903 Args: | 907 as_root=True, check_return=True) |
904 device_path: A string containing the absolute path to the file to write | 908 else: |
905 on the device. | 909 self.adb.Push(host_temp.name, device_path) |
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 | 910 |
921 @decorators.WithTimeoutAndRetriesFromInstance() | 911 @decorators.WithTimeoutAndRetriesFromInstance() |
922 def Ls(self, device_path, timeout=None, retries=None): | 912 def Ls(self, device_path, timeout=None, retries=None): |
923 """Lists the contents of a directory on the device. | 913 """Lists the contents of a directory on the device. |
924 | 914 |
925 Args: | 915 Args: |
926 device_path: A string containing the path of the directory on the device | 916 device_path: A string containing the path of the directory on the device |
927 to list. | 917 to list. |
928 timeout: timeout in seconds | 918 timeout: timeout in seconds |
929 retries: number of retries | 919 retries: number of retries |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1150 Returns: | 1140 Returns: |
1151 A Parallelizer operating over |devices|. | 1141 A Parallelizer operating over |devices|. |
1152 """ | 1142 """ |
1153 if not devices or len(devices) == 0: | 1143 if not devices or len(devices) == 0: |
1154 devices = pylib.android_commands.GetAttachedDevices() | 1144 devices = pylib.android_commands.GetAttachedDevices() |
1155 parallelizer_type = (parallelizer.Parallelizer if async | 1145 parallelizer_type = (parallelizer.Parallelizer if async |
1156 else parallelizer.SyncParallelizer) | 1146 else parallelizer.SyncParallelizer) |
1157 return parallelizer_type([ | 1147 return parallelizer_type([ |
1158 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 1148 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
1159 for d in devices]) | 1149 for d in devices]) |
OLD | NEW |