Passed
Push — master ( 032958...e7129a )
by Konrad
09:54
created

db_sync_tool.recipes.typo3.get_database_setting()   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 1
nop 3
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
        _db_config = {
34
            'name': get_database_setting_from_env(client, '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 (142/100).

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

Loading history...
35
            'host': get_database_setting_from_env(client, '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 (140/100).

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

Loading history...
36
            'password': get_database_setting_from_env(client, '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 (148/100).

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

Loading history...
37
            'port': get_database_setting_from_env(client, '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 (139/100).

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

Loading history...
38
            if get_database_setting_from_env(client, 'TYPO3_CONF_VARS__DB__Connections__Default__port',
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 (103/100).

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

Loading history...
39
                                    system.config[client]['path']) != '' else 3306,
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 9 spaces).
Loading history...
40
            'user': get_database_setting_from_env(client, '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 (140/100).

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

Loading history...
41
        }
42
    elif 'AdditionalConfiguration.php' in _path:
43
        # Try to parse settings from AdditionalConfiguration.php file
44
        _db_config = {
45
            '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...
46
            '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...
47
            '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...
48
            '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...
49
            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...
50
                                    system.config[client]['path']) != '' else 3306,
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 30 spaces).
Loading history...
51
            '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...
52
        }
53
    else:
54
        sys.exit(
55
            output.message(
56
                output.Subject.ERROR,
57
                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...
58
                False
59
            )
60
        )
61
62
    system.config[client]['db'] = _db_config
0 ignored issues
show
introduced by
The variable _db_config does not seem to be defined for all execution paths.
Loading history...
63
64
65
def parse_database_credentials(db_credentials):
66
    """
67
    Parsing database credentials to needed format
68
    :param db_credentials: Dictionary
69
    :return: Dictionary
70
    """
71
    #
72
    # Distinguish between database config scheme of TYPO3 v8+ and TYPO3 v7-
73
    #
74
    if 'Connections' in db_credentials:
75
        _db_config = db_credentials['Connections']['Default']
76
        _db_config['name'] = _db_config['dbname']
77
    else:
78
        _db_config = db_credentials
79
        _db_config['user'] = _db_config['username']
80
        _db_config['name'] = _db_config['database']
81
82
    if 'port' not in _db_config:
83
        _db_config['port'] = 3306
84
85
    return _db_config
86
87
88
def get_database_setting_from_additional_configuration(client, name, file):
89
    """
90
    Get database setting try to regex from AdditionalConfiguration
91
    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...
92
    :param client: String
93
    :param name: String
94
    :param file: String
95
    :return:
96
    """
97
    return mode.run_command(
98
        helper.get_command(client, 'sed') +
99
        f' -nE "s/\'{name}\'.*=>.*\'(.*)\'.*$/\\1/p" {file}',
100
        client,
101
        True
102
    ).replace('\n', '').strip()
103
104
def get_database_setting_from_env(client, name, file):
105
    """
106
    Get database setting try to regex from .env
107
    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...
108
    :param client: String
109
    :param name: String
110
    :param file: String
111
    :return:
112
    """
113
    return mode.run_command(
114
        helper.get_command(client, 'sed') +
115
        f' -nE "s/{name}=(.*).*$/\\1/p" {file}',
116
        client,
117
        True
118
    ).replace('\n', '').strip()
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...