
    .Ph0                        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	m
Z
mZ  e
            Zd dlZ	 d dlmZ n!# e$ r d dlmZmZ  e            ZddZY nw xY we j        Ze j        Zej        Zej        j        Zej        Zej        Zej        j        Zej        j        Zej         Z ej        j!        Z!ej        j"        Z"ej        j#        Z#ej$        Z$ej%        Z%ej&        Z&ej'        Z'd Z(d Z)d d	l*m+Z+m,Z,  G d
 d          Z-d Z.ddZ/d Z0ddZ1ddZ2ddZ3d Z4d Z5 G d d          Z6 G d d          Z7dS )    N)BytesIO)StaticWindowMapManagerSlidingWindowMapManagerSlidingWindowMapBuffer)unpack_from)unpackcalcsizec                     	 t           |          }n)# t          $ r t          |           }|t           | <   Y nw xY wt          | ||||z                      S N)__calcsize_cacheKeyErrorr	   r   )fmtdataoffsetsizes       J/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/gitdb/util.pyr   r   !   sj    	)#C(DD 	) 	) 	)C==D$(S!!!	) c4 56777s    #66c                     t           j        dk    rHt          d          D ]0}	  | |i |c S # t          $ r t	          j        d           Y -w xY w | |i |S  | |i |S )Nwin32
   g?)sysplatformrange	Exceptiontimesleep)funcargskwargs_s       r   _retryr    D   s     |wr 	  	 A tT,V,,,,,      
