o
    Uh                     @   sh   d Z ddlZddlZddlZddlmZ dddZdddZdddZG dd de	Z
G dd dZdS )zSafe access to git files.    N)ClassVarreturnc                 C   s&   zt |  W dS  ty   Y dS w )z1Ensure a directory exists, creating if necessary.N)osmakedirsFileExistsError)dirname r   `/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/file.pyensure_dir_exists   s
   r
   c                 C   s   t j|sz	t | | W dS  ty    w ddl}z|jd| dd\}}t | t | W n ty:    w zt || W n tyK    w zt | | W n tyb   t ||  w t | dS )zCRename file with temporary backup file to rollback if rename fails.Nr   z.tmp.)prefixdir)	r   pathexistsrenameOSErrortempfilemkstempcloseremove)oldnamenewnamer   fdtmpfiler   r   r	   _fancy_rename&   s6   
r   rb  c                 C   sR   d|v rt dd|v rt dd|vrt dd|v r#t| |||S t| ||S )a  Create a file object that obeys the git file locking protocol.

    Returns: a builtin file object or a _GitFile object

    Note: See _GitFile for a description of the file locking protocol.

    Only read-only and write-only (binary) modes are supported; r+, w+, and a
    are not.  To read and write from the same file, you can take advantage of
    the fact that opening a file for write does not actually open the file you
    request.

    The default file mask makes any created files user-writable and
    world-readable.

    az'append mode not supported for Git files+z+read/write mode not supported for Git filesbz%text mode not supported for Git filesw)r   _GitFileopen)filenamemodebufsizemaskr   r   r	   GitFileG   s   r(   c                       s"   e Zd ZdZd fddZ  ZS )
FileLockedzFile is already locked.r   Nc                    s   || _ || _t || d S N)r$   lockfilenamesuper__init__)selfr$   r+   	__class__r   r	   r-   f   s   zFileLocked.__init__r   N)__name__
__module____qualname____doc__r-   __classcell__r   r   r/   r	   r)   c   s    r)   c                   @   s   e Zd ZU dZh dZeee  ed< h dZ	eee  ed< ddd	Z
dd
dZdddZdddZdd Zdd Zdd ZdS )r"   ap  File that follows the git locking protocol for writes.

    All writes to a file foo will be written into foo.lock in the same
    directory, and the lockfile will be renamed to overwrite the original file
    on close.

    Note: You *must* call close() or abort() on a _GitFile for the lock to be
        released. Typically this will happen in a finally block.
    >   r%   nameclosederrorsencodingnewlines	softspacePROXY_PROPERTIES>   readseektellflushwritefilenoisatty__iter__readlinetruncate	readlines
writelinesPROXY_METHODSr   Nc              
   C   s   || _ t| j tr| j d | _n| j d | _zt| jtjtjB tjB t	tddB |}W n t
y@ } zt|| j|d }~ww t|||| _d| _| jD ]}t| |t	| j| qOd S )Ns   .lockz.lockO_BINARYr   F)	_filename
isinstancebytes_lockfilenamer   r#   O_RDWRO_CREATO_EXCLgetattrr   r)   fdopen_file_closedrJ   setattr)r.   r$   r%   r&   r'   r   excmethodr   r   r	   r-      s&   
z_GitFile.__init__c                 C   sH   | j rdS | j  zt| j d| _ W dS  ty#   d| _ Y dS w )zClose and discard the lockfile without overwriting the target.

        If the file is already closed, this is a no-op.
        NT)rV   rU   r   r   r   rO   FileNotFoundErrorr.   r   r   r	   abort   s   
z_GitFile.abortc                 C   s   | j rdS | j  t| j  | j  z;ttdddur)t| j	| j
 ntjdkr7t| j	| j
 nt| j	| j
 W |   dS W |   dS W |   dS |   w )a7  Close this file, saving the lockfile over the original.

        Note: If this method fails, it will attempt to delete the lockfile.
            However, it is not guaranteed to do so (e.g. if a filesystem
            becomes suddenly read-only), which will prevent future writes to
            this file until the lockfile is removed manually.

        Raises:
          OSError: if the original file could not be overwritten. The
            lock file is still closed, so further attempts to write to the same
            file object will raise ValueError.
        Nreplacewin32)rV   rU   rA   r   fsyncrC   r   rS   r]   rO   rL   sysplatformr   r   r\   r[   r   r   r	   r      s    


	z_GitFile.closec                 C   s2   t | ddstjd| tdd |   d S d S )NrV   Tz	unclosed    )
stacklevel)rS   warningswarnResourceWarningr\   r[   r   r   r	   __del__   s   z_GitFile.__del__c                 C   s   | S r*   r   r[   r   r   r	   	__enter__   s   z_GitFile.__enter__c                 C   s    |d ur
|    d S |   d S r*   )r\   r   )r.   exc_typeexc_valexc_tbr   r   r	   __exit__   s   z_GitFile.__exit__c                 C   s   || j v rt| j|S t|)z,Proxy property calls to the underlying file.)r=   rS   rU   AttributeError)r.   r7   r   r   r	   __getattr__   s   
z_GitFile.__getattr__r1   )r2   r3   r4   r5   r=   r   setstr__annotations__rJ   r-   r\   r   rg   rh   rl   rn   r   r   r   r	   r"   l   s   
 
	



r"   r1   )r   r   r   )r5   r   r`   rd   typingr   r
   r   r(   	Exceptionr)   r"   r   r   r   r	   <module>   s   


!	