From a0e2db42e6016fe5443de1f91e639114bbb1899e Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sun, 19 Mar 2023 16:33:58 +0100 Subject: [PATCH] refactor --- .../chunked_transfer/active_send/common.py | 10 ++++++++++ .../active_send/exec_receive_chunks_passive.py | 5 +++-- .../active_send/exec_send_chunks_active.py | 5 +++-- 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 src/subprocess_util/chunked_transfer/active_send/common.py diff --git a/src/subprocess_util/chunked_transfer/active_send/common.py b/src/subprocess_util/chunked_transfer/active_send/common.py new file mode 100644 index 0000000..6833cef --- /dev/null +++ b/src/subprocess_util/chunked_transfer/active_send/common.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from enum import Enum + + +class NextChunkEnum(Enum): + OK_BINARY = b'OK\n' + OK_STR = 'OK\n' + EOF_BINARY = b'EOF\n' + EOF_STR = 'EOF\n' diff --git a/src/subprocess_util/chunked_transfer/active_send/exec_receive_chunks_passive.py b/src/subprocess_util/chunked_transfer/active_send/exec_receive_chunks_passive.py index 4493f59..0f6481b 100644 --- a/src/subprocess_util/chunked_transfer/active_send/exec_receive_chunks_passive.py +++ b/src/subprocess_util/chunked_transfer/active_send/exec_receive_chunks_passive.py @@ -2,6 +2,7 @@ import socket from pathlib import Path from queue import Queue +from subprocess_util.chunked_transfer.active_send.common import NextChunkEnum from subprocess_util.exec_consume_chunks import execute_consume_chunks from subprocess_util.unix_sock_input import accept_loop_until_message @@ -23,11 +24,11 @@ def exec_receive_chunks_passive( sock.listen(1) chunk_no = 1 - messages = [b'OK\n', b'EOF\n'] + messages: list[bytes] = [NextChunkEnum.OK_BINARY.value, NextChunkEnum.EOF_BINARY.value] while True: msg = accept_loop_until_message(sock, messages) - last_chunk = msg == b'EOF\n' + last_chunk = msg == NextChunkEnum.EOF_BINARY.value target_chunk_path = get_target_chunk_path(chunk_no) queue_put((target_chunk_path, last_chunk)) diff --git a/src/subprocess_util/chunked_transfer/active_send/exec_send_chunks_active.py b/src/subprocess_util/chunked_transfer/active_send/exec_send_chunks_active.py index 871c4b6..5f91f9f 100644 --- a/src/subprocess_util/chunked_transfer/active_send/exec_send_chunks_active.py +++ b/src/subprocess_util/chunked_transfer/active_send/exec_send_chunks_active.py @@ -1,6 +1,7 @@ import shlex from pathlib import Path +from subprocess_util.chunked_transfer.active_send.common import NextChunkEnum from subprocess_util.exec_produce_chunks import execute_produce_chunks from subprocess_util.repeat import repeat_until_successful @@ -26,14 +27,14 @@ def exec_send_chunks_active( usr_confirmation_socket = source_chunk_path.parent.joinpath(f'{chunk_no}.SOCKET') target_socket = target_chunk_dir.joinpath('SOCKET') - message = 'EOF' if last_chunk else 'OK' + message: str = NextChunkEnum.EOF_STR.value if last_chunk else NextChunkEnum.OK_STR.value rsync_cmd = ['rsync', str(source_chunk_path), f'{ssh_target}:{str(target_chunk_path)}'] inform_cmd = ['ssh', ssh_target, - f'echo {message} | nc -U {shlex.quote(str(target_socket))}'] + f"printf '{message}' | nc -U {shlex.quote(str(target_socket))}"] repeat_until_successful(rsync_cmd, usr_confirmation_socket) # Delete local chunk after it has been transferred.