OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 an interface to communicate with the device via the adb command. | 5 """Provides an interface to communicate with the device via the adb command. |
6 | 6 |
7 Assumes adb binary is currently on system path. | 7 Assumes adb binary is currently on system path. |
8 """ | 8 """ |
9 # pylint: disable-all | 9 # pylint: disable-all |
10 | 10 |
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 # A dict of commands the user should not run directly and a mapping to the | 629 # A dict of commands the user should not run directly and a mapping to the |
630 # API they should use instead. | 630 # API they should use instead. |
631 preferred_apis = { | 631 preferred_apis = { |
632 'getprop': 'system_properties[<PROPERTY>]', | 632 'getprop': 'system_properties[<PROPERTY>]', |
633 'setprop': 'system_properties[<PROPERTY>]', | 633 'setprop': 'system_properties[<PROPERTY>]', |
634 'su': 'RunShellCommandWithSU()', | 634 'su': 'RunShellCommandWithSU()', |
635 } | 635 } |
636 | 636 |
637 # A dict of commands to methods that may call them. | 637 # A dict of commands to methods that may call them. |
638 whitelisted_callers = { | 638 whitelisted_callers = { |
639 'su': 'RunShellCommandWithSU', | 639 'su': 'RunShellCommandWithSU', |
tonyg
2014/04/26 00:41:31
We should be able to add your method name here ins
navabi
2014/04/26 22:52:06
Done.
| |
640 } | 640 } |
641 | 641 |
642 base_command = shlex.split(command)[0] | 642 base_command = shlex.split(command)[0] |
643 if (base_command in preferred_apis and | 643 if (base_command in preferred_apis and |
644 (base_command not in whitelisted_callers or | 644 (base_command not in whitelisted_callers or |
645 whitelisted_callers[base_command] not in [ | 645 whitelisted_callers[base_command] not in [ |
646 f[3] for f in inspect.stack()])): | 646 f[3] for f in inspect.stack()])): |
647 error_msg = ('%s should not be run directly. Instead use: %s' % | 647 error_msg = ('%s should not be run directly. Instead use: %s' % |
648 (base_command, preferred_apis[base_command])) | 648 (base_command, preferred_apis[base_command])) |
649 raise ValueError(error_msg) | 649 raise ValueError(error_msg) |
650 | 650 |
651 # It is tempting to turn this function into a generator, however this is not | 651 # It is tempting to turn this function into a generator, however this is not |
652 # possible without using a private (local) adb_shell instance (to ensure no | 652 # possible without using a private (local) adb_shell instance (to ensure no |
653 # other command interleaves usage of it), which would defeat the main aim of | 653 # other command interleaves usage of it), which would defeat the main aim of |
654 # being able to reuse the adb shell instance across commands. | 654 # being able to reuse the adb shell instance across commands. |
655 def RunShellCommand(self, command, timeout_time=20, log_result=False): | 655 def RunShellCommand(self, command, timeout_time=20, log_result=False, |
656 skip_check=False): | |
656 """Send a command to the adb shell and return the result. | 657 """Send a command to the adb shell and return the result. |
657 | 658 |
658 Args: | 659 Args: |
659 command: String containing the shell command to send. Must not include | 660 command: String containing the shell command to send. Must not include |
660 the single quotes as we use them to escape the whole command. | 661 the single quotes as we use them to escape the whole command. |
661 timeout_time: Number of seconds to wait for command to respond before | 662 timeout_time: Number of seconds to wait for command to respond before |
662 retrying, used by AdbInterface.SendShellCommand. | 663 retrying, used by AdbInterface.SendShellCommand. |
663 log_result: Boolean to indicate whether we should log the result of the | 664 log_result: Boolean to indicate whether we should log the result of the |
664 shell command. | 665 shell command. |
665 | 666 |
666 Returns: | 667 Returns: |
667 list containing the lines of output received from running the command | 668 list containing the lines of output received from running the command |
668 """ | 669 """ |
669 self._CheckCommandIsValid(command) | 670 if not skip_check: |
671 self._CheckCommandIsValid(command) | |
670 self._LogShell(command) | 672 self._LogShell(command) |
671 if "'" in command: | 673 if "'" in command: |
672 logging.warning(command + " contains ' quotes") | 674 logging.warning(command + " contains ' quotes") |
673 result = self._adb.SendShellCommand( | 675 result = self._adb.SendShellCommand( |
674 "'%s'" % command, timeout_time).splitlines() | 676 "'%s'" % command, timeout_time).splitlines() |
675 # TODO(b.kelemen): we should really be able to drop the stderr of the | 677 # TODO(b.kelemen): we should really be able to drop the stderr of the |
676 # command or raise an exception based on what the caller wants. | 678 # command or raise an exception based on what the caller wants. |
677 result = [ l for l in result if not l.startswith('WARNING') ] | 679 result = [ l for l in result if not l.startswith('WARNING') ] |
678 if ['error: device not found'] == result: | 680 if ['error: device not found'] == result: |
679 raise errors.DeviceUnresponsiveError('device not found') | 681 raise errors.DeviceUnresponsiveError('device not found') |
680 if log_result: | 682 if log_result: |
681 self._LogShell('\n'.join(result)) | 683 self._LogShell('\n'.join(result)) |
682 return result | 684 return result |
683 | 685 |
684 def GetShellCommandStatusAndOutput(self, command, timeout_time=20, | 686 def GetShellCommandStatusAndOutput(self, command, timeout_time=20, |
685 log_result=False): | 687 log_result=False, skip_check=False): |
686 """See RunShellCommand() above. | 688 """See RunShellCommand() above. |
687 | 689 |
688 Returns: | 690 Returns: |
689 The tuple (exit code, list of output lines). | 691 The tuple (exit code, list of output lines). |
690 """ | 692 """ |
691 lines = self.RunShellCommand( | 693 lines = self.RunShellCommand( |
692 command + '; echo %$?', timeout_time, log_result) | 694 command + '; echo %$?', timeout_time, log_result) |
693 last_line = lines[-1] | 695 last_line = lines[-1] |
694 status_pos = last_line.rfind('%') | 696 status_pos = last_line.rfind('%') |
695 assert status_pos >= 0 | 697 assert status_pos >= 0 |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1093 _TEMP_FILE_BASE_FMT = 'temp_file_%d' | 1095 _TEMP_FILE_BASE_FMT = 'temp_file_%d' |
1094 _TEMP_SCRIPT_FILE_BASE_FMT = 'temp_script_file_%d.sh' | 1096 _TEMP_SCRIPT_FILE_BASE_FMT = 'temp_script_file_%d.sh' |
1095 | 1097 |
1096 def _GetDeviceTempFileName(self, base_name): | 1098 def _GetDeviceTempFileName(self, base_name): |
1097 i = 0 | 1099 i = 0 |
1098 while self.FileExistsOnDevice( | 1100 while self.FileExistsOnDevice( |
1099 self.GetExternalStorage() + '/' + base_name % i): | 1101 self.GetExternalStorage() + '/' + base_name % i): |
1100 i += 1 | 1102 i += 1 |
1101 return self.GetExternalStorage() + '/' + base_name % i | 1103 return self.GetExternalStorage() + '/' + base_name % i |
1102 | 1104 |
1103 def RunShellCommandWithSU(self, command, timeout_time=20, log_result=False): | 1105 def RunShellCommandWithSU(self, command, timeout_time=20, log_result=False, |
1104 return self.RunShellCommand('su -c %s' % command, timeout_time, log_result) | 1106 skip_check='False'): |
1107 return self.RunShellCommand('su -c %s' % command, timeout_time, log_result, | |
1108 skip_check) | |
1105 | 1109 |
1106 def CanAccessProtectedFileContents(self): | 1110 def CanAccessProtectedFileContents(self): |
1107 """Returns True if Get/SetProtectedFileContents would work via "su". | 1111 """Returns True if Get/SetProtectedFileContents would work via "su". |
1108 | 1112 |
1109 Devices running user builds don't have adb root, but may provide "su" which | 1113 Devices running user builds don't have adb root, but may provide "su" which |
1110 can be used for accessing protected files. | 1114 can be used for accessing protected files. |
1111 """ | 1115 """ |
1112 r = self.RunShellCommandWithSU('cat /dev/null') | 1116 r = self.RunShellCommandWithSU('cat /dev/null') |
1113 return r == [] or r[0].strip() == '' | 1117 return r == [] or r[0].strip() == '' |
1114 | 1118 |
(...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1924 """ | 1928 """ |
1925 def __init__(self, output): | 1929 def __init__(self, output): |
1926 self._output = output | 1930 self._output = output |
1927 | 1931 |
1928 def write(self, data): | 1932 def write(self, data): |
1929 data = data.replace('\r\r\n', '\n') | 1933 data = data.replace('\r\r\n', '\n') |
1930 self._output.write(data) | 1934 self._output.write(data) |
1931 | 1935 |
1932 def flush(self): | 1936 def flush(self): |
1933 self._output.flush() | 1937 self._output.flush() |
OLD | NEW |