make subprocess_readlines more robust

This commit is contained in:
Bence Pőcze 2023-09-14 19:27:32 +02:00
parent 52796e95cf
commit 6e5c3d7f07
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -3,14 +3,13 @@ from typing import Iterator
def subprocess_readlines(cmd, cwd=None) -> Iterator[str]: def subprocess_readlines(cmd, cwd=None) -> Iterator[str]:
process = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE) process = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, text=True)
for line in process.stdout: for line in process.stdout:
line = line.decode().rstrip(r'\n') line = line.rstrip('\n')
yield line yield line
process.wait() process.communicate()
if process.returncode != 0: if process.returncode != 0:
raise subprocess.CalledProcessError(process.returncode, cmd) raise subprocess.CalledProcessError(process.returncode, cmd)