db_sync_tool.__main__.get_arguments()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 212
Code Lines 204

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 204
dl 0
loc 212
rs 7
c 0
b 0
f 0
cc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/env python3
2
# -*- coding: future_fstrings -*-
3
4
"""
5
Main script
6
"""
7
8
import argparse
9
import os
10
import sys
11
from collections import defaultdict
0 ignored issues
show
Unused Code introduced by
Unused defaultdict imported from collections
Loading history...
12
13
# Workaround for ModuleNotFoundError
14
sys.path.append(os.getcwd())
15
from db_sync_tool import sync
0 ignored issues
show
introduced by
Import "from db_sync_tool import sync" should be placed at the top of the module
Loading history...
16
from db_sync_tool.utility import helper
0 ignored issues
show
introduced by
Import "from db_sync_tool.utility import helper" should be placed at the top of the module
Loading history...
17
18
19
def main(args=None):
20
    """
21
    Main entry point for the command line. Parse the arguments and call to the main process.
22
    :param args:
23
    :return:
24
    """
25
    if args is None:
26
        args = {}
27
28
    args = get_arguments(args)
29
    sync.Sync(
30
        config_file=args.config_file,
31
        verbose=args.verbose,
32
        yes=args.yes,
33
        mute=args.mute,
34
        dry_run=args.dry_run,
35
        import_file=args.import_file,
36
        dump_name=args.dump_name,
37
        keep_dump=args.keep_dump,
38
        host_file=args.host_file,
39
        clear=args.clear_database,
40
        force_password=args.force_password,
41
        use_rsync=args.use_rsync,
42
        use_rsync_options=args.use_rsync_options,
43
        reverse=args.reverse,
44
        args=args
45
    )
46
47
48
def get_arguments(args):
49
    """
50
    Parses and returns script arguments
51
    :param args:
52
    :return:
53
    """
54
    parser = argparse.ArgumentParser(prog='db_sync_tool',
55
                                     description='A tool for automatic database synchronization from '
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

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

Loading history...
56
                                                 'and to host systems.')
57
    parser.add_argument('origin',
58
                        help='Origin database defined in host file',
59
                        nargs='?',
60
                        type=str)
61
    parser.add_argument('target',
62
                        help='Target database defined in host file',
63
                        nargs='?',
64
                        type=str)
65
    parser.add_argument('-f', '--config-file',
66
                        help='Path to configuration file',
67
                        required=False,
68
                        type=str)
69
    parser.add_argument('-v', '--verbose',
70
                        help='Enable extended console output',
71
                        required=False,
72
                        action='store_true')
73
    parser.add_argument('-y', '--yes',
74
                        help='Skipping user confirmation for database import',
75
                        required=False,
76
                        action='store_true')
77
    parser.add_argument('-m', '--mute',
78
                        help='Mute console output',
79
                        required=False,
80
                        action='store_true')
81
    parser.add_argument('-dr', '--dry-run',
82
                        help='Testing process without running database export, transfer or import.',
83
                        required=False,
84
                        action='store_true')
85
    parser.add_argument('-i', '--import-file',
86
                        help='Import database from a specific file dump',
87
                        required=False,
88
                        type=str)
89
    parser.add_argument('-dn', '--dump-name',
90
                        help='Set a specific dump file name (default is "_[dbname]_[date]")',
91
                        required=False,
92
                        type=str)
93
    parser.add_argument('-kd', '--keep-dump',
94
                        help='Skipping target import of the database dump and saving the available dump file in the '
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

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

Loading history...
95
                             'given directory',
96
                        required=False,
97
                        type=str)
98
    parser.add_argument('-o', '--host-file',
99
                        help='Using an additional hosts file for merging hosts information with the configuration file',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (120/100).

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

Loading history...
100
                        required=False,
101
                        type=str)
102
    parser.add_argument('-l', '--log-file',
103
                        help='File path for creating a additional log file',
104
                        required=False,
105
                        type=str)
106
    parser.add_argument('-cd', '--clear-database',
107
                        help='Dropping all tables before importing a new sync to get a clean database.',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

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

Loading history...
108
                        required=False,
109
                        action='store_true')
110
    parser.add_argument('-ta', '--tables',
111
                        help='Defining specific tables to export, e.g. --tables=table1,table2',
112
                        required=False,
113
                        type=str)
114
    parser.add_argument('-r', '--reverse',
115
                        help='Reverse origin and target hosts',
116
                        required=False,
117
                        action='store_true')
118
    parser.add_argument('-t', '--type',
119
                        help='Defining the framework type [TYPO3, Symfony, Drupal, Wordpress]',
120
                        required=False,
121
                        type=str)
122
    parser.add_argument('-tp', '--target-path',
123
                        help='File path to target database credential file depending on the framework type',
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...
124
                        required=False,
125
                        type=str)
126
    parser.add_argument('-tn', '--target-name',
127
                        help='Providing a name for the target system',
128
                        required=False,
129
                        type=str)
