
    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  ej                  e      Zd Zd ZddZej*                  dd       Zd Zd Zdd	d
efdZde
fdZy)z.Helper functions used throughout Cookiecutter.    N)Path)Dict)	Extension)StrictEnvironmentc                 \    t        j                  |t        j                          | |       y)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.12/site-packages/cookiecutter/utils.pyforce_deleter      s     HHT4==!J    c                 :    t        j                  | t               y)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    
 MM$-r   os.PathLike[str]returnc                     t         j                  d|        	 t        |       j                  dd       y# 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    %   sW    
 LLJDQJT
5 J6tf=>EIJs   5 	AAAc              #      K   t        j                         }	 | t        j                  |        d t        j                  |       y# t        j                  |       w xY w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&   1   sE      YY[FHHW
s   A#A	 A#	A  A#c                     t        j                  |       }t        j                  | |j                  t        j                  z         y)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,   @   s-    
 WW[!FHH[&..4<<78r   c                 L      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                 V    t         |   |       |j                  j                  <   y N)super__init__filters__name__)selfenvironment	__class__filter_functions     r   r3   z5simple_filter.<locals>.SimpleFilterExtension.__init__M   s&    G[)<KK 8 89r   )r5   
__module____qualname__r3   __classcell__)r8   r9   s   @r   SimpleFilterExtensionr/   L   s    	L 	Lr   r=   )r   r5   )r9   r=   s   ` r   simple_filterr>   I   s(    L	 L
 &5%=%="  r   repo_dirc                     t        |       j                         } t        j                  d      }| d| j                   }t
        j                  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)r?   base_dirnew_dirs      r   create_tmp_repo_dirrK   U   sj    H~%%'H~6H
!HMM?+G
LL)(4yAB
OOHg&=r   contextc                 b    | j                  di       j                  di       }t        d| dd|S )z6Create a jinja environment using the provided context.rA   _jinja2_env_varsT)rL   keep_trailing_newline )getr   )rL   envvarss     r   create_env_with_contextrS   _   s5    kk."-112DbIGTWDTGTTr   )r   r   r   Nr1   )__doc__
contextlibloggingr   r   r
   rE   pathlibr   typingr   
jinja2.extr   cookiecutter.environmentr   	getLoggerr5   r   r   r   r    contextmanagerr&   r,   r>   rK   rS   rP   r   r   <module>r]      s    4   	        6			8	$.	J  9	!"4  UT Ur   