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 919 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 _TEMP_FILE_BASE_FMT = 'temp_file_%d' | 930 _TEMP_FILE_BASE_FMT = 'temp_file_%d' |
931 _TEMP_SCRIPT_FILE_BASE_FMT = 'temp_script_file_%d.sh' | 931 _TEMP_SCRIPT_FILE_BASE_FMT = 'temp_script_file_%d.sh' |
932 | 932 |
933 def _GetDeviceTempFileName(self, base_name): | 933 def _GetDeviceTempFileName(self, base_name): |
934 i = 0 | 934 i = 0 |
935 while self.FileExistsOnDevice( | 935 while self.FileExistsOnDevice( |
936 self.GetExternalStorage() + '/' + base_name % i): | 936 self.GetExternalStorage() + '/' + base_name % i): |
937 i += 1 | 937 i += 1 |
938 return self.GetExternalStorage() + '/' + base_name % i | 938 return self.GetExternalStorage() + '/' + base_name % i |
939 | 939 |
| 940 def RunShellCommandWithSU(self, command, timeout_time=20, log_result=False): |
| 941 return self.RunShellCommand('su -c %s' % command, |
| 942 timeout_time=timeout_time, |
| 943 log_result=log_result) |
| 944 |
940 def CanAccessProtectedFileContents(self): | 945 def CanAccessProtectedFileContents(self): |
941 """Returns True if Get/SetProtectedFileContents would work via "su". | 946 """Returns True if Get/SetProtectedFileContents would work via "su". |
942 | 947 |
943 Devices running user builds don't have adb root, but may provide "su" which | 948 Devices running user builds don't have adb root, but may provide "su" which |
944 can be used for accessing protected files. | 949 can be used for accessing protected files. |
945 """ | 950 """ |
946 r = self.RunShellCommand('su -c cat /dev/null') | 951 r = self.RunShellCommandWithSU('cat /dev/null') |
947 return r == [] or r[0].strip() == '' | 952 return r == [] or r[0].strip() == '' |
948 | 953 |
949 def GetProtectedFileContents(self, filename, log_result=False): | 954 def GetProtectedFileContents(self, filename, log_result=False): |
950 """Gets contents from the protected file specified by |filename|. | 955 """Gets contents from the protected file specified by |filename|. |
951 | 956 |
952 This is less efficient than GetFileContents, but will work for protected | 957 This is less efficient than GetFileContents, but will work for protected |
953 files and device files. | 958 files and device files. |
954 """ | 959 """ |
955 # Run the script as root | 960 # Run the script as root |
956 return self.RunShellCommand('su -c cat "%s" 2> /dev/null' % filename) | 961 return self.RunShellCommandWithSU('cat "%s" 2> /dev/null' % filename) |
957 | 962 |
958 def SetProtectedFileContents(self, filename, contents): | 963 def SetProtectedFileContents(self, filename, contents): |
959 """Writes |contents| to the protected file specified by |filename|. | 964 """Writes |contents| to the protected file specified by |filename|. |
960 | 965 |
961 This is less efficient than SetFileContents, but will work for protected | 966 This is less efficient than SetFileContents, but will work for protected |
962 files and device files. | 967 files and device files. |
963 """ | 968 """ |
964 temp_file = self._GetDeviceTempFileName(AndroidCommands._TEMP_FILE_BASE_FMT) | 969 temp_file = self._GetDeviceTempFileName(AndroidCommands._TEMP_FILE_BASE_FMT) |
965 temp_script = self._GetDeviceTempFileName( | 970 temp_script = self._GetDeviceTempFileName( |
966 AndroidCommands._TEMP_SCRIPT_FILE_BASE_FMT) | 971 AndroidCommands._TEMP_SCRIPT_FILE_BASE_FMT) |
967 | 972 |
968 # Put the contents in a temporary file | 973 # Put the contents in a temporary file |
969 self.SetFileContents(temp_file, contents) | 974 self.SetFileContents(temp_file, contents) |
970 # Create a script to copy the file contents to its final destination | 975 # Create a script to copy the file contents to its final destination |
971 self.SetFileContents(temp_script, 'cat %s > %s' % (temp_file, filename)) | 976 self.SetFileContents(temp_script, 'cat %s > %s' % (temp_file, filename)) |
972 # Run the script as root | 977 # Run the script as root |
973 self.RunShellCommand('su -c sh %s' % temp_script) | 978 self.RunShellCommandWithSU('sh %s' % temp_script) |
974 # And remove the temporary files | 979 # And remove the temporary files |
975 self.RunShellCommand('rm ' + temp_file) | 980 self.RunShellCommand('rm ' + temp_file) |
976 self.RunShellCommand('rm ' + temp_script) | 981 self.RunShellCommand('rm ' + temp_script) |
977 | 982 |
978 def RemovePushedFiles(self): | 983 def RemovePushedFiles(self): |
979 """Removes all files pushed with PushIfNeeded() from the device.""" | 984 """Removes all files pushed with PushIfNeeded() from the device.""" |
980 for p in self._pushed_files: | 985 for p in self._pushed_files: |
981 self.RunShellCommand('rm -r %s' % p, timeout_time=2 * 60) | 986 self.RunShellCommand('rm -r %s' % p, timeout_time=2 * 60) |
982 | 987 |
983 def ListPathContents(self, path): | 988 def ListPathContents(self, path): |
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1570 """ | 1575 """ |
1571 def __init__(self, output): | 1576 def __init__(self, output): |
1572 self._output = output | 1577 self._output = output |
1573 | 1578 |
1574 def write(self, data): | 1579 def write(self, data): |
1575 data = data.replace('\r\r\n', '\n') | 1580 data = data.replace('\r\r\n', '\n') |
1576 self._output.write(data) | 1581 self._output.write(data) |
1577 | 1582 |
1578 def flush(self): | 1583 def flush(self): |
1579 self._output.flush() | 1584 self._output.flush() |
OLD | NEW |