file_sync_tool.utility.helper.dict_to_args()   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nop 1
dl 0
loc 17
rs 9.3333
c 0
b 0
f 0
1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# -*- coding: future_fstrings -*-
3
4
import sys
0 ignored issues
show
Unused Code introduced by
The import sys seems to be unused.
Loading history...
5
import os
0 ignored issues
show
Unused Code introduced by
The import os seems to be unused.
Loading history...
6
import re
7
from db_sync_tool.utility import mode, system, output
8
from file_sync_tool.transfer import utility
0 ignored issues
show
Unused Code introduced by
Unused utility imported from file_sync_tool.transfer
Loading history...
9
10
11
def check_rsync_version():
12
    """
13
    Check rsync version
14
    :return:
15
    """
16
    _raw_version = mode.run_command(
17
        'rsync --version',
18
        mode.Client.LOCAL,
19
        True
20
    )
21
    _version = parse_version(_raw_version)
22
    output.message(
23
        output.Subject.LOCAL,
24
        f'rsync version {_version}'
25
    )
26
27
28
def check_sshpass_version():
0 ignored issues
show
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
29
    """
30
    Check sshpass version
31
    :return:
32
    """
33
    system.config['use_sshpass'] = False
34
    _raw_version = mode.run_command(
35
        'sshpass -V',
36
        mode.Client.LOCAL,
37
        force_output=True,
38
        allow_fail=True
39
    )
40
    _version = parse_version(_raw_version)
41
42
    if _version:
43
        output.message(
44
            output.Subject.LOCAL,
45
            f'sshpass version {_version}'
46
        )
47
        system.config['use_sshpass'] = True
48
        return True
49
50
51
def parse_version(output):
0 ignored issues
show
Comprehensibility Bug introduced by
output is re-defining a name which is already available in the outer-scope (previously defined on line 7).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
52
    """
53
    Parse version out of console output
54
    https://stackoverflow.com/a/60730346
55
    :param output: String
56
    :return:
57
    """
58
    _version_pattern = r'\d+(=?\.(\d+(=?\.(\d+)*)*)*)*'
59
    _regex_matcher = re.compile(_version_pattern)
60
    _version = _regex_matcher.search(output)
61
    if _version:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
62
        return _version.group(0)
63
    else:
64
        return None
65
66
67
def check_authorizations():
68
    """
69
    Check authorization for clients
70
    :return:
71
    """
72
    if system.config['use_sshpass']:
73
        # When using sshpass, check for passwords
74
        system.check_authorization(mode.Client.ORIGIN)
75
        system.check_authorization(mode.Client.TARGET)
76
    elif not 'ssh_key' in system.config[mode.Client.ORIGIN] and \
77
            not 'ssh_key' in system.config[mode.Client.TARGET] and \
78
            (mode.get_sync_mode() == mode.SyncMode.PROXY or len(system.config['files']['config']) > 1):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

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

Loading history...
79
        # Suggest to install sshpass
80
        output.message(
81
            output.Subject.INFO,
82
            f'Suggestion: Install {output.CliFormat.BOLD}sshpass{output.CliFormat.ENDC} to avoid multiple input of ssh passwords'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (129/100).

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

Loading history...
83
        )
84
85
86
def dict_to_args(dict):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in dict.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
87
    """
88
    Convert an dictionary to a args list
89
    :param dict: Dictionary
90
    :return: List
91
    """
92
    _args = []
93
    for key, val in dict.items():
94
        if isinstance(val, bool):
95
            if val:
96
                _args.append(f'--{key}')
97
        else:
98
            _args.append(f'--{key}')
99
            _args.append(str(val))
100
    if len(_args) == 0:
101
        return None
102
    return _args
103
104
105
def adjust_sync_mode():
106
    """
107
108
    :return:
109
    """
110
    if mode.get_sync_mode() == mode.SyncMode.DUMP_LOCAL:
111
        mode.sync_mode = mode.SyncMode.SYNC_LOCAL
112
    if mode.get_sync_mode() == mode.SyncMode.DUMP_REMOTE:
113
        mode.sync_mode = mode.SyncMode.SYNC_REMOTE
114
115
116
def extend_config(args):
117
    """
118
    Extending optional config
119
    :param args:
120
    :return:
121
    """
122
    config = system.config
123
124
    if args is None or not args:
125
        return config
126
127
    if not args.target_host is None:
128
        config['target']['host'] = args.target_host
129
130
    if not args.target_user is None:
131
        config['target']['user'] = args.target_user
132
133
    if not args.target_password is None:
134
        config['target']['password'] = args.target_password
135
136
    if not args.target_key is None:
137
        config['target']['ssh_key'] = args.target_key
138
139
    if not args.target_port is None:
140
        config['target']['port'] = args.target_port
141
142
    if not args.origin_host is None:
143
        config['origin']['host'] = args.origin_host
144
145
    if not args.origin_user is None:
146
        config['origin']['user'] = args.origin_user
147
148
    if not args.origin_password is None:
149
        config['origin']['password'] = args.origin_password
150
151
    if not args.origin_key is None:
152
        config['origin']['ssh_key'] = args.origin_key
153
154
    if not args.origin_port is None:
155
        config['origin']['port'] = args.origin_port
156
157
    if not args.files_origin is None:
158
        if 'config' not in config['files']:
159
            config['files']['config'] = []
160
            config['files']['config'].append({})
161
        config['files']['config'][0]['origin'] = args.files_origin
162
163
    if not args.files_target is None:
164
        if 'config' not in config['files']:
165
            config['files']['config'] = []
166
            config['files']['config'].append({})
167
        config['files']['config'][0]['target'] = args.files_target
168
169
    if not args.files_exclude is None:
170
        config['files']['config'][0]['exclude'] = args.files_exclude.split(',')
171
172
    if not args.files_option is None:
173
        config['files']['option'] = args.files_option.split(',')
174
175
    return config
176