subprocess-util/exec_print_receive.py

73 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import sys
import threading
from pathlib import Path
from typing import IO, AnyStr
from receive_inform import receive_inform
def _print_stdout(bin_pipe: IO[AnyStr]):
b: bytes
for b in bin_pipe:
sys.stdout.write(f'[STDOUT] {b.decode("UTF-8")}')
# TODO: Has this any effect?
# bin_pipe.close()
def _print_stderr(bin_pipe: IO[AnyStr]):
b: bytes
for b in bin_pipe:
sys.stderr.write(f'[STDERR] {b.decode("UTF-8")}')
# TODO: Has this any effect?
# bin_pipe.close()
def execute_print_receive_chunks(command: list[str],
socket_file: Path,
chunk_file_tmpl: Path) -> int:
process = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True,
)
# TODO - This function
# - execute the command (print live stdout/stderr)
#
# TODO - Inside receive_inform()
# - while True
# - wait for message at socket_file
# - if 'OK\n'
# - determine chunk filename
# - read chunk, pass to stdin of command
# - delete chunk
# - if 'EOF\n'
# - break
#
# TODO - This function
# - wait for command to finish
t_out = threading.Thread(
target=_print_stdout, args=(process.stdout,))
t_err = threading.Thread(
target=_print_stderr, args=(process.stderr,))
for t in (t_out, t_err):
t.daemon = True
t.start()
receive_inform(process.stdin, socket_file, chunk_file_tmpl)
returncode = process.wait()
for t in (t_out, t_err):
t.join()
return returncode