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 | 9 |
10 import collections | 10 import collections |
(...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 # NOTE: We can't use adb_interface.Push() because it hardcodes a timeout of | 687 # NOTE: We can't use adb_interface.Push() because it hardcodes a timeout of |
688 # 60 seconds which isn't sufficient for a lot of users of this method. | 688 # 60 seconds which isn't sufficient for a lot of users of this method. |
689 push_command = 'push %s %s' % (local_path, device_path) | 689 push_command = 'push %s %s' % (local_path, device_path) |
690 logging.info('>>> $' + push_command) | 690 logging.info('>>> $' + push_command) |
691 output = self._adb.SendCommand(push_command, timeout_time=30 * 60) | 691 output = self._adb.SendCommand(push_command, timeout_time=30 * 60) |
692 assert _HasAdbPushSucceeded(output) | 692 assert _HasAdbPushSucceeded(output) |
693 | 693 |
694 | 694 |
695 def GetFileContents(self, filename, log_result=False): | 695 def GetFileContents(self, filename, log_result=False): |
696 """Gets contents from the file specified by |filename|.""" | 696 """Gets contents from the file specified by |filename|.""" |
697 return self.RunShellCommand('if [ -f "' + filename + '" ]; then cat "' + | 697 return self.RunShellCommand('cat "%s" 2>/dev/null' % filename, |
698 filename + '"; fi', log_result=log_result) | 698 log_result=log_result) |
699 | 699 |
700 def SetFileContents(self, filename, contents): | 700 def SetFileContents(self, filename, contents): |
701 """Writes |contents| to the file specified by |filename|.""" | 701 """Writes |contents| to the file specified by |filename|.""" |
702 with tempfile.NamedTemporaryFile() as f: | 702 with tempfile.NamedTemporaryFile() as f: |
703 f.write(contents) | 703 f.write(contents) |
704 f.flush() | 704 f.flush() |
705 self._adb.Push(f.name, filename) | 705 self._adb.Push(f.name, filename) |
706 | 706 |
707 _TEMP_FILE_BASE_FMT = 'temp_file_%d' | 707 _TEMP_FILE_BASE_FMT = 'temp_file_%d' |
708 _TEMP_SCRIPT_FILE_BASE_FMT = 'temp_script_file_%d.sh' | 708 _TEMP_SCRIPT_FILE_BASE_FMT = 'temp_script_file_%d.sh' |
709 | 709 |
710 def _GetDeviceTempFileName(self, base_name): | 710 def _GetDeviceTempFileName(self, base_name): |
711 i = 0 | 711 i = 0 |
712 while self.FileExistsOnDevice( | 712 while self.FileExistsOnDevice( |
713 self.GetExternalStorage() + '/' + base_name % i): | 713 self.GetExternalStorage() + '/' + base_name % i): |
714 i += 1 | 714 i += 1 |
715 return self.GetExternalStorage() + '/' + base_name % i | 715 return self.GetExternalStorage() + '/' + base_name % i |
716 | 716 |
| 717 def CanAccessProtectedFileContents(self): |
| 718 """Returns True if Get/SetProtectedFileContents would work via "su". |
| 719 |
| 720 Devices running user builds don't have adb root, but may provide "su" which |
| 721 can be used for accessing protected files. |
| 722 """ |
| 723 return self.RunShellCommand('su -c echo') == [''] |
| 724 |
| 725 def GetProtectedFileContents(self, filename, log_result=False): |
| 726 """Gets contents from the protected file specified by |filename|. |
| 727 |
| 728 This is less efficient than GetFileContents, but will work for protected |
| 729 files and device files. |
| 730 """ |
| 731 # Run the script as root |
| 732 return self.RunShellCommand('su -c cat "%s" 2> /dev/null' % filename) |
| 733 |
717 def SetProtectedFileContents(self, filename, contents): | 734 def SetProtectedFileContents(self, filename, contents): |
718 """Writes |contents| to the protected file specified by |filename|. | 735 """Writes |contents| to the protected file specified by |filename|. |
719 | 736 |
720 This is less efficient than SetFileContents, but will work for protected | 737 This is less efficient than SetFileContents, but will work for protected |
721 files and device files. | 738 files and device files. |
722 """ | 739 """ |
723 temp_file = self._GetDeviceTempFileName(AndroidCommands._TEMP_FILE_BASE_FMT) | 740 temp_file = self._GetDeviceTempFileName(AndroidCommands._TEMP_FILE_BASE_FMT) |
724 temp_script = self._GetDeviceTempFileName( | 741 temp_script = self._GetDeviceTempFileName( |
725 AndroidCommands._TEMP_SCRIPT_FILE_BASE_FMT) | 742 AndroidCommands._TEMP_SCRIPT_FILE_BASE_FMT) |
726 | 743 |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1190 """ | 1207 """ |
1191 def __init__(self, output): | 1208 def __init__(self, output): |
1192 self._output = output | 1209 self._output = output |
1193 | 1210 |
1194 def write(self, data): | 1211 def write(self, data): |
1195 data = data.replace('\r\r\n', '\n') | 1212 data = data.replace('\r\r\n', '\n') |
1196 self._output.write(data) | 1213 self._output.write(data) |
1197 | 1214 |
1198 def flush(self): | 1215 def flush(self): |
1199 self._output.flush() | 1216 self._output.flush() |
OLD | NEW |