def wait_for_device(self, timeout=5): """ Perform `adb wait-for-device` command Args: timeout: time interval in seconds to wait for device Raises: DeviceConnectionError: if device is not available after timeout Returns: None """ proc = self.start_cmd("wait-for-device") timer = threading.Timer(timeout, proc.kill) timer.start() ret = proc.wait() if ret == 0: timer.cancel() else: raise DeviceConnectionError("device not ready")
def cmd(self, cmds, device=True, ensure_unicode=True, timeout=None): """ Run the adb command(s) in subprocess and return the standard output Args: cmds: command(s) to be run device: if True, the device serial number must be specified by -s serialno argument ensure_unicode: encode/decode unicode of standard outputs (stdout, stderr) timeout: timeout in seconds Raises: DeviceConnectionError: if any error occurs when connecting the device AdbError: if any other adb error occurs Returns: command(s) standard output (stdout) """ proc = self.start_cmd(cmds, device) if timeout: stdout, stderr = proc_communicate_timeout(proc, timeout) else: stdout, stderr = proc.communicate() if ensure_unicode: stdout = stdout.decode(get_std_encoding(sys.stdout)) stderr = stderr.decode(get_std_encoding(sys.stderr)) if proc.returncode > 0: # adb connection error pattern = DeviceConnectionError.DEVICE_CONNECTION_ERROR if isinstance(stderr, binary_type): pattern = pattern.encode("utf-8") if re.search(pattern, stderr): raise DeviceConnectionError(stderr) else: raise AdbError(stdout, stderr) return stdout