
                         b    S r SSKrSSKrSSKrS rS rS
S jr " S S\5      r " S S	\	5      r
g)zSafe access to git files.    Nc                 R     [         R                  " U 5        g! [         a     gf = f)z1Ensure a directory exists, creating if necessary.N)osmakedirsFileExistsError)dirnames    lib/third_party/dulwich/file.pyensure_dir_existsr	      s%    
G s    
&&c                 0   [         R                  R                  U5      (       d   [         R                  " X5        gSSKn UR                  SU SS9u  p4[         R                  " U5        [         R                  " U5         [         R                  " X5         [         R                  " X5        [         R                  " U5        g! [         a    e f = f! [         a    e f = f! [         a    e f = f! [         a    [         R                  " XA5        e f = f)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    77>>'""	IIg' 	  ((S(I

		'

		'#
		'# IIg3  		   	    
		'#s/   C ?C C$ C3 CC!$C03"Dc                     SU;   a  [        S5      eSU;   a  [        S5      eSU;  a  [        S5      eSU;   a  [        XU5      $ [        R                  " XU5      $ )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.
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)IOError_GitFileioopen)filenamemodebufsizes      r   GitFiler'   E   sb     d{?@@
d{CDD
$=>>
d{00wwxw//    c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )
FileLocked]   zFile is already locked.c                 D   > Xl         X l        [        [        U ]  X5        g N)r$   lockfilenamesuperr*   __init__)selfr$   r.   	__class__s      r   r0   FileLocked.__init__`   s     (j$(@r(   )r$   r.   )__name__
__module____qualname____firstlineno____doc__r0   __static_attributes____classcell__)r2   s   @r   r*   r*   ]   s    !A Ar(   r*   c                   T    \ rS rSrSr\" / SQ5      rSrS rS r	S r
S rS	 rS
 rSrg)r!   f   aX  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.
)closedencodingerrorsr%   namenewlines	softspace)__iter__flushfilenoisattyreadreadline	readlinesseektelltruncatewrite
writelinesc           	      Z   Xl         [        U R                   [        5      (       a  U R                   S-   U l        OU R                   S-   U l         [        R
                  " U R                  [        R                  [        R                  -  [        R                  -  [        [        SS5      -  5      n[        R                  " XBU5      U l        SU l        U R                   H#  n[!        X[        U R                  U5      5        M%     g ! [         a    [        XR                  5      ef = f)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)r1   r$   r%   r&   r   methods         r   r0   _GitFile.__init__   s    !dnne,,!%(!:D!%'!9D	;""		BJJ&2WRQ5OOB YYr1
((FD'$**f"=> )  	;X'9'9::	;s   A#D
 
 D*c                     U R                   (       a  gU R                  R                  5          [        R                  " U R
                  5        SU l         g! [         a
    SU l          gf = f)zpClose and discard the lockfile without overwriting the target.

If the file is already closed, this is a no-op.
NT)r[   rZ   r   r   r   rT   FileNotFoundErrorr1   s    r   abort_GitFile.abort   sR    
 <<

	 IId(()DL  	 DL	 s   'A A*)A*c                 @   U R                   (       a  g[        R                  " U R                  R	                  5       5        U R                  R                  5          [        [        SS5      b,  [        R                  " U R                  U R                  5        O`[        R                  S:w  a,  [        R                  " U R                  U R                  5        O [        U R                  U R                  5        U R                  5         g! U R                  5         f = f)a  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)r[   r   fsyncrZ   rE   r   rX   rf   rT   rQ   sysplatformr   r   rc   rb   s    r   r   _GitFile.close   s     <<
""$%

	r9d+7

4--t~~><<7*IId00$..A "$"4"4dnnEJJLDJJLs   BD Dc                     U $ r-    rb   s    r   	__enter___GitFile.__enter__   s    r(   c                 $    U R                  5         g r-   )r   )r1   exc_typeexc_valexc_tbs       r   __exit___GitFile.__exit__   s    

r(   c                 b    XR                   ;   a  [        U R                  U5      $ [        U5      e)z,Proxy property calls to the underlying file.)PROXY_PROPERTIESrX   rZ   AttributeError)r1   r@   s     r   __getattr___GitFile.__getattr__   s*    (((4::t,,T""r(   )r[   rZ   rQ   rT   N)r4   r5   r6   r7   r8   setrw   r\   r0   rc   r   rn   rt   ry   r9   rm   r(   r   r!   r!   f   s>     	

M?& :#r(   r!   )rb)r8   r"   r   ri   r	   r   r'   	Exceptionr*   objectr!   rm   r(   r   <module>r      s?   *   	 	 
B00A An#v n#r(   