From 8e09cb3ed90b1d1a953f942836edfce400983ad5 Mon Sep 17 00:00:00 2001 From: Artem Bliznetsov Date: Thu, 11 Apr 2024 00:26:30 +0300 Subject: [PATCH] scp-helper --- scp_helper => helperlib.py | 80 +++++++++++++++++--------------------- scp-helper | 39 +++++++++++++++++++ 2 files changed, 75 insertions(+), 44 deletions(-) rename scp_helper => helperlib.py (65%) mode change 100755 => 100644 create mode 100755 scp-helper diff --git a/scp_helper b/helperlib.py old mode 100755 new mode 100644 similarity index 65% rename from scp_helper rename to helperlib.py index 2d0818c..61c1b2f --- a/scp_helper +++ b/helperlib.py @@ -1,29 +1,47 @@ -#!/usr/bin/env python3 -# Копирование файлов из/на сервер -# scp_helper [host1:]/path [host2:]/path -# - -import argparse import os CMD_TAR_DECOMP='tar -xz' CMD_TAR_COMP='tar -cz' SUDO_REMOTE=True +def get_config(): + config_path = os.environ.get('SSH_HELPER_HOST_LIST') + config = {} + with open(config_path, 'r') as f: + for line in f: + if line[0] not in ('#', ' ', '\n'): + connection_name, host, user, port, *_ = line.split() + config[connection_name] = { + 'host': host, + 'user': user, + 'port': port + } + return config + def parse_arg(s): - # Парсим путь. Проверяем тип пути + # Парсим путь. Разделяем на 3 части: + # хост - если не указан, то вызвращает local + # work_path - рабочая директория + # obj - обхект в рабочей директории + if s.find(':') > -1: connection_name, path = s.split(':') else: connection_name = 'local' path = s - - if path[0] != '/': - path = './' + path - path_parts = path.split('/') - work_path = '/'.join(path_parts[0:-1]) - obj = path_parts[-1] + if path[0] not in ('/', '~', '.'): + if connection_name == 'local': + path = './' + path + else: + path = '~/' + path + + last_slash = path.rfind('/') + + work_path = path[0:last_slash] + if len(work_path) == 0: + work_path = '/' + obj = path[last_slash+1:] return { 'connection_name': connection_name, @@ -32,10 +50,14 @@ def parse_arg(s): } def make_pack_cmd(work_path, obj): + assert obj is not None + assert len(obj) > 0 cmd = f'tar -C {work_path} -c {obj}' return cmd def make_unpack_cmd(work_path, obj): + if work_path == '/': + work_path = '' cmd = f'tar -C {work_path}/{obj} -x' return cmd @@ -49,7 +71,7 @@ def make_ssh_cmd(host, port, user, cmd): def get_connection_config(con_name): # Получаем конфигурацию подключения по SSH - config_path = os.environ.get('SSH_CONNECTOR_HOST_LIST') + config_path = os.environ.get('SSH_HELPER_HOST_LIST') res = None with open(config_path, 'r') as f: for line in f: @@ -98,33 +120,3 @@ def make_full_cmd(config, tar_func): ) return cmd - -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description='Copy files' - ) - parser.add_argument('in_path', type=str) - parser.add_argument('out_path', type=str) - - - args = parser.parse_args() - - in_config = get_path_config(args.in_path) - out_config = get_path_config(args.out_path) - - in_cmd = make_full_cmd( - in_config, - make_pack_cmd - ) - - out_cmd = make_full_cmd( - out_config, - make_unpack_cmd - ) - - full_cmd = f'{in_cmd} | {out_cmd}' - print(full_cmd) - - - - diff --git a/scp-helper b/scp-helper new file mode 100755 index 0000000..4a74d84 --- /dev/null +++ b/scp-helper @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# Копирование файлов из/на сервер +# scp_helper [host1:]/path [host2:]/path + +import argparse +import os + +from helperlib import * + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Copy files' + ) + parser.add_argument('in_path', type=str) + parser.add_argument('out_path', type=str) + + args = parser.parse_args() + + in_config = get_path_config(args.in_path) + out_config = get_path_config(args.out_path) + + in_cmd = make_full_cmd( + in_config, + make_pack_cmd + ) + + out_cmd = make_full_cmd( + out_config, + make_unpack_cmd + ) + + full_cmd = f'{in_cmd} | {out_cmd}' + print("Будет выполнена команда:") + print(full_cmd) + print("продолжить (y/n)") + if input() in ('y','Y'): + os.system(full_cmd) + +