Passed
Push — master ( ffbe9f...5c3c8b )
by Konrad
01:19
created

db_sync_tool.remote.transfer.upload_status()   A

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
rs 9.85
c 0
b 0
f 0
cc 3
nop 2
1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# -*- coding: utf-8 -*-
3
4
import sys
5
from db_sync_tool.utility import mode, system, helper, output
6
from db_sync_tool.database import utility as database_utility
7
from db_sync_tool.remote import utility, client
8
9
10
def transfer_origin_database_dump():
11
    """
12
    Transfer the origin database dump files
13
    :return:
14
    """
15
    if not mode.is_import():
16
        if mode.get_sync_mode() == mode.SyncMode.RECEIVER:
17
            get_origin_database_dump(helper.get_dump_dir(mode.Client.TARGET))
18
            system.check_target_configuration()
19
        elif mode.get_sync_mode() == mode.SyncMode.SENDER:
20
            system.check_target_configuration()
21
            put_origin_database_dump(helper.get_dump_dir(mode.Client.ORIGIN))
22
            utility.remove_origin_database_dump()
23
        elif mode.get_sync_mode() == mode.SyncMode.PROXY:
24
            helper.create_local_temporary_data_dir()
25
            get_origin_database_dump(system.default_local_sync_path)
26
            system.check_target_configuration()
27
            put_origin_database_dump(system.default_local_sync_path)
28
        elif system.option['is_same_client']:
29
            utility.remove_origin_database_dump(True)
30
    else:
31
        system.check_target_configuration()
32
33
34
def get_origin_database_dump(target_path):
35
    """
36
    Downloading the origin database dump files
37
    :param target_path: String
38
    :return:
39
    """
40
    sftp = client.ssh_client_origin.open_sftp()
41
    output.message(
42
        output.Subject.ORIGIN,
43
        'Downloading database dump',
44
        True
45
    )
46
    if mode.get_sync_mode() != mode.SyncMode.PROXY:
47
        helper.check_and_create_dump_dir(mode.Client.TARGET, target_path)
48
49
    #
50
    # ToDo: Download speed problems
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
51
    # https://github.com/paramiko/paramiko/issues/60
52
    #
53
    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 (108/100).

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

Loading history...
54
             target_path + database_utility.database_dump_file_name + '.tar.gz', download_status)
55
    sftp.close()
56
    if not system.option['mute']:
57
        print('')
58
59
    utility.remove_origin_database_dump()
60
61
62
def download_status(sent, size):
63
    """
64
    Printing the download status information
65
    :param sent: Float
66
    :param size: Float
67
    :return:
68
    """
69
    if not system.option['mute']:
70
        sent_mb = round(float(sent) / 1024 / 1024, 1)
71
        size = round(float(size) / 1024 / 1024, 1)
72
        sys.stdout.write(
73
            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...
74
            format(sent_mb, size, ))
75
        sys.stdout.write('\r')
76
77
78
def put_origin_database_dump(origin_path):
79
    """
80
    Uploading the origin database dump file
81
    :param origin_path: String
82
    :return:
83
    """
84
    sftp = client.ssh_client_target.open_sftp()
85
86
    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...
87
        _subject = output.Subject.LOCAL
88
    else:
89
        _subject = output.Subject.ORIGIN
90
91
    output.message(
92
        _subject,
93
        'Uploading database dump',
94
        True
95
    )
96
    helper.check_and_create_dump_dir(mode.Client.TARGET, helper.get_dump_dir(mode.Client.TARGET))
97
98
    #
99
    # ToDo: Download speed problems
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
100
    # https://github.com/paramiko/paramiko/issues/60
101
    #
102
    sftp.put(origin_path + database_utility.database_dump_file_name + '.tar.gz',
103
             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 (108/100).

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

Loading history...
104
             upload_status)
105
    sftp.close()
106
    if not system.option['mute']:
107
        print('')
108
109
110
def upload_status(sent, size):
111
    """
112
    Printing the upload status information
113
    :param sent: Float
114
    :param size: Float
115
    :return:
116
    """
117
    if not system.option['mute']:
118
        sent_mb = round(float(sent) / 1024 / 1024, 1)
119
        size = round(float(size) / 1024 / 1024, 1)
120
121
        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...
122
            _subject = output.Subject.LOCAL
123
        else:
124
            _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...
125
126
        sys.stdout.write(
127
            _subject + " Status: {0} MB of {1} MB uploaded".
128
            format(sent_mb, size, ))
129
        sys.stdout.write('\r')
130