import socket from pathlib import Path from unix_sock_input import accept_loop_until_command_received def receive_inform(cmd: list[str], socket_file: Path, chunk_file_tmpl: Path): """ :param chunk_file_tmpl: :param cmd: :param socket_file: Create a UNIX socket and wait for messages. :return: """ # TODO # - execute the command # TODO # - 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 print(f'Listening on socket {socket_file}') sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(str(socket_file)) sock.listen(1) ct = 1 commands = [b'OK\n', b'EOF\n'] while True: command = accept_loop_until_command_received(sock, commands) if command == b'OK\n': chunk_file = chunk_file_tmpl.parent.joinpath(f'{chunk_file_tmpl.name}.{ct}') chunk = chunk_file.read_bytes() # TODO send chunk to stdin of command chunk_file.unlink(missing_ok=False) ct += 1 elif command == b'EOF\n': break else: raise ValueError("Invalid state") print(f'Closing socket {socket_file}') sock.close() socket_file.unlink(missing_ok=False) # TODO # - wait for command to finish