def _exec(self, cmd: str, fail_on_error: bool = False,
error_message: str = "Failed executing {cmd!r}: out={out!r}, err={err!r}",
timeout: int = 10) -> bool:
"""
Execute the passed command.
:param cmd:
:param fail_on_error:
:param error_message: error message format
:param timeout: time in seconds after which the command is aborted
:return: Was the command executed successfully?
"""
out_mode = subprocess.PIPE if fail_on_error else subprocess.DEVNULL
proc = subprocess.Popen(["/bin/sh", "-c", cmd], stdout=out_mode, stderr=out_mode,
universal_newlines=True)
out, err = proc.communicate(timeout=timeout)
if proc.wait() > 0:
if fail_on_error:
self._fail(error_message.format(cmd=cmd, out=out, err=err))
else:
return False
return True
评论列表
文章目录