U
    ^»{j–  ã                   @  s´   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	Z	d dl
mZ d dlmZ d dlZerˆd dlmZmZmZmZ e e¡ZG dd„ deƒZG dd	„ d	ƒZdS )
é    )ÚannotationsN)ÚPath)ÚTYPE_CHECKING)ÚAnyÚCallableÚMutableMappingÚSequencec                   @  s   e Zd ZdZdS )ÚTmpDirWarningz@A warning if for whatever reason we can't eliminate the tmp dir.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/choreographer/utils/_tmpfile.pyr	      s   r	   c                   @  sj   e Zd ZU dZded< ded< ded< dd	d
œdddœdd„Zd	d	dœddddœdd„Zddœdd„ZdS )ÚTmpDirectoryaT  
    The python stdlib `TemporaryDirectory` wrapper for easier use.

    Python's `TemporaryDirectory` suffered a couple API changes that mean
    you can't call it the same way for similar versions. This wrapper is
    also much more aggressive about deleting the directory when it's done,
    not necessarily relying on OS functions.
    z tempfile.TemporaryDirectory[str]Útemp_dirr   ÚpathÚboolÚexistsNF)Úsneakz
str | None)r   r   c                C  sâ   t tjdd… dkƒ| _i }|r*d|i}n|r<dt ¡ dœ}t ¡ dkrXtj	f |Ž| _
n^tjdd… }|dkrˆtj	f dd	d
œ|—Ž| _
n.|dkr¨tj	f dd	i|—Ž| _
ntj	f |Ž| _
t| j
jƒ| _t d| j› d�¡ d	| _dS )zç
        Construct a wrapped `TemporaryDirectory`.

        Args:
            path: manually specify the directory to use
            sneak: (default False) avoid using /tmp
                Ubuntu's snap will sandbox /tmp

        Né   )r   é   Údirz.choreographer-)Úprefixr   ÚWindowsFT)ÚdeleteÚignore_cleanup_errors)r   é
   r   zTemp directory created: Ú.)r   ÚsysÚversion_infoÚ_with_onexcr   ÚhomeÚplatformÚsystemÚtempfileÚTemporaryDirectoryr   Únamer   Ú_loggerÚinfor   )Úselfr   r   ÚargsZvinfor   r   r   Ú__init__+   s4    

þý
ÿþ
zTmpDirectory.__init__)Ú
check_onlyÚquietz5tuple[int, int, Sequence[tuple[Path, BaseException]]])r-   r.   Úreturnc                C  s
  | j  ¡ sd| _ddg fS d}d}g }tj| j dd�D �] \}}}|t|ƒ7 }|t|ƒ7 }|s6|D ]x}	t|ƒ|	 }
t d|
› �¡ z&|
 t	j
¡ |
jdd� t d¡ W qb tk
rØ } z| |
|f¡ W 5 d }~X Y qbX qb|D ]v}t|ƒ| }
t d|
› �¡ z"|
 t	j
¡ |
 ¡  t d¡ W qà tk
�rT } z| |
|f¡ W 5 d }~X Y qàX qàq6|�s²z| j  t	j
¡ | j  ¡  W n4 tk
�r° } z| | j |f¡ W 5 d }~X Y nX |�rÔ|�sÄ|�rÌd| _nd| _n,|�rú|�sòt d	|› �t¡ d| _nd| _|||fS )
NFr   )Útopdownz
Have file T)Ú
missing_okÚDeletedzHave directory zOThe temporary directory could not be deleted, execution will continue. errors: )r   r   ÚosÚwalkÚlenr   r(   Zdebug2ÚchmodÚstatÚS_IWUSRÚunlinkÚ	ExceptionÚappendÚrmdirÚwarningsÚwarnr	   )r*   r-   r.   Zn_dirsZn_filesÚerrorsÚrootÚdirsÚfilesÚfÚfpÚeÚdr   r   r   Ú_delete_manuallyS   s^    


"$"ýzTmpDirectory._delete_manuallyÚNone©r/   c              
     s„  z.t ˆ dƒrˆ jrˆ j ¡  dˆ _t d¡ W n2 tk
r` } zt d|› �¡ W 5 d}~X Y nX dddd	d
œdd„}zJˆ jr�tj	ˆ j
|d� ntj	ˆ j
|d� dˆ _t ˆ dƒr´ˆ `t d¡ W n¾ tk
rò   dˆ _t ˆ dƒräˆ `t d¡ Y nŽ tk
�r~ } zntjd|d� ˆ jdd� ˆ j�s0W Y ¢FdS t d|› �¡ d	dœ‡ fdd„}|ƒ  ˆ j
 ¡ �rnt d¡ W 5 d}~X Y nX dS )z@Try several different ways to eliminate the temporary directory.r   Fz$TemporaryDirectory.cleanup() worked.z+TemporaryDirectory.cleanup() failed. Error NzCallable[[str], None]z
str | Pathr   rH   )Úfuncr   Ú_excinfor/   c                 S  s:   z t |ƒ tj¡ | t|ƒƒ W n tk
r4   Y nX d S )N)r   r6   r7   r8   ÚstrÚFileNotFoundError)rJ   r   rK   r   r   r   Úremove_readonly¡   s
    z+TmpDirectory.clean.<locals>.remove_readonly)Úonexc)Úonerrorzshutil.rmtree worked.zError during tmp file removal.)Úexc_infoT)r-   z7shutil.rmtree() failed to delete temporary file. Error rI   c                    sf   d} d}ˆ j  ¡ rL| |k rLt d| › d�¡ ˆ jdd� | d7 } t d¡ qˆ j  ¡ rbˆ jd	d� d S )
Nr   é   zExtra manual clean executing r   T)r.   é   é   F)r   r   r(   r)   rG   ÚtimeÚsleep)ÚiÚtries©r*   r   r   Úextra_cleanÁ   s    
z'TmpDirectory.clean.<locals>.extra_cleanz/Temporary dictory couldn't be removed manually.)Úhasattrr   Úcleanupr   r(   r)   r:   r!   ÚshutilÚrmtreer   rM   ÚdebugrG   Úwarning)r*   rE   rN   rZ   r   rY   r   Úclean”   s>    
"


zTmpDirectory.clean)N)r
   r   r   r   Ú__annotations__r,   rG   ra   r   r   r   r   r      s   
	+üAr   )Ú
__future__r   r3   r#   r]   r7   r   r%   rU   r=   Úpathlibr   Útypingr   Zlogistror   r   r   r   Ú	getLoggerr
   r(   ÚUserWarningr	   r   r   r   r   r   Ú<module>   s    
