| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 emulator-5554 offline | 100 emulator-5554 offline |
| 101 """ | 101 """ |
| 102 re_device = re.compile('^([a-zA-Z0-9_:.-]+)\tdevice$', re.MULTILINE) | 102 re_device = re.compile('^([a-zA-Z0-9_:.-]+)\tdevice$', re.MULTILINE) |
| 103 devices = re_device.findall(cmd_helper.GetCmdOutput(['adb', 'devices'])) | 103 devices = re_device.findall(cmd_helper.GetCmdOutput(['adb', 'devices'])) |
| 104 preferred_device = os.environ.get('ANDROID_SERIAL') | 104 preferred_device = os.environ.get('ANDROID_SERIAL') |
| 105 if preferred_device in devices: | 105 if preferred_device in devices: |
| 106 devices.remove(preferred_device) | 106 devices.remove(preferred_device) |
| 107 devices.insert(0, preferred_device) | 107 devices.insert(0, preferred_device) |
| 108 return devices | 108 return devices |
| 109 | 109 |
| 110 def IsDeviceAttached(device): |
| 111 return device in GetAttachedDevices() |
| 112 |
| 110 def _GetFilesFromRecursiveLsOutput(path, ls_output, re_file, utc_offset=None): | 113 def _GetFilesFromRecursiveLsOutput(path, ls_output, re_file, utc_offset=None): |
| 111 """Gets a list of files from `ls` command output. | 114 """Gets a list of files from `ls` command output. |
| 112 | 115 |
| 113 Python's os.walk isn't used because it doesn't work over adb shell. | 116 Python's os.walk isn't used because it doesn't work over adb shell. |
| 114 | 117 |
| 115 Args: | 118 Args: |
| 116 path: The path to list. | 119 path: The path to list. |
| 117 ls_output: A list of lines returned by an `ls -lR` command. | 120 ls_output: A list of lines returned by an `ls -lR` command. |
| 118 re_file: A compiled regular expression which parses a line into named groups | 121 re_file: A compiled regular expression which parses a line into named groups |
| 119 consisting of at minimum "filename", "date", "time", "size" and | 122 consisting of at minimum "filename", "date", "time", "size" and |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 192 |
| 190 Args: | 193 Args: |
| 191 device: If given, adb commands are only send to the device of this ID. | 194 device: If given, adb commands are only send to the device of this ID. |
| 192 Otherwise commands are sent to all attached devices. | 195 Otherwise commands are sent to all attached devices. |
| 193 """ | 196 """ |
| 194 | 197 |
| 195 def __init__(self, device=None): | 198 def __init__(self, device=None): |
| 196 self._adb = adb_interface.AdbInterface() | 199 self._adb = adb_interface.AdbInterface() |
| 197 if device: | 200 if device: |
| 198 self._adb.SetTargetSerial(device) | 201 self._adb.SetTargetSerial(device) |
| 202 self._device = device |
| 199 self._logcat = None | 203 self._logcat = None |
| 200 self.logcat_process = None | 204 self.logcat_process = None |
| 201 self._pushed_files = [] | 205 self._pushed_files = [] |
| 202 self._device_utc_offset = self.RunShellCommand('date +%z')[0] | 206 self._device_utc_offset = self.RunShellCommand('date +%z')[0] |
| 203 self._md5sum_path = '' | 207 self._md5sum_path = '' |
| 204 self._external_storage = '' | 208 self._external_storage = '' |
| 205 | 209 |
| 206 def Adb(self): | 210 def Adb(self): |
| 207 """Returns our AdbInterface to avoid us wrapping all its methods.""" | 211 """Returns our AdbInterface to avoid us wrapping all its methods.""" |
| 208 return self._adb | 212 return self._adb |
| (...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1033 def FileExistsOnDevice(self, file_name): | 1037 def FileExistsOnDevice(self, file_name): |
| 1034 """Checks whether the given file exists on the device. | 1038 """Checks whether the given file exists on the device. |
| 1035 | 1039 |
| 1036 Args: | 1040 Args: |
| 1037 file_name: Full path of file to check. | 1041 file_name: Full path of file to check. |
| 1038 | 1042 |
| 1039 Returns: | 1043 Returns: |
| 1040 True if the file exists, False otherwise. | 1044 True if the file exists, False otherwise. |
| 1041 """ | 1045 """ |
| 1042 assert '"' not in file_name, 'file_name cannot contain double quotes' | 1046 assert '"' not in file_name, 'file_name cannot contain double quotes' |
| 1043 status = self._adb.SendShellCommand( | 1047 try: |
| 1044 '\'test -e "%s"; echo $?\'' % (file_name)) | 1048 status = self._adb.SendShellCommand( |
| 1045 if 'test: not found' not in status: | 1049 '\'test -e "%s"; echo $?\'' % (file_name)) |
| 1050 if 'test: not found' not in status: |
| 1051 return int(status) == 0 |
| 1052 |
| 1053 status = self._adb.SendShellCommand( |
| 1054 '\'ls "%s" >/dev/null 2>&1; echo $?\'' % (file_name)) |
| 1046 return int(status) == 0 | 1055 return int(status) == 0 |
| 1056 except ValueError: |
| 1057 if IsDeviceAttached(self._device): |
| 1058 raise errors.DeviceUnresponsiveError('Device may be offline.') |
| 1047 | 1059 |
| 1048 status = self._adb.SendShellCommand( | 1060 return False |
| 1049 '\'ls "%s" >/dev/null 2>&1; echo $?\'' % (file_name)) | |
| 1050 return int(status) == 0 | |
| 1051 | 1061 |
| 1052 | 1062 |
| 1053 class NewLineNormalizer(object): | 1063 class NewLineNormalizer(object): |
| 1054 """A file-like object to normalize EOLs to '\n'. | 1064 """A file-like object to normalize EOLs to '\n'. |
| 1055 | 1065 |
| 1056 Pexpect runs adb within a pseudo-tty device (see | 1066 Pexpect runs adb within a pseudo-tty device (see |
| 1057 http://www.noah.org/wiki/pexpect), so any '\n' printed by adb is written | 1067 http://www.noah.org/wiki/pexpect), so any '\n' printed by adb is written |
| 1058 as '\r\n' to the logfile. Since adb already uses '\r\n' to terminate | 1068 as '\r\n' to the logfile. Since adb already uses '\r\n' to terminate |
| 1059 lines, the log ends up having '\r\r\n' at the end of each line. This | 1069 lines, the log ends up having '\r\r\n' at the end of each line. This |
| 1060 filter replaces the above with a single '\n' in the data stream. | 1070 filter replaces the above with a single '\n' in the data stream. |
| 1061 """ | 1071 """ |
| 1062 def __init__(self, output): | 1072 def __init__(self, output): |
| 1063 self._output = output | 1073 self._output = output |
| 1064 | 1074 |
| 1065 def write(self, data): | 1075 def write(self, data): |
| 1066 data = data.replace('\r\r\n', '\n') | 1076 data = data.replace('\r\r\n', '\n') |
| 1067 self._output.write(data) | 1077 self._output.write(data) |
| 1068 | 1078 |
| 1069 def flush(self): | 1079 def flush(self): |
| 1070 self._output.flush() | 1080 self._output.flush() |
| 1071 | 1081 |
| OLD | NEW |