| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 027c10494100b4d7 device | 93 027c10494100b4d7 device |
| 94 emulator-5554 offline | 94 emulator-5554 offline |
| 95 | 95 |
| 96 Args: | 96 Args: |
| 97 hardware: Include attached actual devices that are online. | 97 hardware: Include attached actual devices that are online. |
| 98 emulator: Include emulators (i.e. AVD's) currently on host. | 98 emulator: Include emulators (i.e. AVD's) currently on host. |
| 99 offline: Include devices and emulators that are offline. | 99 offline: Include devices and emulators that are offline. |
| 100 | 100 |
| 101 Returns: List of devices. | 101 Returns: List of devices. |
| 102 """ | 102 """ |
| 103 adb_devices_output = cmd_helper.GetCmdOutput([constants.ADB_PATH, 'devices']) | 103 adb_devices_output = cmd_helper.GetCmdOutput([constants.GetAdbPath(), |
| 104 'devices']) |
| 104 | 105 |
| 105 re_device = re.compile('^([a-zA-Z0-9_:.-]+)\tdevice$', re.MULTILINE) | 106 re_device = re.compile('^([a-zA-Z0-9_:.-]+)\tdevice$', re.MULTILINE) |
| 106 online_devices = re_device.findall(adb_devices_output) | 107 online_devices = re_device.findall(adb_devices_output) |
| 107 | 108 |
| 108 re_device = re.compile('^(emulator-[0-9]+)\tdevice', re.MULTILINE) | 109 re_device = re.compile('^(emulator-[0-9]+)\tdevice', re.MULTILINE) |
| 109 emulator_devices = re_device.findall(adb_devices_output) | 110 emulator_devices = re_device.findall(adb_devices_output) |
| 110 | 111 |
| 111 re_device = re.compile('^([a-zA-Z0-9_:.-]+)\toffline$', re.MULTILINE) | 112 re_device = re.compile('^([a-zA-Z0-9_:.-]+)\toffline$', re.MULTILINE) |
| 112 offline_devices = re_device.findall(adb_devices_output) | 113 offline_devices = re_device.findall(adb_devices_output) |
| 113 | 114 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 235 |
| 235 def __init__(self, device=None, api_strict_mode=False): | 236 def __init__(self, device=None, api_strict_mode=False): |
| 236 """Constructor. | 237 """Constructor. |
| 237 | 238 |
| 238 Args: | 239 Args: |
| 239 device: If given, adb commands are only send to the device of this ID. | 240 device: If given, adb commands are only send to the device of this ID. |
| 240 Otherwise commands are sent to all attached devices. | 241 Otherwise commands are sent to all attached devices. |
| 241 api_strict_mode: A boolean indicating whether fatal errors should be | 242 api_strict_mode: A boolean indicating whether fatal errors should be |
| 242 raised if this API is used improperly. | 243 raised if this API is used improperly. |
| 243 """ | 244 """ |
| 244 adb_dir = os.path.dirname(constants.ADB_PATH) | 245 adb_dir = os.path.dirname(constants.GetAdbPath()) |
| 245 if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep): | 246 if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep): |
| 246 # Required by third_party/android_testrunner to call directly 'adb'. | 247 # Required by third_party/android_testrunner to call directly 'adb'. |
| 247 os.environ['PATH'] += os.pathsep + adb_dir | 248 os.environ['PATH'] += os.pathsep + adb_dir |
| 248 self._adb = adb_interface.AdbInterface() | 249 self._adb = adb_interface.AdbInterface() |
| 249 if device: | 250 if device: |
| 250 self._adb.SetTargetSerial(device) | 251 self._adb.SetTargetSerial(device) |
| 251 self._device = device | 252 self._device = device |
| 252 self._logcat = None | 253 self._logcat = None |
| 253 self.logcat_process = None | 254 self.logcat_process = None |
| 254 self._logcat_tmpoutfile = None | 255 self._logcat_tmpoutfile = None |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 ret = self.KillAdbServer() | 505 ret = self.KillAdbServer() |
| 505 if ret != 0: | 506 if ret != 0: |
| 506 raise errors.MsgException('KillAdbServer: %d' % ret) | 507 raise errors.MsgException('KillAdbServer: %d' % ret) |
| 507 | 508 |
| 508 ret = self.StartAdbServer() | 509 ret = self.StartAdbServer() |
| 509 if ret != 0: | 510 if ret != 0: |
| 510 raise errors.MsgException('StartAdbServer: %d' % ret) | 511 raise errors.MsgException('StartAdbServer: %d' % ret) |
| 511 | 512 |
| 512 def KillAdbServer(self): | 513 def KillAdbServer(self): |
| 513 """Kill adb server.""" | 514 """Kill adb server.""" |
| 514 adb_cmd = [constants.ADB_PATH, 'kill-server'] | 515 adb_cmd = [constants.GetAdbPath(), 'kill-server'] |
| 515 ret = cmd_helper.RunCmd(adb_cmd) | 516 ret = cmd_helper.RunCmd(adb_cmd) |
| 516 retry = 0 | 517 retry = 0 |
| 517 while retry < 3: | 518 while retry < 3: |
| 518 ret = cmd_helper.RunCmd(['pgrep', 'adb']) | 519 ret = cmd_helper.RunCmd(['pgrep', 'adb']) |
| 519 if ret != 0: | 520 if ret != 0: |
| 520 # pgrep didn't find adb, kill-server succeeded. | 521 # pgrep didn't find adb, kill-server succeeded. |
| 521 return 0 | 522 return 0 |
| 522 retry += 1 | 523 retry += 1 |
| 523 time.sleep(retry) | 524 time.sleep(retry) |
| 524 return ret | 525 return ret |
| 525 | 526 |
| 526 def StartAdbServer(self): | 527 def StartAdbServer(self): |
| 527 """Start adb server.""" | 528 """Start adb server.""" |
| 528 adb_cmd = ['taskset', '-c', '0', constants.ADB_PATH, 'start-server'] | 529 adb_cmd = ['taskset', '-c', '0', constants.GetAdbPath(), 'start-server'] |
| 529 ret = cmd_helper.RunCmd(adb_cmd) | 530 ret = cmd_helper.RunCmd(adb_cmd) |
| 530 retry = 0 | 531 retry = 0 |
| 531 while retry < 3: | 532 while retry < 3: |
| 532 ret = cmd_helper.RunCmd(['pgrep', 'adb']) | 533 ret = cmd_helper.RunCmd(['pgrep', 'adb']) |
| 533 if ret == 0: | 534 if ret == 0: |
| 534 # pgrep found adb, start-server succeeded. | 535 # pgrep found adb, start-server succeeded. |
| 535 # Waiting for device to reconnect before returning success. | 536 # Waiting for device to reconnect before returning success. |
| 536 self._adb.SendCommand('wait-for-device') | 537 self._adb.SendCommand('wait-for-device') |
| 537 return 0 | 538 return 0 |
| 538 retry += 1 | 539 retry += 1 |
| (...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1254 if filters: | 1255 if filters: |
| 1255 args.extend(filters) | 1256 args.extend(filters) |
| 1256 else: | 1257 else: |
| 1257 args.append('*:v') | 1258 args.append('*:v') |
| 1258 | 1259 |
| 1259 if logfile: | 1260 if logfile: |
| 1260 logfile = NewLineNormalizer(logfile) | 1261 logfile = NewLineNormalizer(logfile) |
| 1261 | 1262 |
| 1262 # Spawn logcat and synchronize with it. | 1263 # Spawn logcat and synchronize with it. |
| 1263 for _ in range(4): | 1264 for _ in range(4): |
| 1264 self._logcat = pexpect.spawn(constants.ADB_PATH, args, timeout=10, | 1265 self._logcat = pexpect.spawn(constants.GetAdbPath(), args, timeout=10, |
| 1265 logfile=logfile) | 1266 logfile=logfile) |
| 1266 if not clear or self.SyncLogCat(): | 1267 if not clear or self.SyncLogCat(): |
| 1267 break | 1268 break |
| 1268 self._logcat.close(force=True) | 1269 self._logcat.close(force=True) |
| 1269 else: | 1270 else: |
| 1270 logging.critical('Error reading from logcat: ' + str(self._logcat.match)) | 1271 logging.critical('Error reading from logcat: ' + str(self._logcat.match)) |
| 1271 sys.exit(1) | 1272 sys.exit(1) |
| 1272 | 1273 |
| 1273 def SyncLogCat(self): | 1274 def SyncLogCat(self): |
| 1274 """Synchronize with logcat. | 1275 """Synchronize with logcat. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1336 'to debug)' % | 1337 'to debug)' % |
| 1337 (timeout, success_re.pattern)) | 1338 (timeout, success_re.pattern)) |
| 1338 except pexpect.EOF: | 1339 except pexpect.EOF: |
| 1339 # It seems that sometimes logcat can end unexpectedly. This seems | 1340 # It seems that sometimes logcat can end unexpectedly. This seems |
| 1340 # to happen during Chrome startup after a reboot followed by a cache | 1341 # to happen during Chrome startup after a reboot followed by a cache |
| 1341 # clean. I don't understand why this happens, but this code deals with | 1342 # clean. I don't understand why this happens, but this code deals with |
| 1342 # getting EOF in logcat. | 1343 # getting EOF in logcat. |
| 1343 logging.critical('Found EOF in adb logcat. Restarting...') | 1344 logging.critical('Found EOF in adb logcat. Restarting...') |
| 1344 # Rerun spawn with original arguments. Note that self._logcat.args[0] is | 1345 # Rerun spawn with original arguments. Note that self._logcat.args[0] is |
| 1345 # the path of adb, so we don't want it in the arguments. | 1346 # the path of adb, so we don't want it in the arguments. |
| 1346 self._logcat = pexpect.spawn(constants.ADB_PATH, | 1347 self._logcat = pexpect.spawn(constants.GetAdbPath(), |
| 1347 self._logcat.args[1:], | 1348 self._logcat.args[1:], |
| 1348 timeout=self._logcat.timeout, | 1349 timeout=self._logcat.timeout, |
| 1349 logfile=self._logcat.logfile) | 1350 logfile=self._logcat.logfile) |
| 1350 | 1351 |
| 1351 def StartRecordingLogcat(self, clear=True, filters=['*:v']): | 1352 def StartRecordingLogcat(self, clear=True, filters=['*:v']): |
| 1352 """Starts recording logcat output to eventually be saved as a string. | 1353 """Starts recording logcat output to eventually be saved as a string. |
| 1353 | 1354 |
| 1354 This call should come before some series of tests are run, with either | 1355 This call should come before some series of tests are run, with either |
| 1355 StopRecordingLogcat or SearchLogcatRecord following the tests. | 1356 StopRecordingLogcat or SearchLogcatRecord following the tests. |
| 1356 | 1357 |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1799 """ | 1800 """ |
| 1800 def __init__(self, output): | 1801 def __init__(self, output): |
| 1801 self._output = output | 1802 self._output = output |
| 1802 | 1803 |
| 1803 def write(self, data): | 1804 def write(self, data): |
| 1804 data = data.replace('\r\r\n', '\n') | 1805 data = data.replace('\r\r\n', '\n') |
| 1805 self._output.write(data) | 1806 self._output.write(data) |
| 1806 | 1807 |
| 1807 def flush(self): | 1808 def flush(self): |
| 1808 self._output.flush() | 1809 self._output.flush() |
| OLD | NEW |