Passed
Push — master ( 7fdd2b...425778 )
by Konrad
10:23
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
9
10
from db_sync_tool.utility import mode, system, helper
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
    else:
32
        # Try to parse settings from AdditionalConfiguration.php file
33
        _db_config = {
34
            'name': get_database_setting(client, 'dbname', system.config[client]['path']),
35
            'host': get_database_setting(client, 'host', system.config[client]['path']),
36
            'password': get_database_setting(client, 'password', system.config[client]['path']),
37
            'port': get_database_setting(client, 'port', system.config[client]['path'])
38
            if get_database_setting(client, 'port',
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 8 spaces).
Loading history...
39
                                    system.config[client]['path']) != '' else 3306,
40
            'user': get_database_setting(client, 'user', system.config[client]['path']),
41
        }
42
43
    system.config[client]['db'] = _db_config
44
45
46
def parse_database_credentials(db_credentials):
47
    """
48
    Parsing database credentials to needed format
49
    :param db_credentials: Dictionary
50
    :return: Dictionary
51
    """
52
    #
53
    # Distinguish between database config scheme of TYPO3 v8+ and TYPO3 v7-
54
    #
55
    if 'Connections' in db_credentials:
56
        _db_config = db_credentials['Connections']['Default']
57
        _db_config['name'] = _db_config['dbname']
58
    else:
59
        _db_config = db_credentials
60
        _db_config['user'] = _db_config['username']
61
        _db_config['name'] = _db_config['database']
62
63
    if 'port' not in _db_config:
64
        _db_config['port'] = 3306
65
66
    return _db_config
67
68
69
def get_database_setting(client, name, file):
70
    """
71
    Get database setting try to regex from AdditionalConfiguration
72
    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...
73
    :param client: String
74
    :param name: String
75
    :param file: String
76
    :return:
77
    """
78
    return mode.run_command(
79
        helper.get_command(client, 'sed') +
80
        f' -nE "s/\'{name}\'.*=>.*\'(.*)\'.*$/\\1/p" {file}',
81
        client,
82
        True
83
    ).replace('\n', '').strip()
84