Execute a process and stream output to logger :param cmd: command and arguments to run :type cmd: List[str]
(cmd: List[str])
| 126 | |
| 127 | |
| 128 | def execute_in_subprocess(cmd: List[str]): |
| 129 | """ |
| 130 | Execute a process and stream output to logger |
| 131 | |
| 132 | :param cmd: command and arguments to run |
| 133 | :type cmd: List[str] |
| 134 | """ |
| 135 | log.info("Executing cmd: %s", " ".join([shlex.quote(c) for c in cmd])) |
| 136 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=0, close_fds=True) |
| 137 | log.info("Output:") |
| 138 | if proc.stdout: |
| 139 | with proc.stdout: |
| 140 | for line in iter(proc.stdout.readline, b''): |
| 141 | log.info("%s", line.decode().rstrip()) |
| 142 | |
| 143 | exit_code = proc.wait() |
| 144 | if exit_code != 0: |
| 145 | raise subprocess.CalledProcessError(exit_code, cmd) |
| 146 | |
| 147 | |
| 148 | def execute_interactive(cmd: List[str], **kwargs): |