db_sync_tool.recipes.typo3   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 52
dl 0
loc 111
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
B check_configuration() 0 52 7
A parse_database_credentials() 0 21 3
A get_database_setting_from_additional_configuration() 0 10 1
A get_database_setting_from_env() 0 10 1
1
#!/usr/bin/env python3
2
# -*- coding: future_fstrings -*-
3
4
"""
5
TYPO3 script
6
"""
7
8
import json, sys
0 ignored issues
show
introduced by
Multiple imports on one line (json, sys)
Loading history...
9
10
from db_sync_tool.utility import mode, system, helper, output
11
12
13
def check_configuration(client):
14
    """
15
    Checking remote TYPO3 database configuration
16
    :param client: String
17
    :return:
18
    """
19
    _path = system.config[client]['path']
20
21
    if 'LocalConfiguration' in _path:
22
        stdout = mode.run_command(
23
            helper.get_command(client, 'php') + ' -r "echo json_encode(include \'' +
24
            system.config[client][
25
                'path'] + '\');"',
26
            client,
27
            True
28
        )
29
30
        _db_config = parse_database_credentials(json.loads(stdout)['DB'])
31
    elif '.env' in _path:
32
        # Try to parse settings from .env file
33
        if 'db' not in system.config[client]:
34
            system.config[client]['db'] = {}
35
36
        _db_config = {
37
            'name': get_database_setting_from_env(client, system.config[client]['db'].get('name', 'TYPO3_CONF_VARS__DB__Connections__Default__dbname'), system.config[client]['path']),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (183/100).

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

Loading history...
38
            'host': get_database_setting_from_env(client, system.config[client]['db'].get('host', 'TYPO3_CONF_VARS__DB__Connections__Default__host'), system.config[client]['path']),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (181/100).

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

Loading history...
39
            'password': get_database_setting_from_env(client, system.config[client]['db'].get('password', 'TYPO3_CONF_VARS__DB__Connections__Default__password'), system.config[client]['path']),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (193/100).

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

Loading history...
40
            'port': get_database_setting_from_env(client, system.config[client]['db'].get('port', 'TYPO3_CONF_VARS__DB__Connections__Default__port'), system.config[client]['path'])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (180/100).

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

Loading history...
41
            if get_database_setting_from_env(client, system.config[client]['db'].get('port', 'TYPO3_CONF_VARS__DB__Connections__Default__port'), system.config[client]['path']) != '' else 3306,
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 8 spaces).
Loading history...
Coding Style introduced by
This line is too long as per the coding-style (192/100).

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

Loading history...
42
            'user': get_database_setting_from_env(client, system.config[client]['db'].get('user', 'TYPO3_CONF_VARS__DB__Connections__Default__user'), system.config[client]['path']),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (181/100).

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

Loading history...
43
        }
44
    elif 'AdditionalConfiguration.php' in _path:
45
        # Try to parse settings from AdditionalConfiguration.php file
46
        _db_config = {
47
            'name': get_database_setting_from_additional_configuration(client, 'dbname', system.config[client]['path']),
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...
48
            'host': get_database_setting_from_additional_configuration(client, 'host', system.config[client]['path']),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

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

Loading history...
49
            'password': get_database_setting_from_additional_configuration(client, 'password', system.config[client]['path']),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (126/100).

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

Loading history...
50
            'port': get_database_setting_from_additional_configuration(client, 'port', system.config[client]['path'])
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...
51
            if get_database_setting_from_additional_configuration(client, 'port',
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 8 spaces).
Loading history...
52
                                    system.config[client]['path']) != '' else 3306,
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 30 spaces).
Loading history...
53
            'user': get_database_setting_from_additional_configuration(client, 'user', system.config[client]['path']),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

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

Loading history...
54
        }
55
    else:
56
        sys.exit(
57
            output.message(
58
                output.Subject.ERROR,
59
                f'Can\'t extract database information from given path {system.config[client]["path"]}. Can only extract settings from the following files: LocalConfiguration.php, AdditionalConfiguration.php, .env',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (214/100).

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

Loading history...
60
                False
61
            )
62
        )
63
64
    system.config[client]['db'] = helper.clean_db_config(_db_config)
0 ignored issues
show
introduced by
The variable _db_config does not seem to be defined for all execution paths.
Loading history...
65
66
67
def parse_database_credentials(db_credentials):
68
    """
69
    Parsing database credentials to needed format
70
    :param db_credentials: Dictionary
71
    :return: Dictionary
72
    """
73
    #
74
    # Distinguish between database config scheme of TYPO3 v8+ and TYPO3 v7-
75
    #
76
    if 'Connections' in db_credentials:
77
        _db_config = db_credentials['Connections']['Default']
78
        _db_config['name'] = _db_config['dbname']
79
    else:
80
        _db_config = db_credentials
81
        _db_config['user'] = _db_config['username']
82
        _db_config['name'] = _db_config['database']
83
84
    if 'port' not in _db_config:
85
        _db_config['port'] = 3306
86
87
    return _db_config
88
89
90
def get_database_setting_from_additional_configuration(client, name, file):
91
    """
92
    Get database setting try to regex from AdditionalConfiguration
93
    sed -nE "s/'dbname'.*=>.*'(.*)'.*$/\1/p" /var/www/html/tests/files/www1/AdditionalConfiguration.php
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...
94
    :param client: String
95
    :param name: String
96
    :param file: String
97
    :return:
98
    """
99
    return helper.run_sed_command(client, f'"s/\'{name}\'.*=>.*\'(.*)\'.*$/\\1/p" {file}')
100
101
def get_database_setting_from_env(client, name, file):
102
    """
103
    Get database setting try to regex from .env
104
    sed -nE "s/TYPO3_CONF_VARS__DB__Connections__Default__host=(.*).*$/\1/p" /var/www/html/tests/files/www1/typo3.env
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...
105
    :param client: String
106
    :param name: String
107
    :param file: String
108
    :return:
109
    """
110
    return helper.run_sed_command(client, f'"s/{name}=(.*).*$/\\1/p" {file}')
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...