<s> """<STR_LIT>""" <EOL> import io <EOL> import logging <EOL> import os <EOL> import subprocess <EOL> import sys <EOL> import tempfile <EOL> from jinja2 import Template <EOL> from cookiecutter import utils <EOL> _HOOKS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def find_hooks ( ) : <EOL> """<STR_LIT>""" <EOL> hooks_dir = '<STR_LIT>' <EOL> r = { } <EOL> logging . debug ( '<STR_LIT>' . format ( hooks_dir ) ) <EOL> if not os . path . isdir ( hooks_dir ) : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return r <EOL> for f in os . listdir ( hooks_dir ) : <EOL> basename = os . path . splitext ( os . path . basename ( f ) ) [ <NUM_LIT:0> ] <EOL> if basename in _HOOKS : <EOL> r [ basename ] = os . path . abspath ( os . path . join ( hooks_dir , f ) ) <EOL> return r <EOL> def run_script ( script_path , cwd = '<STR_LIT:.>' ) : <EOL> """<STR_LIT>""" <EOL> run_thru_shell = sys . platform . startswith ( '<STR_LIT>' ) <EOL> if script_path . endswith ( '<STR_LIT>' ) : <EOL> script_command = [ sys . executable , script_path ] <EOL> else : <EOL> script_command = [ script_path ] <EOL> utils . make_executable ( script_path ) <EOL> proc = subprocess . Popen ( <EOL> script_command , <EOL> shell = run_thru_shell , <EOL> cwd = cwd <EOL> ) <EOL> proc . wait ( ) <EOL> def run_script_with_context ( script_path , cwd , context ) : <EOL> """<STR_LIT>""" <EOL> _ , extension = os . path . splitext ( script_path ) <EOL> contents = io . open ( script_path , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) . read ( ) <EOL> with tempfile . NamedTemporaryFile ( <EOL> delete = False , <EOL> mode = '<STR_LIT:w>' , <EOL> suffix = extension <EOL> ) as temp : <EOL> temp . write ( Template ( contents ) . render ( ** context ) ) <EOL> run_script ( temp . name , cwd ) <EOL> def run_hook ( hook_name , project_dir , context ) : <EOL> """<STR_LIT>""" <EOL> script = find_hooks ( ) . get ( hook_name ) <EOL> if script is None : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return <EOL> return run_script_with_context ( script , project_dir , context ) </s>
