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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 Args: | 194 Args: |
195 device: If given, adb commands are only send to the device of this ID. | 195 device: If given, adb commands are only send to the device of this ID. |
196 Otherwise commands are sent to all attached devices. | 196 Otherwise commands are sent to all attached devices. |
197 """ | 197 """ |
198 | 198 |
199 def __init__(self, device=None): | 199 def __init__(self, device=None): |
200 self._adb = adb_interface.AdbInterface() | 200 self._adb = adb_interface.AdbInterface() |
201 if device: | 201 if device: |
202 self._adb.SetTargetSerial(device) | 202 self._adb.SetTargetSerial(device) |
203 self._logcat = None | 203 self._logcat = None |
| 204 self.logcat_process = None |
204 self._pushed_files = [] | 205 self._pushed_files = [] |
205 self._device_utc_offset = self.RunShellCommand('date +%z')[0] | 206 self._device_utc_offset = self.RunShellCommand('date +%z')[0] |
206 self._md5sum_path = '' | 207 self._md5sum_path = '' |
207 self._external_storage = '' | 208 self._external_storage = '' |
208 | 209 |
209 def Adb(self): | 210 def Adb(self): |
210 """Returns our AdbInterface to avoid us wrapping all its methods.""" | 211 """Returns our AdbInterface to avoid us wrapping all its methods.""" |
211 return self._adb | 212 return self._adb |
212 | 213 |
213 def IsRootEnabled(self): | 214 def IsRootEnabled(self): |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 All pushed files can be removed by calling RemovePushedFiles(). | 553 All pushed files can be removed by calling RemovePushedFiles(). |
553 """ | 554 """ |
554 assert os.path.exists(local_path), 'Local path not found %s' % local_path | 555 assert os.path.exists(local_path), 'Local path not found %s' % local_path |
555 | 556 |
556 if not self._md5sum_path: | 557 if not self._md5sum_path: |
557 default_build_type = os.environ.get('BUILD_TYPE', 'Debug') | 558 default_build_type = os.environ.get('BUILD_TYPE', 'Debug') |
558 md5sum_path = '%s/out/%s/md5sum_bin' % (CHROME_SRC, default_build_type) | 559 md5sum_path = '%s/out/%s/md5sum_bin' % (CHROME_SRC, default_build_type) |
559 if not os.path.exists(md5sum_path): | 560 if not os.path.exists(md5sum_path): |
560 md5sum_path = '%s/out/Release/md5sum_bin' % (CHROME_SRC) | 561 md5sum_path = '%s/out/Release/md5sum_bin' % (CHROME_SRC) |
561 if not os.path.exists(md5sum_path): | 562 if not os.path.exists(md5sum_path): |
562 print >>sys.stderr, 'Please build md5sum.' | 563 print >> sys.stderr, 'Please build md5sum.' |
563 sys.exit(1) | 564 sys.exit(1) |
564 command = 'push %s %s' % (md5sum_path, MD5SUM_DEVICE_PATH) | 565 command = 'push %s %s' % (md5sum_path, MD5SUM_DEVICE_PATH) |
565 assert _HasAdbPushSucceeded(self._adb.SendCommand(command)) | 566 assert _HasAdbPushSucceeded(self._adb.SendCommand(command)) |
566 self._md5sum_path = md5sum_path | 567 self._md5sum_path = md5sum_path |
567 | 568 |
568 self._pushed_files.append(device_path) | 569 self._pushed_files.append(device_path) |
569 hashes_on_device = _ComputeFileListHash( | 570 hashes_on_device = _ComputeFileListHash( |
570 self.RunShellCommand(MD5SUM_DEVICE_PATH + ' ' + device_path)) | 571 self.RunShellCommand(MD5SUM_DEVICE_PATH + ' ' + device_path)) |
571 assert os.path.exists(local_path), 'Local path not found %s' % local_path | 572 assert os.path.exists(local_path), 'Local path not found %s' % local_path |
572 hashes_on_host = _ComputeFileListHash( | 573 hashes_on_host = _ComputeFileListHash( |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 args = [] | 683 args = [] |
683 if self._adb._target_arg: | 684 if self._adb._target_arg: |
684 args += shlex.split(self._adb._target_arg) | 685 args += shlex.split(self._adb._target_arg) |
685 args += ['logcat', '-v', 'threadtime'] | 686 args += ['logcat', '-v', 'threadtime'] |
686 if filters: | 687 if filters: |
687 args.extend(filters) | 688 args.extend(filters) |
688 else: | 689 else: |
689 args.append('*:v') | 690 args.append('*:v') |
690 | 691 |
691 if logfile: | 692 if logfile: |
692 class NewLineNormalizer(object): | |
693 """A file-like object to normalize EOLs to '\n'. | |
694 | |
695 Pexpect runs adb within a pseudo-tty device (see | |
696 http://www.noah.org/wiki/pexpect), so any '\n' printed by adb is written | |
697 as '\r\n' to the logfile. Since adb already uses '\r\n' to terminate | |
698 lines, the log ends up having '\r\r\n' at the end of each line. This | |
699 filter replaces the above with a single '\n' in the data stream. | |
700 """ | |
701 def __init__(self, output): | |
702 self.output = output | |
703 | |
704 def write(self, data): | |
705 data = data.replace('\r\r\n', '\n') | |
706 self.output.write(data) | |
707 | |
708 def flush(self): | |
709 self.output.flush() | |
710 | |
711 logfile = NewLineNormalizer(logfile) | 693 logfile = NewLineNormalizer(logfile) |
712 | 694 |
713 # Spawn logcat and syncronize with it. | 695 # Spawn logcat and syncronize with it. |
714 for _ in range(4): | 696 for _ in range(4): |
715 self._logcat = pexpect.spawn('adb', args, timeout=timeout, | 697 self._logcat = pexpect.spawn('adb', args, timeout=timeout, |
716 logfile=logfile) | 698 logfile=logfile) |
717 self.RunShellCommand('log startup_sync') | 699 self.RunShellCommand('log startup_sync') |
718 if self._logcat.expect(['startup_sync', pexpect.EOF, | 700 if self._logcat.expect(['startup_sync', pexpect.EOF, |
719 pexpect.TIMEOUT]) == 0: | 701 pexpect.TIMEOUT]) == 0: |
720 break | 702 break |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1008 """ | 990 """ |
1009 assert '"' not in file_name, 'file_name cannot contain double quotes' | 991 assert '"' not in file_name, 'file_name cannot contain double quotes' |
1010 status = self._adb.SendShellCommand( | 992 status = self._adb.SendShellCommand( |
1011 '\'test -e "%s"; echo $?\'' % (file_name)) | 993 '\'test -e "%s"; echo $?\'' % (file_name)) |
1012 if 'test: not found' not in status: | 994 if 'test: not found' not in status: |
1013 return int(status) == 0 | 995 return int(status) == 0 |
1014 | 996 |
1015 status = self._adb.SendShellCommand( | 997 status = self._adb.SendShellCommand( |
1016 '\'ls "%s" >/dev/null 2>&1; echo $?\'' % (file_name)) | 998 '\'ls "%s" >/dev/null 2>&1; echo $?\'' % (file_name)) |
1017 return int(status) == 0 | 999 return int(status) == 0 |
| 1000 |
| 1001 |
| 1002 class NewLineNormalizer(object): |
| 1003 """A file-like object to normalize EOLs to '\n'. |
| 1004 |
| 1005 Pexpect runs adb within a pseudo-tty device (see |
| 1006 http://www.noah.org/wiki/pexpect), so any '\n' printed by adb is written |
| 1007 as '\r\n' to the logfile. Since adb already uses '\r\n' to terminate |
| 1008 lines, the log ends up having '\r\r\n' at the end of each line. This |
| 1009 filter replaces the above with a single '\n' in the data stream. |
| 1010 """ |
| 1011 def __init__(self, output): |
| 1012 self._output = output |
| 1013 |
| 1014 def write(self, data): |
| 1015 data = data.replace('\r\r\n', '\n') |
| 1016 self._output.write(data) |
| 1017 |
| 1018 def flush(self): |
| 1019 self._output.flush() |
OLD | NEW |