
    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Zddlm	Z	 ddl
mZ ddlmZ  ej        e          Zd Zd ZddZej        dd            Zd ZddZd Zdd	d
e	fdZdS )z.Helper functions used throughout Cookiecutter.    N)Path)	Extension)read_user_yes_noc                 Z    t          j        |t          j                    | |           dS )zError handler for `shutil.rmtree()` equivalent to `rm -rf`.

    Usage: `shutil.rmtree(path, onerror=force_delete)`
    From https://docs.python.org/3/library/shutil.html#rmtree-example
    N)oschmodstatS_IWRITE)funcpathexc_infos      2lib/python3.11/site-packages/cookiecutter/utils.pyforce_deleter      s)     HT4=!!!DJJJJJ    c                 <    t          j        | t                     dS )zfRemove a directory and all its contents. Like rm -rf on Unix.

    :param path: A directory path.
    )onerrorN)shutilrmtreer   )r   s    r   r   r      s    
 M$------r   r   os.PathLike[str]returnc                     t                               d|            	 t          |                               dd           dS # t          $ r}t	          d|            |d}~ww xY w)zZEnsure that a directory exists.

    :param path: A directory tree path for creation.
    z7Making sure path exists (creates tree if not exist): %sT)parentsexist_okzUnable to create directory at N)loggerdebugr   mkdirOSError)r   errors     r   make_sure_path_existsr   $   s    
 LLJDQQQJT

55555 J J J=t==>>EIJs   $A 
A%A  A%c              #      K   t          j                    }	 | t          j        |            dV  t          j        |           dS # t          j        |           w xY w)zoContext manager version of os.chdir.

    When exited, returns to the working directory prior to entering.
    N)r   getcwdchdir)dirnamecurdirs     r   work_inr%   0   sc       Y[[FHW
s   A Ac                 |    t          j        |           }t          j        | |j        t          j        z             dS )zOMake `script_path` executable.

    :param script_path: The file to change
    N)r   r	   r   st_modeS_IEXEC)script_pathstatuss     r   make_executabler+   ?   s4    
 W[!!FH[&.4<788888r   Fc                    |rd}nd|  d}t          |d          }|rEt          j                            |           rt	          |            nt          j        |            dS t          dd          }|rdS t          j                     dS )a  
    Ask user if it's okay to delete the previously-downloaded file/directory.

    If yes, delete it. If no, checks to see if the old version should be
    reused. If yes, it's reused; otherwise, Cookiecutter exits.

    :param path: Previously downloaded zipfile.
    :param no_input: Suppress prompt to delete repo and just delete it.
    :return: True if the content was deleted
    TzYou've downloaded z1 before. Is it okay to delete and re-download it?yesz+Do you want to re-use the existing version?FN)r   r   r   isdirr   removesysexit)r   no_inputok_to_deletequestionok_to_reuses        r   prompt_and_deleter6   H   s      9 YXXX 	 (%88 7== 	4LLLLIdOOOt&95
 
  	5




r   c                 J      G  fddt                     } j        |_        |S )z@Decorate a function to wrap it in a simplified jinja2 extension.c                   "     e Zd Z fdZ xZS ),simple_filter.<locals>.SimpleFilterExtensionc                 h    t                                          |           |j        j        <   d S N)super__init__filters__name__)selfenvironment	__class__filter_functions     r   r=   z5simple_filter.<locals>.SimpleFilterExtension.__init__r   s1    GG[)))<KK 8999r   )r?   
__module____qualname__r=   __classcell__)rB   rC   s   @r   SimpleFilterExtensionr9   q   sH        	L 	L 	L 	L 	L 	L 	L 	L 	L 	Lr   rG   )r   r?   )rC   rG   s   ` r   simple_filterrH   n   sU    L L L L L L L	 L L L
 &5%="  r   repo_dirc                    t          |                                           } t          j        d          }| d| j         }t
                              d|  d|            t          j        | |           t          |          S )z?Create a temporary dir with a copy of the contents of repo_dir.cookiecutter)prefix/zCopying repo_dir from z to )	r   resolvetempfilemkdtempnamer   r   r   copytree)rI   base_dirnew_dirs      r   create_tmp_repo_dirrU   z   s    H~~%%''H~666H++HM++G
LLA(AAAABBB
OHg&&&==r   )r   r   r   Nr;   )F)__doc__
contextlibloggingr   r   r	   r0   rO   pathlibr   
jinja2.extr   cookiecutter.promptr   	getLoggerr?   r   r   r   r   contextmanagerr%   r+   r6   rH   rU    r   r   <module>r_      sO   4 4      				   



                    0 0 0 0 0 0		8	$	$  . . .	J 	J 	J 	J    9 9 9# # # #L	! 	! 	!"4       r   