subprocess-util/transfer_inform.py

55 lines
1.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pathlib import Path
from exec_print_capture import execute_print_capture
from unix_sock_input import wait_until_command_received
def transfer_inform(rsync_cmd: list[str],
inform_cmd: list[str],
user_input_file: Path):
"""
First, this method transfers files to a remote pc.
Then, it informs the remote pc.
If some error occurs during the above steps,
it waits for user input (via a UNIX-socket) and repeats the failed step.
:param rsync_cmd:
:param inform_cmd:
:param user_input_file:
:return:
"""
# - rsync to remote pc
# - catch error
# - wait until user input, then repeat
_execute_loop_until_successful(rsync_cmd, user_input_file)
# - inform remote pc about complete rsync
# - catch error
# - wait until user input, then repeat
_execute_loop_until_successful(inform_cmd, user_input_file)
def _execute_loop_until_successful(cmd: list[str], user_input_file: Path):
while True:
returncode, _out, _err = execute_print_capture(cmd)
if returncode == 0:
break
else:
print(f'\n'
f'Error while executing:\n'
f'\t{cmd}\n'
f'\tFor details, see above output.')
print(f'Info:\n'
f'\tPlease fix the above error first. Then continue here:\n'
f'\tsudo pacman -S --needed openbsd-netcat\n'
f'\techo "OK" | nc -U "{user_input_file.absolute()}"')
wait_until_command_received(user_input_file, [b'OK\n'])
print()