Passed
Branch v4.0-dev (e005f1)
by Emmanuel
05:49
created

stakkr.setup   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 110
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0
wmc 18

4 Functions

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 1
A _create_dir() 0 7 4
A _copy_file() 0 13 4
A _post_install() 0 35 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A StakkrPostInstall.__init__() 0 11 2
1
# coding: utf-8
2
"""Setup post actions, used in main setup.py."""
3
4
import os
5
import shutil
6
import sys
7
from setuptools.command.install import install
8
from stakkr import file_utils
9
10
11
try:
12
    import click
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
13
14
    @click.command(help="""Initialize for the first time stakkr by copying
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
15
templates and directory structure""")
16
    @click.option('--force', '-f', help="Force recreate directories structure", is_flag=True)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (93/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
17
    def init(force: bool):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
18
        """CLI Entry point, when initializing stakkr manually."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
19
        config_file = os.getcwd() + '/stakkr.yml'
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
20
        if os.path.isfile(config_file) and force is False:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
21
            click.secho('Config file (stakkr.yml) already present. Leaving.', fg='yellow')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (90/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
22
            return
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
23
24
        msg = "Config (stakkr.yml) not present, don't forget to create it"
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
25
        click.secho(msg, fg='yellow')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
26
        _post_install(force)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
27
28
except ImportError:
29
    def init():
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
30
        """If click is not installed, display that message."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
31
        print('Stakkr has not been installed yet')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
32
        sys.exit(1)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
33
34
35
def _post_install(force: bool = False):
36
    """Create templates (directories and files)."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
37
    print('Post Installation : create templates')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
38
39
    project_dir = os.getcwd()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
40
    # If already installed don't do anything
41
    if os.path.isfile(project_dir + '/stakkr.yml'):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
42
        return
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
43
44
    required_dirs = [
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
45
        'conf/mysql-override',
46
        'conf/php-fpm-override',
47
        'conf/xhgui-override',
48
        'data',
49
        'home/www-data',
50
        'home/www-data/bin',
51
        'logs',
52
        'plugins',
53
        'services',
54
        'www'
55
    ]
56
    for required_dir in required_dirs:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
57
        _create_dir(project_dir, required_dir, force)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
58
59
    required_tpls = [
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
60
        # 'bash_completion', # How to do with a system wide installation ?
61
        'stakkr.yml.tpl',
62
        'conf/mysql-override/mysqld.cnf',
63
        'conf/php-fpm-override/example.conf',
64
        'conf/php-fpm-override/README',
65
        'conf/xhgui-override/config.php',
66
        'home/www-data/.bashrc'
67
    ]
68
    for required_tpl in required_tpls:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
69
        _copy_file(project_dir, required_tpl, force)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
70
71
72
def _create_dir(project_dir: str, dir_name: str, force: bool):
73
    dir_name = project_dir + '/' + dir_name.lstrip('/')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
74
    if os.path.isdir(dir_name) and force is False:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
75
        return
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
76
77
    if not os.path.isdir(dir_name):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
78
        os.makedirs(dir_name)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
79
80
81
def _copy_file(project_dir: str, source_file: str, force: bool):
82
    full_path = file_utils.get_file('tpls', source_file)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
83
    dest_file = project_dir + '/' + source_file
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
84
    if os.path.isfile(dest_file) and force is False:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
85
        print('  - {} exists, do not overwrite'.format(source_file))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
86
        return
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
87
88
    print('  - {} written'.format(source_file))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
89
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
90
        shutil.copy(full_path, dest_file)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
91
    except Exception:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
92
        msg = "Error trying to copy {} .. check that the file is there ...".format(full_path)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (93/80).

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

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
93
        print(msg, file=sys.stderr)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
94
95
96
class StakkrPostInstall(install):
97
    """Class called by the main setup.py."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
98
99
    def __init__(self, *args, **kwargs):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
100
        """Inherit from setup install class and ensure we are in a venv."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
101
        super(StakkrPostInstall, self).__init__(*args, **kwargs)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
102
103
        try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
104
            file_utils.find_project_dir()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
105
            _post_install(False)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
106
        except OSError:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
107
            msg = 'You must run setup.py from a virtualenv if you want to have'
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
108
            msg += ' templates installed'
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
109
            print(msg)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
110