def cmd(self, cmds, device=True, ensure_unicode=True): """ 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) 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) 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 if re.search(DeviceConnectionError.DEVICE_CONNECTION_ERROR, stderr): raise DeviceConnectionError(stderr) else: raise AdbError(stdout, stderr) return stdout
def get_status(self): """ Perform `adb get-state` and return the device status Raises: AdbError: if status cannot be obtained from the device Returns: None if status is `not found`, otherwise return the standard output from `adb get-state` command """ proc = self.start_cmd("get-state") stdout, stderr = proc.communicate() stdout = stdout.decode(get_std_encoding(sys.stdout)) stderr = stderr.decode(get_std_encoding(sys.stdout)) if proc.returncode == 0: return stdout.strip() elif "not found" in stderr: return None else: raise AdbError(stdout, stderr)