cookiecutter.hooks   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 76
dl 0
loc 146
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A valid_hook() 0 15 1
A find_hook() 0 23 4
B run_script() 0 33 5
A run_script_with_context() 0 26 3
A run_hook() 0 14 2
1
# -*- coding: utf-8 -*-
2
3
"""Functions for discovering and executing various cookiecutter hooks."""
4
5
import errno
6
import io
7
import logging
8
import os
9
import subprocess
10
import sys
11
import tempfile
12
13
from cookiecutter import utils
14
from cookiecutter.environment import StrictEnvironment
15
from cookiecutter.exceptions import FailedHookException
16
17
logger = logging.getLogger(__name__)
18
19
_HOOKS = [
20
    'pre_gen_project',
21
    'post_gen_project',
22
]
23
EXIT_SUCCESS = 0
24
25
26
def valid_hook(hook_file, hook_name):
27
    """Determine if a hook file is valid.
28
29
    :param hook_file: The hook file to consider for validity
30
    :param hook_name: The hook to find
31
    :return: The hook file validity
32
    """
33
    filename = os.path.basename(hook_file)
34
    basename = os.path.splitext(filename)[0]
35
36
    matching_hook = basename == hook_name
37
    supported_hook = basename in _HOOKS
38
    backup_file = filename.endswith('~')
39
40
    return matching_hook and supported_hook and not backup_file
41
42
43
def find_hook(hook_name, hooks_dir='hooks'):
44
    """Return a dict of all hook scripts provided.
45
46
    Must be called with the project template as the current working directory.
47
    Dict's key will be the hook/script's name, without extension, while values
48
    will be the absolute path to the script. Missing scripts will not be
49
    included in the returned dict.
50
51
    :param hook_name: The hook to find
52
    :param hooks_dir: The hook directory in the template
53
    :return: The absolute path to the hook script or None
54
    """
55
    logger.debug('hooks_dir is {}'.format(os.path.abspath(hooks_dir)))
56
57
    if not os.path.isdir(hooks_dir):
58
        logger.debug('No hooks/ dir in template_dir')
59
        return None
60
61
    for hook_file in os.listdir(hooks_dir):
62
        if valid_hook(hook_file, hook_name):
63
            return os.path.abspath(os.path.join(hooks_dir, hook_file))
64
65
    return None
66
67
68
def run_script(script_path, cwd='.'):
69
    """Execute a script from a working directory.
70
71
    :param script_path: Absolute path to the script to run.
72
    :param cwd: The directory to run the script from.
73
    """
74
    run_thru_shell = sys.platform.startswith('win')
75
    if script_path.endswith('.py'):
76
        script_command = [sys.executable, script_path]
77
    else:
78
        script_command = [script_path]
79
80
    utils.make_executable(script_path)
81
82
    try:
83
        proc = subprocess.Popen(
84
            script_command,
85
            shell=run_thru_shell,
86
            cwd=cwd
87
        )
88
        exit_status = proc.wait()
89
        if exit_status != EXIT_SUCCESS:
90
            raise FailedHookException(
91
                'Hook script failed (exit status: {})'.format(exit_status)
92
            )
93
    except OSError as os_error:
94
        if os_error.errno == errno.ENOEXEC:
95
            raise FailedHookException(
96
                'Hook script failed, might be an '
97
                'empty file or missing a shebang'
98
            )
99
        raise FailedHookException(
100
            'Hook script failed (error: {})'.format(os_error)
101
        )
102
103
104
def run_script_with_context(script_path, cwd, context):
105
    """Execute a script after rendering it with Jinja.
106
107
    :param script_path: Absolute path to the script to run.
108
    :param cwd: The directory to run the script from.
109
    :param context: Cookiecutter project template context.
110
    """
111
    _, extension = os.path.splitext(script_path)
112
113
    with io.open(script_path, 'r', encoding='utf-8') as file:
114
        contents = file.read()
115
116
    with tempfile.NamedTemporaryFile(
117
        delete=False,
118
        mode='wb',
119
        suffix=extension
120
    ) as temp:
121
        env = StrictEnvironment(
122
            context=context,
123
            keep_trailing_newline=True,
124
        )
125
        template = env.from_string(contents)
126
        output = template.render(**context)
127
        temp.write(output.encode('utf-8'))
128
129
    run_script(temp.name, cwd)
130
131
132
def run_hook(hook_name, project_dir, context):
133
    """
134
    Try to find and execute a hook from the specified project directory.
135
136
    :param hook_name: The hook to execute.
137
    :param project_dir: The directory to execute the script from.
138
    :param context: Cookiecutter project context.
139
    """
140
    script = find_hook(hook_name)
141
    if script is None:
142
        logger.debug('No {} hook found'.format(hook_name))
143
        return
144
    logger.debug('Running hook {}'.format(hook_name))
145
    run_script_with_context(script, project_dir, context)
146