/usr/lib/python2.7/dist-packages/cookiecutter/hooks.py is in python-cookiecutter 1.3.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
cookiecutter.hooks
------------------
Functions for discovering and executing various cookiecutter hooks.
"""
import io
import logging
import os
import subprocess
import sys
import tempfile
from jinja2 import Template
from cookiecutter import utils
from .exceptions import FailedHookException
_HOOKS = [
'pre_gen_project',
'post_gen_project',
# TODO: other hooks should be listed here
]
EXIT_SUCCESS = 0
def find_hooks():
"""
Must be called with the project template as the current working directory.
Returns a dict of all hook scripts provided.
Dict's key will be the hook/script's name, without extension, while
values will be the absolute path to the script.
Missing scripts will not be included in the returned dict.
"""
hooks_dir = 'hooks'
r = {}
logging.debug('hooks_dir is {0}'.format(hooks_dir))
if not os.path.isdir(hooks_dir):
logging.debug('No hooks/ dir in template_dir')
return r
for f in os.listdir(hooks_dir):
basename = os.path.splitext(os.path.basename(f))[0]
if basename in _HOOKS:
r[basename] = os.path.abspath(os.path.join(hooks_dir, f))
return r
def run_script(script_path, cwd='.'):
"""
Executes a script from a working directory.
:param script_path: Absolute path to the script to run.
:param cwd: The directory to run the script from.
"""
run_thru_shell = sys.platform.startswith('win')
if script_path.endswith('.py'):
script_command = [sys.executable, script_path]
else:
script_command = [script_path]
utils.make_executable(script_path)
proc = subprocess.Popen(
script_command,
shell=run_thru_shell,
cwd=cwd
)
exit_status = proc.wait()
if exit_status != EXIT_SUCCESS:
raise FailedHookException(
"Hook script failed (exit status: %d)" % exit_status)
def run_script_with_context(script_path, cwd, context):
"""
Executes a script after rendering with it Jinja.
:param script_path: Absolute path to the script to run.
:param cwd: The directory to run the script from.
:param context: Cookiecutter project template context.
"""
_, extension = os.path.splitext(script_path)
contents = io.open(script_path, 'r', encoding='utf-8').read()
with tempfile.NamedTemporaryFile(
delete=False,
mode='wb',
suffix=extension
) as temp:
output = Template(contents).render(**context)
temp.write(output.encode('utf-8'))
run_script(temp.name, cwd)
def run_hook(hook_name, project_dir, context):
"""
Try to find and execute a hook from the specified project directory.
:param hook_name: The hook to execute.
:param project_dir: The directory to execute the script from.
:param context: Cookiecutter project context.
"""
script = find_hooks().get(hook_name)
if script is None:
logging.debug('No hooks found')
return
run_script_with_context(script, project_dir, context)
|