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