3 tT$V$$$tT$V$$$s   -AAc                  6    t          t          j        g| R i |S r   )r    osremove)r   r   s     r   r#   r#   R   s"    ")-d---f---    )NULL_BIN_SHANULL_HEX_SHAc                   6    e Zd ZdZdZd
dZd Zd Zd Zd Z	d	S )_RandomAccessBytesIOzWrapper to provide required functionality in case memory maps cannot or may
    not be used. This is only really required in python 2.4_sio c                 .    t          |          | _        d S r   )r   r)   )selfbufs     r   __init__z_RandomAccessBytesIO.__init__g   s    CLL			r$   c                 ,    t          | j        |          S r   )getattrr)   r,   attrs     r   __getattr__z _RandomAccessBytesIO.__getattr__j   s    ty$'''r$   c                 D    t          |                                           S r   )lengetvaluer,   s    r   __len__z_RandomAccessBytesIO.__len__m   s    4==??###r$   c                 6    |                                  |         S r   r6   )r,   is     r   __getitem__z _RandomAccessBytesIO.__getitem__p   s    }}q!!r$   c                 :    |                                  ||         S r   r:   )r,   startends      r   __getslice__z!_RandomAccessBytesIO.__getslice__s   s    }}uSy))r$   N)r*   )
__name__
__module____qualname____doc__	__slots__r.   r3   r8   r<   r@    r$   r   r(   r(   a   ss        ? ?I! ! ! !( ( ($ $ $" " "* * * * *r$   r(   c                 F    	 t          |           S # t          $ r | cY S w xY w)z
    Return the integer representation of the byte string.  This supports Python
    3 byte arrays as well as standard strings.
    )ord	TypeError)bs    r   byte_ordrK   w   s6    
1vv   s      r$   c                     	 t          j        |           S # t          $ r ddl}|                    |           }|cY S w xY w)z`A python2.4 workaround for the sha/hashlib module fiasco

    **Note** From the dulwich project r   N)hashlibsha1	NameErrorsha)sourcerP   rN   s      r   make_sharR      sS    |F###   


wwvs    %>>c                     | dk    rt          d          S 	 t          j        d|           S # t          $ r t          d| z            cY S w xY w)zB:return: a file-protocol accessible memory block of the given sizer   r$       )r(   mmapOSError)r   s    r   allocate_memoryrX      se    qyy#C(((2yT""" 2 2 2
 $EDL111112s   , A
AFTc                 |   	 |rj	 t          j         | dt           j                  S # t          $ r; t          j         | t          j        |           j        t           j                  cY S w xY wn# t          $ r Y nw xY wt          j        | t          j        |           j                  }|rt          |          S |S )a  :return: read-only contents of the file represented by the file descriptor fd

    :param fd: file descriptor opened for reading
    :param stream: if False, random access is provided, otherwise the stream interface
        is provided.
    :param allow_mmap: if True, its allowed to map the contents into memory, which
        allows large files to be handled and accessed efficiently. The file-descriptor
        will change its position if this is Falser   )access)rV   ACCESS_READrW   r"   fstatst_sizereadr(   )fdstream
allow_mmapcontentss       r   file_contents_rorc      s    
 	TTyQt/?@@@@ T T TyRXb\\%9$BRSSSSSST		T    
 wr28B<</00H .#H---Os-   A/  & AA+(A/ *A++A/ /
A<;A<c           	          t          j        | t           j        t          t           dd          z  |z            }	 t	          |||          t          |           S # t          |           w xY w)a<  Get the file contents at filepath as fast as possible

    :return: random access compatible memory of the given filepath
    :param stream: see ``file_contents_ro``
    :param allow_mmap: see ``file_contents_ro``
    :param flags: additional flags to pass to os.open
    :raise OSError: If the file could not be opened

    **Note** for now we don't try to use O_NOATIME directly as the right value needs to be
    shared per database in fact. It only makes a real difference for loose object
    databases anyway, and they use it with the help of the ``flags`` parameterO_BINARYr   )r"   openO_RDONLYr0   rc   close)filepathr`   ra   flagsr_   s        r   file_contents_ro_filepathrk      s^     
2;Z)C)CCeK	L	LBFJ77b				b				s   A A,c                 T    t          t                              |           |          S )z
    :return: a buffer compatible object which uses our mapped memory manager internally
        ready to read the whole given filepath)rj   )r   mmanmake_cursor)ri   rj   s     r   sliding_ro_bufferro      s%     "$"2"28"<"<EJJJJr$   c                 J    t          |           dk    r| S t          |           S )z!:return: hexified version  of sha(   )r5   
bin_to_hexrP   s    r   
to_hex_shart      s"    
3xx2~~
c??r$   c                 J    t          |           dk    r| S t          |           S )N   )r5   
hex_to_binrs   s    r   
to_bin_sharx      s"    
3xx2~~
c??r$   c                   2    e Zd ZdZ e            Zd Zd ZdS )	LazyMixina?  
    Base class providing an interface to lazily retrieve attribute values upon
    first access. If slots are used, memory will only be reserved once the attribute
    is actually accessed and retrieved the first time. All future accesses will
    return the cached value as stored in the Instance's dict or slot.
    c                 b    |                      |           t                              | |          S )z
        Whenever an attribute is requested that we do not know, we allow it
        to be created and set. Next time the same attribute is requested, it is simply
        returned from our dict/slots. )_set_cache_object__getattribute__r1   s     r   r3   zLazyMixin.__getattr__   s.    
 	&&tT222r$   c                     dS )a  
        This method should be overridden in the derived class.
        It should check whether the attribute named by attr can be created
        and cached. Do nothing if you do not know the attribute or call your subclass

        The derived class may create as many additional attributes as it deems
        necessary in case a git command returns more information than represented
        in the single attribute.NrF   r1   s     r   r|   zLazyMixin._set_cache_  s	     	r$   N)rA   rB   rC   rD   tuplerE   r3   r|   rF   r$   r   rz   rz      sI          I3 3 3	 	 	 	 	r$   rz   c                   D    e Zd ZdZdZd Zd Zd ZddZd Z	d	 Z
ddZdS )LockedFDa  
    This class facilitates a safe read and write operation to a file on disk.
    If we write to 'file', we obtain a lock file at 'file.lock' and write to
    that instead. If we succeed, the lock file will be renamed to overwrite
    the original file.

    When reading, we obtain a lock file, but to prevent other writers from
    succeeding while we are reading the file.

    This type handles error correctly in that it will assure a consistent state
    on destruction.

    **note** with this setup, parallel reading is not possible	_filepath_fd_writec                 0    || _         d| _        d| _        dS )z.Initialize an instance with the givne filepathNr   )r,   ri   s     r   r.   zLockedFD.__init__  s    !r$   c                 @    | j         |                                  d S d S r   )r   rollbackr7   s    r   __del__zLockedFD.__del__$  s#    8MMOOOOO  r$   c                     d| j         z  S )Nz%s.lock)r   r7   s    r   _lockfilepathzLockedFD._lockfilepath)  s    4>))r$   Fc                    | j         t          d| j        z            || _         t          t          dd          }t          j        t          j        z  t          j        z  |z  }	 t	          j        |                                 |t          dd                    }|st	          j
        |           n|| _        n7# t          $ r*}t          d|                                 z            |d}~ww xY w| j        U	 t	          j        | j        t          j        |z            | _        n'#  t          |                                             xY w|rddlm}  || j                  S | j        S )	a  
        Open the file descriptor for reading or writing, both in binary mode.

        :param write: if True, the file descriptor will be opened for writing. Other
            wise it will be opened read-only.
        :param stream: if True, the file descriptor will be wrapped into a simple stream
            object which supports only reading or writing
        :return: fd to read from or write to. It is still maintained by this instance
            and must not be closed directly
        :raise IOError: if the lock could not be retrieved
        :raise OSError: If the actual file could not be opened for reading

        **note** must only be called onceNzCalled %s multiple timesre   r   600   z Lock at %r could not be obtained)FDStream)r   AssertionErrorrf   r0   r"   O_WRONLYO_CREATO_EXCLr   intrh   r   rW   r   rg   r#   gitdb.streamr   )r,   writer`   binarylockmoder_   er   s           r   rf   zLockedFD.open,  sp    ;" !;di!GHHH Z++;+bi7&@	\++--xUAGGB  	\ 	\ 	\<t?Q?Q?S?SSTTZ[[	\
 874>2;3GHHt))++,,,  	------8DH%%%8Os%   &AB; ;
C/%C**C/:,D' '$Ec                 2    |                      d           dS )zWhen done writing, call this function to commit your changes into the
        actual file.
        The file descriptor will be closed, and the lockfile handled.

        **Note** can be called multiple timesT
successfulN_end_writingr7   s    r   commitzLockedFD.commita  s!     	T*****r$   c                 2    |                      d           dS )zAbort your operation without any changes. The file descriptor will be
        closed, and the lock released.

        **Note** can be called multiple timesFr   Nr   r7   s    r   r   zLockedFD.rollbacki  s!    
 	U+++++r$   Tc                    | j         t          d          | j        dS t          j        | j                   d| _        |                                 }| j         ry|rwt          j        dk    r(t          | j	                  rt          | j	                   t          j        || j	                   t          | j	        t          dd                     dS t          |           dS )z,Handle the lock according to the write mode Nz-Cannot end operation if it wasn't started yetr   644r   )r   r   r   r"   rh   r   r   r   isfiler   r#   renamechmodr   )r,   r   lockfiles      r   r   zLockedFD._end_writingp  s    ; !PQQQ8F
%%''; 	: 	|w&&$.)) +4>*** Ih///
 $.#eQ--00000 8r$   N)FF)T)rA   rB   rC   rD   rE   r.   r   r   rf   r   r   r   rF   r$   r   r   r     s        B B /I    
* * *2 2 2 2j+ + +, , ,     r$   r   )r   )r$   )FT)FTr   )8binasciir"   rV   r   r   errnoior   smmapr   r   r   rm   rM   structr   ImportErrorr   r	   dictr   a2b_hexrw   b2a_hexrr   ENOENTpathexistsmkdirr   isdirr   r   dirnamebasenamejoinr^   r   rh   fsyncr    r#   gitdb.constr%   r&   r(   rK   rR   rX   rc   rk   ro   rt   rx   rz   r   rF   r$   r   <module>r      s  
  				  



                     8""""""" 8 8 8''''''''tvv8 8 8 8 8 8	8" 

 
 



		
'/7	w|	w


% % %. . .
       * * * * * * * *,  	 	 	 	2 2 2"   :   (K K K K           @~ ~ ~ ~ ~ ~ ~ ~ ~ ~s   ? AA