2023-01-11 11:04:27 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
from pathlib import Path
|
2023-01-11 11:44:22 +01:00
|
|
|
from typing import IO, AnyStr, Callable
|
2023-01-11 11:04:27 +01:00
|
|
|
|
2023-01-11 11:44:22 +01:00
|
|
|
from get_chunk_file import _get_chunk_file
|
2023-01-11 11:04:27 +01:00
|
|
|
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,
|
2023-01-11 11:44:22 +01:00
|
|
|
chunk_file_tmpl: Path,
|
|
|
|
get_chunk_file: Callable[[Path, int], Path] = _get_chunk_file,
|
|
|
|
) -> int:
|
2023-01-11 11:04:27 +01:00
|
|
|
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()
|
|
|
|
|
2023-01-11 11:44:22 +01:00
|
|
|
receive_inform(process.stdin, socket_file, chunk_file_tmpl, get_chunk_file)
|
2023-01-11 11:04:27 +01:00
|
|
|
|
|
|
|
returncode = process.wait()
|
|
|
|
for t in (t_out, t_err):
|
|
|
|
t.join()
|
|
|
|
|
|
|
|
return returncode
|