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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 Otherwise commands are sent to all attached devices. | 197 Otherwise commands are sent to all attached devices. |
198 """ | 198 """ |
199 | 199 |
200 def __init__(self, device=None): | 200 def __init__(self, device=None): |
201 self._adb = adb_interface.AdbInterface() | 201 self._adb = adb_interface.AdbInterface() |
202 if device: | 202 if device: |
203 self._adb.SetTargetSerial(device) | 203 self._adb.SetTargetSerial(device) |
204 self._device = device | 204 self._device = device |
205 self._logcat = None | 205 self._logcat = None |
206 self.logcat_process = None | 206 self.logcat_process = None |
| 207 self._logcat_tmpoutfile = None |
207 self._pushed_files = [] | 208 self._pushed_files = [] |
208 self._device_utc_offset = self.RunShellCommand('date +%z')[0] | 209 self._device_utc_offset = self.RunShellCommand('date +%z')[0] |
209 self._md5sum_path = '' | 210 self._md5sum_path = '' |
210 self._external_storage = '' | 211 self._external_storage = '' |
211 self._util_wrapper = '' | 212 self._util_wrapper = '' |
212 | 213 |
213 def Adb(self): | 214 def Adb(self): |
214 """Returns our AdbInterface to avoid us wrapping all its methods.""" | 215 """Returns our AdbInterface to avoid us wrapping all its methods.""" |
215 return self._adb | 216 return self._adb |
216 | 217 |
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
948 StopRecordingLogcat or SearchLogcatRecord following the tests. | 949 StopRecordingLogcat or SearchLogcatRecord following the tests. |
949 | 950 |
950 Args: | 951 Args: |
951 clear: True if existing log output should be cleared. | 952 clear: True if existing log output should be cleared. |
952 filters: A list of logcat filters to be used. | 953 filters: A list of logcat filters to be used. |
953 """ | 954 """ |
954 if clear: | 955 if clear: |
955 self._adb.SendCommand('logcat -c') | 956 self._adb.SendCommand('logcat -c') |
956 logcat_command = 'adb %s logcat -v threadtime %s' % (self._adb._target_arg, | 957 logcat_command = 'adb %s logcat -v threadtime %s' % (self._adb._target_arg, |
957 ' '.join(filters)) | 958 ' '.join(filters)) |
| 959 self._logcat_tmpoutfile = tempfile.TemporaryFile(bufsize=0) |
958 self.logcat_process = subprocess.Popen(logcat_command, shell=True, | 960 self.logcat_process = subprocess.Popen(logcat_command, shell=True, |
959 stdout=subprocess.PIPE) | 961 stdout=self._logcat_tmpoutfile) |
960 | 962 |
961 def StopRecordingLogcat(self): | 963 def StopRecordingLogcat(self): |
962 """Stops an existing logcat recording subprocess and returns output. | 964 """Stops an existing logcat recording subprocess and returns output. |
963 | 965 |
964 Returns: | 966 Returns: |
965 The logcat output as a string or an empty string if logcat was not | 967 The logcat output as a string or an empty string if logcat was not |
966 being recorded at the time. | 968 being recorded at the time. |
967 """ | 969 """ |
968 if not self.logcat_process: | 970 if not self.logcat_process: |
969 return '' | 971 return '' |
970 # Cannot evaluate directly as 0 is a possible value. | 972 # Cannot evaluate directly as 0 is a possible value. |
971 # Better to read the self.logcat_process.stdout before killing it, | 973 # Better to read the self.logcat_process.stdout before killing it, |
972 # Otherwise the communicate may return incomplete output due to pipe break. | 974 # Otherwise the communicate may return incomplete output due to pipe break. |
973 if self.logcat_process.poll() is None: | 975 if self.logcat_process.poll() is None: |
974 self.logcat_process.kill() | 976 self.logcat_process.kill() |
975 (output, _) = self.logcat_process.communicate() | 977 self.logcat_process.wait() |
976 self.logcat_process = None | 978 self.logcat_process = None |
| 979 self._logcat_tmpoutfile.seek(0) |
| 980 output = self._logcat_tmpoutfile.read() |
| 981 self._logcat_tmpoutfile.close() |
977 return output | 982 return output |
978 | 983 |
979 def SearchLogcatRecord(self, record, message, thread_id=None, proc_id=None, | 984 def SearchLogcatRecord(self, record, message, thread_id=None, proc_id=None, |
980 log_level=None, component=None): | 985 log_level=None, component=None): |
981 """Searches the specified logcat output and returns results. | 986 """Searches the specified logcat output and returns results. |
982 | 987 |
983 This method searches through the logcat output specified by record for a | 988 This method searches through the logcat output specified by record for a |
984 certain message, narrowing results by matching them against any other | 989 certain message, narrowing results by matching them against any other |
985 specified criteria. It returns all matching lines as described below. | 990 specified criteria. It returns all matching lines as described below. |
986 | 991 |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1268 """ | 1273 """ |
1269 def __init__(self, output): | 1274 def __init__(self, output): |
1270 self._output = output | 1275 self._output = output |
1271 | 1276 |
1272 def write(self, data): | 1277 def write(self, data): |
1273 data = data.replace('\r\r\n', '\n') | 1278 data = data.replace('\r\r\n', '\n') |
1274 self._output.write(data) | 1279 self._output.write(data) |
1275 | 1280 |
1276 def flush(self): | 1281 def flush(self): |
1277 self._output.flush() | 1282 self._output.flush() |
OLD | NEW |