1 """The command_line component utils."""
3 from __future__
import annotations
8 _LOGGER = logging.getLogger(__name__)
9 _EXEC_FAILED_CODE = 127
13 command: str, timeout: int, *, log_return_code: bool =
True
15 """Run a shell command with a timeout.
17 If log_return_code is set to False, it will not print an error if a non-zero
18 return code is returned.
21 _LOGGER.debug(
"Running command: %s", command)
22 proc = await asyncio.create_subprocess_shell(
26 async
with asyncio.timeout(timeout):
27 await proc.communicate()
29 _LOGGER.error(
"Timeout for command: %s", command)
32 return_code = proc.returncode
33 if return_code == _EXEC_FAILED_CODE:
34 _LOGGER.error(
"Error trying to exec command: %s", command)
35 elif log_return_code
and return_code != 0:
37 "Command failed (with return code %s): %s",
41 return return_code
or 0
45 """Run a shell command with a timeout and return the output."""
47 proc = await asyncio.create_subprocess_shell(
50 stdout=asyncio.subprocess.PIPE,
52 async
with asyncio.timeout(timeout):
53 stdout, _ = await proc.communicate()
55 if proc.returncode != 0:
57 "Command failed (with return code %s): %s", proc.returncode, command
60 return stdout.strip().decode(
"utf-8")
62 _LOGGER.error(
"Timeout for command: %s", command)
int async_call_shell_with_timeout(str command, int timeout, *bool log_return_code=True)
str|None async_check_output_or_log(str command, int timeout)