130
    parser.add_argument('-th', '--target-host',
131
                        help='SSH host to target system',
132
                        required=False,
133
                        type=str)
134
    parser.add_argument('-tu', '--target-user',
135
                        help='SSH user for target system',
136
                        required=False,
137
                        type=str)
138
    parser.add_argument('-tpw', '--target-password',
139
                        help='SSH password for target system',
140
                        required=False,
141
                        type=str)
142
    parser.add_argument('-tk', '--target-key',
143
                        help='File path to SSH key for target system',
144
                        required=False,
145
                        type=str)
146
    parser.add_argument('-tpo', '--target-port',
147
                        help='SSH port for target system',
148
                        required=False,
149
                        type=int)
150
    parser.add_argument('-tdd', '--target-dump-dir',
151
                        help='Directory path for database dump file on target system',
152
                        required=False,
153
                        type=str)
154
    parser.add_argument('-tkd', '--target-keep-dumps',
155
                        help='Keep dump file count for target system',
156
                        required=False,
157
                        type=int)
158
    parser.add_argument('-tdn', '--target-db-name',
159
                        help='Database name for target system',
160
                        required=False,
161
                        type=str)
162
    parser.add_argument('-tdh', '--target-db-host',
163
                        help='Database host for target system',
164
                        required=False,
165
                        type=str)
166
    parser.add_argument('-tdu', '--target-db-user',
167
                        help='Database user for target system',
168
                        required=False,
169
                        type=str)
170
    parser.add_argument('-tdpw', '--target-db-password',
171
                        help='Database password for target system',
172
                        required=False,
173
                        type=str)
174
    parser.add_argument('-tdpo', '--target-db-port',
175
                        help='Database port for target system',
176
                        required=False,
177
                        type=int)
178
    parser.add_argument('-tad', '--target-after-dump',
179
                        help='Additional dump file to insert after the regular database import',
180
                        required=False,
181
                        type=int)
182
    parser.add_argument('-op', '--origin-path',
183
                        help='File path to origin database credential file depending on the framework type',
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...
184
                        required=False,
185
                        type=str)
186
    parser.add_argument('-on', '--origin-name',
187
                        help='Providing a name for the origin system',
188
                        required=False,
189
                        type=str)
190
    parser.add_argument('-oh', '--origin-host',
191
                        help='SSH host to origin system',
192
                        required=False,
193
                        type=str)
194
    parser.add_argument('-ou', '--origin-user',
195
                        help='SSH user for origin system',
196
                        required=False,
197
                        type=str)
198
    parser.add_argument('-opw', '--origin-password',
199
                        help='SSH password for origin system',
200
                        required=False,
201
                        type=str)
202
    parser.add_argument('-ok', '--origin-key',
203
                        help='File path to SSH key for origin system',
204
                        required=False,
205
                        type=str)
206
    parser.add_argument('-opo', '--origin-port',
207
                        help='SSH port for origin system',
208
                        required=False,
209
                        type=int)
210
    parser.add_argument('-odd', '--origin-dump-dir',
211
                        help='Directory path for database dump file on origin system',
212
                        required=False,
213
                        type=str)
214
    parser.add_argument('-okd', '--origin-keep-dumps',
215
                        help='Keep dump file count for origin system',
216
                        required=False,
217
                        type=int)
218
    parser.add_argument('-odn', '--origin-db-name',
219
                        help='Database name for origin system',
220
                        required=False,
221
                        type=str)
222
    parser.add_argument('-odh', '--origin-db-host',
223
                        help='Database host for origin system',
224
                        required=False,
225
                        type=str)
226
    parser.add_argument('-odu', '--origin-db-user',
227
                        help='Database user for origin system',
228
                        required=False,
229
                        type=str)
230
    parser.add_argument('-odpw', '--origin-db-password',
231
                        help='Database password for origin system',
232
                        required=False,
233
                        type=str)
234
    parser.add_argument('-odpo', '--origin-db-port',
235
                        help='Database port for origin system',
236
                        required=False,
237
                        type=int)
238
    parser.add_argument('-fpw', '--force-password',
239
                        help='Force password user query',
240
                        required=False,
241
                        action='store_true')
242
    parser.add_argument('-ur', '--use-rsync',
243
                        help='Use rsync as transfer method',
244
                        required=False,
245
                        action='store_true')
246
    parser.add_argument('-uro', '--use-rsync-options',
247
                        help='Additional rsync options',
248
                        required=False,
249
                        type=str)
250
    parser.add_argument('-w', '--where',
251
                        help='Additional where clause for mysql dump to sync only selected rows',
252
                        required=False,
253
                        type=str)
254
    parser.add_argument('-amo', '--additional-mysqldump-options',
255
                        help='Additional mysqldump options for creating the database dump, e.g. --additional-mysqldump-options="--where="deleted=0"',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (149/100).

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

Loading history...
256
                        required=False,
257
                        type=str)
258
259
    return parser.parse_args(helper.dict_to_args(args))
260
261
262
if __name__ == "__main__":
263
    main()
264