def _exec_cmd(self, args, shell, timeout):
"""Executes adb commands.
Args:
args: string or list of strings, program arguments.
See subprocess.Popen() documentation.
shell: bool, True to run this command through the system shell,
False to invoke it directly. See subprocess.Popen() docs.
timeout: float, the number of seconds to wait before timing out.
If not specified, no timeout takes effect.
Returns:
The output of the adb command run if exit code is 0.
Raises:
AdbError: The adb command exit code is not 0.
AdbTimeoutError: The adb command timed out.
"""
proc = subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)
process = psutil.Process(proc.pid)
if timeout and timeout <= 0:
raise Error('Timeout is not a positive value: %s' % timeout)
if timeout and timeout > 0:
try:
process.wait(timeout=timeout)
except psutil.TimeoutExpired:
process.terminate()
raise AdbTimeoutError(cmd=args, timeout=timeout)
(out, err) = proc.communicate()
ret = proc.returncode
logging.debug('cmd: %s, stdout: %s, stderr: %s, ret: %s',
cli_cmd_to_string(args), out, err, ret)
if ret == 0:
return out
else:
raise AdbError(cmd=args, stdout=out, stderr=err, ret_code=ret)
评论列表
文章目录