
    E^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 ddl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 dS )zCFunctions for discovering and executing various cookiecutter hooks.    N)Path)UndefinedError)utils)StrictEnvironment)FailedHookException)create_tmp_repo_dirrmtreework_in)
pre_promptpre_gen_projectpost_gen_projectc                     t           j                            |           }t           j                            |          d         }||k    }|t          v }|                    d          }|o|o| 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.11/site-packages/cookiecutter/hooks.py
valid_hookr      sl     w	**Hw))!,H	)M'N##C((K?^?K?    hooksc           	         t                               dt          j                            |                     t          j                            |          st                               d           dS g }t          j        |          D ]b}t          ||           rP|                    t          j                            t          j        	                    ||                               ct          |          dk    rdS |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+   +   s     LL"BGOOI$>$>???7==## 3444tGZ	** P P	i++ 	PNN27??27<<	9+M+MNNOOO
7||qtNr   .c                    t           j                            d          }|                     d          rt           j        | g}n| g}t          j        |            	 t          j        |||          }|	                                }|t          k    rt          d| d          dS # 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_pathr0   run_thru_shellscript_commandprocexit_statuserrs          r   
run_scriptrD   G   s    \,,U33NE"" '.+6%	+&&&Qn#NNNiikk,&&%BKBBB   '&  Q Q Q9%%%Q  ""F"F"F"FGGSPQs   A	B' '
C/19C**C/c                    t           j                            |           \  }}t          | d          5 }|                                }ddd           n# 1 swxY w Y   t          j        dd|          5 }t          |d          }|                    |          }	 |	j	        d	i |}
|
                    |
                    d                     ddd           n# 1 swxY w Y   t          |j        |           dS )
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suffixT)contextkeep_trailing_newline )r   r   r   openreadtempfileNamedTemporaryFiler   from_stringrenderwriteencoderD   name)r>   r0   rK   _	extensionfilecontentstempenvtemplateoutputs              r   run_script_with_contextr_   d   sn    7##K00LAy	kG	,	,	, 99;;               
	$EY	O	O	O +SWtLLL??8,, ++7++

6==))***	+ + + + + + + + + + + + + + + ty#s$   AAA7ACC#&C#c                     t          |           }|st                              d|            dS t                              d|            |D ]}t          |||           dS )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_dirrK   r*   scripts        r   run_hookrc   y   sv     	""G '333
LL"I... > >W====> >r   c                    t          |           5  	 t          |||           nB# t          t          f$ r. |rt	          |           t
                              d|            w xY w	 ddd           dS # 1 swxY w Y   dS )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
   rc   r   r   r	   r!   error)repo_dirr   ra   rK   delete_project_on_failures        r   run_hook_from_repo_dirrh      s     
		  	YW5555
 	 	 	 ) ${###LL2  
 	 6                 s%   A4$A4?A##A44A8;A8rf   zos.PathLike[str]returnc                    t          |           5  t          d          }|s| cddd           S 	 ddd           n# 1 swxY w Y   t          |           } t          |           5  t          d          }|D ]0}	 t          ||            # t          $ r t	          d          w xY w	 ddd           n# 1 swxY w Y   | S )zeRun pre_prompt hook from repo directory.

    :param repo_dir: Project template input directory.
    r   NzPre-Prompt Hook script failed)r
   r+   r   rD   r   )rf   r*   rb   s      r   run_pre_prompt_hookrk      s    
		  L)) 	       	               #8,,H			 K KL)) 	K 	KFK68,,,,& K K K)*IJJJK	KK K K K K K K K K K K K K K K Os9   =AA&B6;BB6B&&B66B:=B:)r   )r,   )!__doc__r<   loggingr   r7   r2   rP   pathlibr   jinja2.exceptionsr   cookiecutterr   cookiecutter.environmentr   cookiecutter.exceptionsr   cookiecutter.utilsr   r	   r
   	getLogger__name__r!   r   r:   r   r+   rD   r_   rc   rh   rk   rM   r   r   <module>rv      s~   I I   				     



        , , , , , ,       6 6 6 6 6 6 7 7 7 7 7 7 C C C C C C C C C C		8	$	$
 
 

 @ @ @    8Q Q Q Q:  *> > >"  :"4       r   