
    d                     l    d Z ddlZddlZddlZd Zd ZddZ G d	 d
e          Z G d d          Z	dS )zSafe access to git files.    Nc                 R    	 t          j        |            dS # t          $ r Y dS w xY w)z1Ensure a directory exists, creating if necessary.N)osmakedirsFileExistsError)dirnames    P/home/feoh/.local/pipx/venvs/poetry/lib/python3.11/site-packages/dulwich/file.pyensure_dir_existsr	      s?    
G   s    
&&c                 .   t           j                            |          s(	 t          j        | |           n# t          $ r  w xY wdS ddl}	 |                    d| d          \  }}t          j        |           t          j        |           n# t          $ r  w xY w	 t          j        ||           n# t          $ r  w xY w	 t          j        | |           n$# t          $ r t          j        ||            w xY wt          j        |           dS )zBRename file with temporary backup file to rollback if rename failsNr   z.tmp.)prefixdir)	r   pathexistsrenameOSErrortempfilemkstempcloseremove)oldnamenewnamer   fdtmpfiles        r   _fancy_renamer   $   s]   7>>'"" 	Igw'''' 	 	 		 OOO ((S(IIW

	'    	
	'7####   
	'7####   
	'7### Igs.   7 AAB B!B7 7CC !C>rb  c                     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masks       r   GitFiler)   E   s{      d{{?@@@
d{{CDDD
$=>>>
d{{$666HdG,,,    c                   "     e Zd ZdZ fdZ xZS )
FileLockedzFile is already locked.c                 h    || _         || _        t                                          ||           d S N)r%   lockfilenamesuper__init__)selfr%   r/   	__class__s      r   r1   zFileLocked.__init__d   s2     (<00000r*   )__name__
__module____qualname____doc__r1   __classcell__)r3   s   @r   r,   r,   a   s>        !!1 1 1 1 1 1 1 1 1r*   r,   c                   H    e Zd ZdZh dZdZd Zd Zd Zd Z	d Z
d	 Z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)__iter__flushfilenoisattyreadreadline	readlinesseektelltruncatewrite
writelinesc           	      &   || _         t          | j         t                    r| j         dz   | _        n| j         dz   | _        	 t	          j        | j        t          j        t          j        z  t          j        z  t          t          dd          z  |          }n(# t          $ r}t          || j                  |d }~ww xY wt	          j        |||          | _        d| _        | j        D ]&}t!          | |t          | j        |                     'd S )Ns   .lockz.lockO_BINARYr   F)	_filename
isinstancebytes_lockfilenamer   r$   O_RDWRO_CREATO_EXCLgetattrr   r,   fdopen_file_closedPROXY_METHODSsetattr)r2   r%   r&   r'   r(   r   excmethods           r   r1   z_GitFile.__init__   s   !dne,, 	:!%(!:D!%'!9D	D"	BJ&2WRQ5O5OO BB
  	D 	D 	DXt'9::C	DYr411
( 	? 	?FD&'$*f"="=>>>>	? 	?s   AB 
B>#B99B>c                     | j         rdS | j                                         	 t          j        | j                   d| _         dS # t          $ r d| _         Y dS w xY w)zClose and discard the lockfile without overwriting the target.

        If the file is already closed, this is a no-op.
        NT)rX   rW   r   r   r   rQ   FileNotFoundErrorr2   s    r   abortz_GitFile.abort   sr    
 < 	F
	 Id()))DLLL  	  	  	 DLLLL	 s    A AAc                 .   | j         rdS | j                                         t          j        | j                                                   | j                                         	 t          t          dd           t          j        | j	        | j
                   nJt          j        dk    r t          j        | j	        | j
                   nt          | j	        | j
                   |                                  dS # |                                  w xY w)a6  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)rX   rW   rA   r   fsyncrB   r   rU   rb   rQ   rN   sysplatformr   r   r`   r_   s    r   r   z_GitFile.close   s     < 	F

""$$%%%
	r9d++7
4-t~>>>><7**Id0$.AAAA "$"4dnEEEJJLLLLLDJJLLLLs   (B C> >Dc                     t          | dd          s5t          j        d| z  t          d           |                                  d S d S )NrX   Tzunclosed %r   )
stacklevel)rU   warningswarnResourceWarningr`   r_   s    r   __del__z_GitFile.__del__   sM    tY-- 	M-$.ANNNNJJLLLLL	 	r*   c                     | S r.    r_   s    r   	__enter__z_GitFile.__enter__   s    r*   c                 ^    ||                                   d S |                                  d S r.   )r`   r   )r2   exc_typeexc_valexc_tbs       r   __exit__z_GitFile.__exit__   s*    JJLLLLLJJLLLLLr*   c                 \    || j         v rt          | j        |          S t          |          )z,Proxy property calls to the underlying file.)PROXY_PROPERTIESrU   rW   AttributeError)r2   r:   s     r   __getattr__z_GitFile.__getattr__   s0    4(((4:t,,,T"""r*   N)r4   r5   r6   r7   rw   rY   r1   r`   r   rm   rp   ru   ry   ro   r*   r   r#   r#   j   s           M? ? ?(       <  
    # # # # #r*   r#   )r   r   r   )
r7   r   re   rj   r	   r   r)   	Exceptionr,   r#   ro   r*   r   <module>r{      s   *    				 



     B- - - -81 1 1 1 1 1 1 1v# v# v# v# v# v# v# v# v# v#r*   