Passed
Pull Request — master (#8)
by
unknown
04:47
created

_post_install()   A

Complexity

Conditions 4

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 4
c 4
b 0
f 0
dl 0
loc 35
rs 9.0399
1
"""
2
Setup post actions, used in the main setup.py
3
"""
4
5
import os
6
import shutil
7
import sys
8
from setuptools.command.install import install
9
from stakkr import package_utils
10
11
12
try:
13
    import click
14
15
    @click.command(help="""Initialize for the first time stakkr by copying
16
templates and directory structure""")
17
    @click.option('--force', '-f', help="Force recreate directories structure",
18
                  is_flag=True)
19
    def init(force: bool):
20
        """CLI Entry point, when initializing stakkr manually"""
21
22
        config_file = package_utils.get_venv_basedir() + '/conf/compose.ini'
23
        if os.path.isfile(config_file) and force is False:
24
            click.secho('Config file (conf/compose.ini) already present. Leaving.', fg='yellow')
25
            return
26
27
        msg = 'Config file (conf/compose.ini) not present, do not forget to create it'
28
        click.secho(msg, fg='yellow')
29
        _post_install(force)
30
except ImportError:
31
    def init():
32
        """If click is not installed, display that message"""
33
34
        print('Stakkr has not been installed yet')
35
        sys.exit(1)
36
37
38
def _post_install(force: bool = False):
39
    print('Post Installation : create templates')
40
41
    venv_dir = package_utils.get_venv_basedir()
42
    # If already installed don't do anything
43
    if os.path.isfile(venv_dir + '/conf/compose.ini'):
44
        return
45
46
    required_dirs = [
47
        'conf/mysql-override',
48
        'conf/php-fpm-override',
49
        'conf/xhgui-override',
50
        'data',
51
        'home/www-data',
52
        'home/www-data/bin',
53
        'logs',
54
        'plugins',
55
        'services',
56
        'www'
57
    ]
58
    for required_dir in required_dirs:
59
        _create_dir(venv_dir, required_dir, force)
60
61
    required_tpls = [
62
        '.env',
63
        'bash_completion',
64
        'conf/compose.ini.tpl',
65
        'conf/mysql-override/mysqld.cnf',
66
        'conf/php-fpm-override/example.conf',
67
        'conf/php-fpm-override/README',
68
        'conf/xhgui-override/config.php',
69
        'home/www-data/.bashrc'
70
    ]
71
    for required_tpl in required_tpls:
72
        _copy_file(venv_dir, required_tpl, force)
73
74
75
def _create_dir(venv_dir: str, dir_name: str, force: bool):
76
    dir_name = venv_dir + '/' + dir_name.lstrip('/')
77
    if os.path.isdir(dir_name) and force is False:
78
        return
79
80
    if not os.path.isdir(dir_name):
81
        os.makedirs(dir_name)
82
83
84
def _copy_file(venv_dir: str, source_file: str, force: bool):
85
    full_path = package_utils.get_file('tpls', source_file)
86
    dest_file = venv_dir + '/' + source_file
87
    if os.path.isfile(dest_file) and force is False:
88
        print('  - {} exists, do not overwrite'.format(source_file))
89
        return
90
91
    print('  - {} written'.format(source_file))
92
    try:
93
        shutil.copy(full_path, dest_file)
94
    except Exception:
95
        msg = "Error trying to copy {} .. check that the file is there ...".format(full_path)
96
        print(msg, file=sys.stderr)
97
98
99
class StakkrPostInstall(install):
100
    """Class called by the main setup.py"""
101
102
    def __init__(self, *args, **kwargs):
103
        super(StakkrPostInstall, self).__init__(*args, **kwargs)
104
105
        try:
106
            package_utils.get_venv_basedir()
107
            _post_install(False)
108
        except OSError:
109
            msg = 'You must run setup.py from a virtualenv if you want to have '
110
            msg += 'the templates installed'
111
            print(msg)
112
113