o
    hU                     @   s  U zd dl mZ W n! ey*   zd dlmZ W n ey'   d dlmZ Y nw Y nw 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mZmZmZ d dlZd dlmZmZmZmZ ddlmZ d	d
lmZ eeZeej dddZ!ede"f e#d< ej$			ddee" dee" deeee"f  deeddf fddZ%dd Z&ej$dddee"ef dee' deeddf fddZ(dS )    )JSONDecodeErrorN)partial)Path)Callable	GeneratorOptionalUnion)BaseFileLockFileLockSoftFileLockTimeout   )	constants   )loggingT)streamallow_unicode.	yaml_dumpsuffixprefixdirreturnc                 k   s    t jd|| |d|}t|j V  zt|j W n ty:   z
tj|jtd W n	 ty7   Y nw Y nw z|	  W dS  tyL   Y dS w )a  
    Context manager to create a temporary directory and safely delete it.

    If tmp directory cannot be deleted normally, we set the WRITE permission and retry.
    If cleanup still fails, we give up but don't raise an exception. This is equivalent
    to  `tempfile.TemporaryDirectory(..., ignore_cleanup_errors=True)` introduced in
    Python 3.10.

    See https://www.scivision.dev/python-tempfile-permission-error-windows/.
    )r   r   r   )onerrorN )
tempfileTemporaryDirectoryr   nameresolveshutilrmtree	Exception_set_write_permission_and_retrycleanup)r   r   r   kwargstmpdirr   r   p/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/utils/_fixes.pySoftTemporaryDirectory+   s$   	r&   c                 C   s   t |tj | | d S )N)oschmodstatS_IWRITE)funcpathexcinfor   r   r%   r!   Q   s   r!   timeout	lock_filer/   c                c   s   t j}t| |d}t }	 t | }|dur#||kr#tt| z|j|r/t||| n|d W nC tyN   t	d|  dt | dd Y n+ t
yw } zdt|v rmtd	|  t| |d}W Y d}~qW Y d}~nd}~ww nqz*|V  W z|  W dS  ty   z
t|   W Y dS  ty   Y nw Y dS w z|  W w  ty   z	t|   W Y w  ty   Y Y w w w )
a  A filelock with some custom logic.

    This filelock is weaker than the default filelock in that:
    1. It won't raise an exception if release fails.
    2. It will default to a SoftFileLock if the filesystem does not support flock.

    An INFO log message is emitted every 10 seconds if the lock is not acquired immediately.
    If a timeout is provided, a `filelock.Timeout` exception is raised if the lock is not acquired within the timeout.
    r.   TNz!Still waiting to acquire lock on z (elapsed: z.1fz	 seconds)zuse SoftFileLock insteadzPFileSystem does not appear to support flock. Falling back to SoftFileLock for %s)r   FILELOCK_LOG_EVERY_SECONDSr
   timer   stracquireminloggerinfoNotImplementedErrorwarningr   releaseOSErrorr   unlink)r0   r/   log_intervallock
start_timeelapsed_timeer   r   r%   WeakFileLockV   s^   "rB   )NNN))requestsr   ImportError
simplejsonjson
contextlibr'   r   r)   r   r2   	functoolsr   pathlibr   typingr   r   r   r   yamlfilelockr	   r
   r   r    r   r   
get_logger__name__r6   dumpr   r3   __annotations__contextmanagerr&   r!   floatrB   r   r   r   r%   <module>   s`   
 %
