This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c00f8c7
commit cece64b
Showing
4 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import sys | ||
import subprocess | ||
from threading import Thread | ||
from io import StringIO | ||
|
||
|
||
def check_call(command: str, stdin=None): | ||
os.environ['PYTHONUNBUFFERED'] = "1" | ||
|
||
stdout_pipe_r, stdout_pipe_w = os.pipe() | ||
stderr_pipe_r, stderr_pipe_w = os.pipe() | ||
|
||
# keep the last 1024 characters of stderr | ||
err_buffer = StringIO() | ||
out_buffer = StringIO() | ||
|
||
process = subprocess.Popen('bash', shell=True, stdin=stdin, stdout=stdout_pipe_w, stderr=stderr_pipe_w, | ||
bufsize=1) | ||
|
||
stdout_thread = Thread(target=_copy_stream, args=(stdout_pipe_r, sys.stdout, process, out_buffer)) | ||
stdout_thread.daemon = True | ||
stdout_thread.start() | ||
|
||
stderr_thread = Thread(target=_copy_stream, args=(stderr_pipe_r, sys.stderr, process, err_buffer)) | ||
stderr_thread.daemon = True | ||
stderr_thread.start() | ||
|
||
exit_code = process.wait() | ||
|
||
if exit_code > 0: | ||
raise subprocess.CalledProcessError( | ||
exit_code, command, stderr=err_buffer.getvalue(), output=out_buffer.getvalue() | ||
) | ||
|
||
|
||
def _copy_stream(in_stream_fd: int, out_stream, process: subprocess.Popen, copy: StringIO = None): | ||
buffer_wrote_size = 0 | ||
|
||
while process.poll() is None: | ||
read = os.read(in_stream_fd, 1024).decode('utf-8') | ||
out_stream.write(read) | ||
|
||
if copy: | ||
if buffer_wrote_size >= 1024: | ||
copy.truncate() | ||
|
||
buffer_wrote_size += len(read) | ||
copy.write(read) | ||
|
||
read = os.read(in_stream_fd, 1024).decode('utf-8') | ||
out_stream.write(read) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters