Passed
Push — master ( dc4f5b...b7297b )
by Konrad
10:09
created

db_sync_tool.remote.transfer.get_sftp_client()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
#!/usr/bin/env python3
2
# -*- coding: future_fstrings -*-
3
4
"""
5
Transfer script
6
"""
7
8
import sys
9
from db_sync_tool.utility import mode, system, helper, output
10
from db_sync_tool.database import utility as database_utility
11
from db_sync_tool.remote import utility, client
12
13
14
def transfer_origin_database_dump():
15
    """
16
    Transfer the origin database dump files
17
    :return:
18
    """
19
    if not mode.is_import():
20
        if mode.get_sync_mode() == mode.SyncMode.RECEIVER:
21
            get_origin_database_dump(helper.get_dump_dir(mode.Client.TARGET))
22
            system.check_target_configuration()
23
        elif mode.get_sync_mode() == mode.SyncMode.SENDER:
24
            system.check_target_configuration()
25
            put_origin_database_dump(helper.get_dump_dir(mode.Client.ORIGIN))
26
            utility.remove_origin_database_dump()
27
        elif mode.get_sync_mode() == mode.SyncMode.PROXY:
28
            helper.create_local_temporary_data_dir()
29
            get_origin_database_dump(system.default_local_sync_path)
30
            system.check_target_configuration()
31
            put_origin_database_dump(system.default_local_sync_path)
32
        elif mode.get_sync_mode() == mode.SyncMode.SYNC_REMOTE or mode.get_sync_mode() == mode.SyncMode.SYNC_LOCAL:
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (115/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
33
            system.check_target_configuration()
34
        elif system.config['is_same_client']:
35
            utility.remove_origin_database_dump(True)
36
    else:
37
        system.check_target_configuration()
38
39
40
def get_origin_database_dump(target_path):
41
    """
42
    Downloading the origin database dump files
43
    :param target_path: String
44
    :return:
45
    """
46
    sftp = get_sftp_client(client.ssh_client_origin)
47
48
    output.message(
49
        output.Subject.ORIGIN,
50
        'Downloading database dump',
51
        True
52
    )
53
    if mode.get_sync_mode() != mode.SyncMode.PROXY:
54
        helper.check_and_create_dump_dir(mode.Client.TARGET, target_path)
55
56
    #
57
    # Download speed problems
58
    # https://github.com/paramiko/paramiko/issues/60
59
    #
60
    if not system.config['dry_run']:
61
        sftp.get(helper.get_dump_dir(mode.Client.ORIGIN) + database_utility.database_dump_file_name + '.tar.gz',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (112/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
62
                 target_path + database_utility.database_dump_file_name + '.tar.gz', download_status)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
63
        if not system.config['mute']:
64
            print('')
65
66
    sftp.close()
67
    utility.remove_origin_database_dump()
68
69
70
def download_status(sent, size):
71
    """
72
    Printing the download status information
73
    :param sent: Float
74
    :param size: Float
75
    :return:
76
    """
77
    if not system.config['mute']:
78
        sent_mb = round(float(sent) / 1024 / 1024, 1)
79
        size = round(float(size) / 1024 / 1024, 1)
80
        sys.stdout.write(
81
            output.Subject.ORIGIN + output.CliFormat.BLACK + '[REMOTE]' + output.CliFormat.ENDC + " Status: {0} MB of {1} MB downloaded".
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (137/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
82
            format(sent_mb, size, ))
83
        sys.stdout.write('\r')
84
85
86
def put_origin_database_dump(origin_path):
87
    """
88
    Uploading the origin database dump file
89
    :param origin_path: String
90
    :return:
91
    """
92
    sftp = get_sftp_client(client.ssh_client_target)
93
94
    if mode.get_sync_mode() == mode.SyncMode.PROXY:
95
        _subject = output.Subject.LOCAL
96
    else:
97
        _subject = output.Subject.ORIGIN
98
99
    output.message(
100
        _subject,
101
        'Uploading database dump',
102
        True
103
    )
104
    helper.check_and_create_dump_dir(mode.Client.TARGET, helper.get_dump_dir(mode.Client.TARGET))
105
106
    #
107
    # Download speed problems
108
    # https://github.com/paramiko/paramiko/issues/60
109
    #
110
    if not system.config['dry_run']:
111
        sftp.put(origin_path + database_utility.database_dump_file_name + '.tar.gz',
112
                 helper.get_dump_dir(mode.Client.TARGET) + database_utility.database_dump_file_name + '.tar.gz',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (112/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
113
                 upload_status)
114
        if not system.config['mute']:
115
            print('')
116
117
    sftp.close()
118
119
120
def upload_status(sent, size):
121
    """
122
    Printing the upload status information
123
    :param sent: Float
124
    :param size: Float
125
    :return:
126
    """
127
    if not system.config['mute']:
128
        sent_mb = round(float(sent) / 1024 / 1024, 1)
129
        size = round(float(size) / 1024 / 1024, 1)
130
131
        if (mode.get_sync_mode() == mode.SyncMode.PROXY):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after if.
Loading history...
132
            _subject = output.Subject.LOCAL
133
        else:
134
            _subject = output.Subject.ORIGIN + output.CliFormat.BLACK + '[LOCAL]' + output.CliFormat.ENDC
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (105/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
135
136
        sys.stdout.write(
137
            _subject + " Status: {0} MB of {1} MB uploaded".
138
            format(sent_mb, size, ))
139
        sys.stdout.write('\r')
140
141
142
def get_sftp_client(ssh_client):
143
    """
144
145
    :param ssh_client:
146
    :return:
147
    """
148
    sftp = ssh_client.open_sftp()
149
    sftp.get_channel().settimeout(client.default_timeout)
150
    return sftp
151