
    e                         d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZmZmZ  ej(                  e      Zg dZdZd	 Zdd
ZddZd Zd Zd ZdddefdZy)zCFunctions for discovering and executing various cookiecutter hooks.    N)Path)UndefinedError)utils)FailedHookException)create_env_with_contextcreate_tmp_repo_dirrmtreework_in)
pre_promptpre_gen_projectpost_gen_projectc                     t         j                  j                  |       }t         j                  j                  |      d   }||k(  }|t        v }|j                  d      }|xr |xr | S )zDetermine if a hook file is valid.

    :param hook_file: The hook file to consider for validity
    :param hook_name: The hook to find
    :return: The hook file validity
    r   ~)ospathbasenamesplitext_HOOKSendswith)	hook_file	hook_namefilenamer   matching_hooksupported_hookbackup_files          2lib/python3.12/site-packages/cookiecutter/hooks.py
valid_hookr       sg     ww	*Hww)!,H	)M'N##C(K?^?K?    c           	         t         j                  dt        j                  j	                  |             t        j                  j                  |      st         j                  d       yg }t        j                  |      D ][  }t        ||       s|j                  t        j                  j	                  t        j                  j                  ||                   ] t        |      dk(  ry|S )a  Return a dict of all hook scripts provided.

    Must be called with the project template as the current working directory.
    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.

    :param hook_name: The hook to find
    :param hooks_dir: The hook directory in the template
    :return: The absolute path to the hook script or None
    zhooks_dir is %szNo hooks/dir in template_dirNr   )loggerdebugr   r   abspathisdirlistdirr   appendjoinlen)r   	hooks_dirscriptsr   s       r   	find_hookr*   0   s     LL"BGGOOI$>?77==#34GZZ	* P	i+NN277??277<<	9+MNOP 7|qNr   c                    t         j                  j                  d      }| j                  d      rt         j                  | g}n| g}t        j                  |        	 t        j                  |||      }|j                         }|t        k7  rt        d| d      y# t        $ r>}|j                  t        j                  k(  rt        d      |t        d| d      |d}~ww xY w)	zExecute 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.
    winz.py)shellcwdz!Hook script failed (exit status: )z?Hook script failed, might be an empty file or missing a shebangzHook script failed (error: N)sysplatform
startswithr   
executabler   make_executable
subprocessPopenwaitEXIT_SUCCESSr   OSErrorerrnoENOEXEC)script_pathr.   run_thru_shellscript_commandprocexit_statuserrs          r   
run_scriptrB   L   s     \\,,U3NE"..+6%	+&Qn#Niik,&%3K=B  '  Q99%%Q "$?uA"FGSPQs   A B 	C%'9C  C%c                    t         j                  j                  |       \  }}t        | d      5 }|j	                         }ddd       t        j                  dd|      5 }t        |      }|j                        }	 |	j                  di |}
|j                  |
j                  d             ddd       t        j                  |       y# 1 sw Y   xY w# 1 sw Y   ,xY w)zExecute a script after rendering it with 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.
    zutf-8)encodingNFwb)deletemodesuffix )r   r   r   openreadtempfileNamedTemporaryFiler   from_stringrenderwriteencoderB   name)r<   r.   context_	extensionfilecontentstempenvtemplateoutputs              r   run_script_with_contextr\   i   s     77##K0LAy	kG	, 99; 
	$	$EY	O +SW%g.??8, +7+

6==)*	+ tyy# + +s   C!ACCC$c                     t        |       }|st        j                  d|        yt        j                  d|        |D ]  }t        |||        y)z
    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.
    zNo %s hook foundNzRunning hook %s)r*   r    r!   r\   )r   project_dirrS   r)   scripts        r   run_hookr`   ~   sM     	"G'3
LL"I. >W=>r   c                     t        |       5  	 t        |||       	 ddd       y# t        t        f$ r% |rt	        |       t
        j                  d|        w xY w# 1 sw Y   yxY w)a|  Run hook from repo directory, clean project directory if hook fails.

    :param repo_dir: Project template input directory.
    :param hook_name: The hook to execute.
    :param project_dir: The directory to execute the script from.
    :param context: Cookiecutter project context.
    :param delete_project_on_failure: Delete the project directory on hook
        failure?
    zCStopping generation because %s hook script didn't exit successfullyN)r
   r`   r   r   r	   r    error)repo_dirr   r^   rS   delete_project_on_failures        r   run_hook_from_repo_dirre      sv     
	 	YW5   
 	 ){#LL2
 	 s   A%4AAA%rc   zos.PathLike[str]returnc                 @   t        |       5  t        d      }|s| cddd       S 	 ddd       t        |       } t        |       5  t        d      }|D ]  }	 t        ||         	 ddd       | S # 1 sw Y   JxY w# t        $ r t	        d      w xY w# 1 sw Y   | S xY w)zeRun pre_prompt hook from repo directory.

    :param repo_dir: Project template input directory.
    r   NzPre-Prompt Hook script failed)r
   r*   r   rB   r   )rc   r)   r_   s      r   run_pre_prompt_hookrh      s     
	 L)  #8,H		 KL) 	KFK68,	KK O  ' K)*IJJKK Os/   A/BA;"B/A8;BBB)hooks).) __doc__r:   loggingr   r5   r0   rL   pathlibr   jinja2.exceptionsr   cookiecutterr   cookiecutter.exceptionsr   cookiecutter.utilsr   r   r	   r
   	getLogger__name__r    r   r8   r   r*   rB   r\   r`   re   rh   rI   r   r   <module>rt      s    I   	  
   ,  7  
		8	$

 @ 8Q:*>":"4  r   