Passed
Push — master ( b9e3b5...041dd8 )
by Emmanuel
05:58
created

setup._post_install()   A

Complexity

Conditions 4

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 29
nop 1
dl 0
loc 36
rs 9.184
c 0
b 0
f 0
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
13
14
    @click.command(help="""Initialize for the first time stakkr by copying
15
templates and directory structure""")
16
    @click.option('--force', '-f', help="Force recreate directories structure", is_flag=True)
17
    def init(force: bool):
18
        """CLI Entry point, when initializing stakkr manually."""
19
        config_file = os.getcwd() + '/stakkr.yml'
20
        if os.path.isfile(config_file) and force is False:
21
            click.secho('Config file (stakkr.yml) already present. Leaving.', fg='yellow')
22
            return
23
24
        msg = "Config (stakkr.yml) not present, don't forget to create it"
25
        click.secho(msg, fg='yellow')
26
        _post_install(force)
27
28
except ImportError:
29
    def init():
30
        """If click is not installed, display that message."""
31
        print('Stakkr has not been installed yet')
32
        sys.exit(1)
33
34
35
def _post_install(force: bool = False):
36
    """Create templates (directories and files)."""
37
    print('Post Installation : create templates')
38
39
    project_dir = os.getcwd()
40
    # If already installed don't do anything
41
    if os.path.isfile(project_dir + '/stakkr.yml'):
42
        return
43
44
    required_dirs = [
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:
57
        _create_dir(project_dir, required_dir, force)
58
59
    required_tpls = [
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:
69
        _copy_file(project_dir, required_tpl, force)
70
71
72
def _create_dir(project_dir: str, dir_name: str, force: bool):
73
    dir_name = project_dir + '/' + dir_name.lstrip('/')
74
    if os.path.isdir(dir_name) and force is False:
75
        return
76
77
    if not os.path.isdir(dir_name):
78
        os.makedirs(dir_name)
79
80
81
def _copy_file(project_dir: str, source_file: str, force: bool):
82
    full_path = file_utils.get_file('tpls', source_file)
83
    dest_file = project_dir + '/' + source_file
84
    if os.path.isfile(dest_file) and force is False:
85
        print('  - {} exists, do not overwrite'.format(source_file))
86
        return
87
88
    print('  - {} written'.format(source_file))
89
    try:
90
        shutil.copy(full_path, dest_file)
91
    except Exception:
92
        msg = "Error trying to copy {} .. check that the file is there ...".format(full_path)
93
        print(msg, file=sys.stderr)
94
95
96
class StakkrPostInstall(install):
97
    """Class called by the main setup.py."""
98
99
    def __init__(self, *args, **kwargs):
100
        """Inherit from setup install class and ensure we are in a venv."""
101
        super(StakkrPostInstall, self).__init__(*args, **kwargs)
102
103
        try:
104
            file_utils.find_project_dir()
105
            _post_install(False)
106
        except OSError:
107
            msg = 'You must run setup.py from a virtualenv if you want to have'
108
            msg += ' templates installed'
109
            print(msg)
110