a
    Lb
                     @   s   d Z ddlm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 eeZdd Zdd Zd	d
 ZejdddZdd ZdddZdS )z.Helper functions used throughout Cookiecutter.    )unicode_literalsN)read_user_yes_noc                 C   s   t |tj | | dS )zError handler for `shutil.rmtree()` equivalent to `rm -rf`.

    Usage: `shutil.rmtree(path, onerror=force_delete)`
    From stackoverflow.com/questions/1889597
    N)oschmodstatS_IWRITE)funcpathexc_info r   1lib/python3.9/site-packages/cookiecutter/utils.pyforce_delete   s    r   c                 C   s   t j| td dS )zfRemove a directory and all its contents. Like rm -rf on Unix.

    :param path: A directory path.
    )onerrorN)shutilrmtreer   )r	   r   r   r   r      s    r   c              
   C   sj   t d|  zt|  t d|  W n> tyd } z&|jtjkrPW Y d}~dS W Y d}~n
d}~0 0 dS )zHEnsure that a directory exists.

    :param path: A directory path.
    zMaking sure path exists: %szCreated directory at: %sNFT)loggerdebugr   makedirsOSErrorerrnoZEEXIST)r	   	exceptionr   r   r   make_sure_path_exists%   s    
$r   c              	   c   s@   t  }z&| durt |  dV  W t | nt | 0 dS )zoContext manager version of os.chdir.

    When exited, returns to the working directory prior to entering.
    N)r   getcwdchdir)dirnamecurdirr   r   r   work_in4   s    
r   c                 C   s"   t | }t | |jtjB  dS )zOMake `script_path` executable.

    :param script_path: The file to change
    N)r   r   r   st_modeS_IEXEC)Zscript_pathstatusr   r   r   make_executableC   s    
r    Fc                 C   sd   |r
d}nd | }t|d}|rFtj| r8t|  n
t|  dS tdd}|rXdS t  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
    TzEYou've downloaded {} before. Is it okay to delete and re-download it?Zyesz+Do you want to re-use the existing version?FN)	formatr   r   r	   isdirr   removesysexit)r	   Zno_inputZok_to_deleteZquestionZok_to_reuser   r   r   prompt_and_deleteL   s$    


r&   )N)F)__doc__Z
__future__r   
contextlibr   loggingr   r   r   r$   Zcookiecutter.promptr   Z	getLogger__name__r   r   r   r   contextmanagerr   r    r&   r   r   r   r   <module>   s"   

	