
    chk                     `   S SK Jr  S SKrS SKrS SKrS SKrS SKJrJrJ	r	J
r
Jr  S SKJrJrJrJrJr  S SKJrJr  S SKJr  Sr S SKJr  S	rS
r " S S\5      r " S S\5      r " S S5      r " S S\5      r  " S S\5      r! " S S\5      r" " S S5      r# " S S5      r$g! \ a     N`f = f)    )BytesION)msb_sizestream_copyapply_delta_dataconnect_deltasdelta_types)allocate_memory	LazyMixinmake_shawriteclose)	NULL_BYTE
BYTE_SPACE)force_bytesF)apply_deltaT)	DecompressMemMapReaderFDCompressedSha1WriterDeltaApplyReader
Sha1WriterFlexibleSha1WriterZippedStoreShaWriterr   FDStream
NullStreamc                       \ rS rSrSrSrSrSS jrS rS r	S	 r
\SS
 j5       rS rS rS r\" \SS5      4S jrSS jrSrg)r   .   as  Reads data in chunks from a memory map and decompresses it. The client sees
only the uncompressed data, respective file-like read calls are handling on-demand
buffered decompression accordingly

A constraint on the total size of bytes is activated, simulating
a logical file within a possibly larger physical memory area

To read efficiently, you clearly don't want to read individual bytes, instead,
read a few kilobytes at least.

**Note:** The chunk-size should be carefully selected as it will involve quite a bit
    of string copying due to the way the zlib is implemented. Its very wasteful,
    hence we try to find a good tradeoff between allocation time and number of
    times we actually allocate. An own zlib implementation would be good here
    to better support streamed reading - it would only need to keep the mmap
    and decompress it into chunks, that's all ... )_m_zip_buf_buflen_br_cws_cwe_s_close_cbr_phii   Nc                     Xl         [        R                  " 5       U l        SU l        SU l        Ub  X0l        SU l        SU l        SU l	        SU l
        SU l        X l        g)ztInitialize with mmap for stream reading
:param m: must be content data - use new if you have object data and no sizeNr   F)r   zlibdecompressobjr   r   r   r#   r    r!   r"   r%   r&   r$   )selfmclose_on_deletionsizes       _C:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\gitdb/stream.py__init__DecompressMemMapReader.__init__E   sZ     &&(		G				'    c                 4    US:X  d   eU R                  5         g )Nr#   )_parse_header_infor*   attrs     r.   _set_cache_"DecompressMemMapReader._set_cache_U   s    t|| 	!r1   c                 $    U R                  5         g N)r   r*   s    r.   __del__DecompressMemMapReader.__del__[   s    

r1   c                 *   SnXl         U R                  U5      nUR                  [        5      nUSU R	                  [
        5      u  pE[        U5      nXPl         SU l        US-  n[        X#S 5      U l	        [        U5      U-
  U l        SU l        XE4$ )zIf this stream contains object data, parse the header info and skip the
stream to a point where each read will yield object content

:return: parsed type_string, sizei    Nr      T)r#   readfindr   splitr   intr    r   r   lenr   r&   )r*   maxbhdrhdrendtypr-   s         r.   r3   )DecompressMemMapReader._parse_header_info^   s     iio)$L&&z2	4y !CL)	3x&(	yr1   c                 F    [        XS5      nUR                  5       u  pEXEU4$ )a~  Create a new DecompressMemMapReader instance for acting as a read-only stream
This method parses the object header from m and returns the parsed
type and size, as well as the created stream instance.

:param m: memory map on which to operate. It must be object data ( header + contents )
:param close_on_deletion: if True, the memory map will be closed once we are
    being deletedr   )r   r3   )r*   r+   r,   instrG   r-   s         r.   newDecompressMemMapReader.new{   s*     &aA>++-	$r1   c                     U R                   $ )z8:return: random access compatible data we are working on)r   r:   s    r.   dataDecompressMemMapReader.data   s    wwr1   c                     U R                   (       a=  [        U R                  S5      (       a  U R                  R                  5         SU l         gg)zClose our underlying stream of compressed bytes if this was allowed during initialization
:return: True if we closed the underlying stream
:note: can be called safely
r   FN)r$   hasattrr   r   r:   s    r.   r   DecompressMemMapReader.close   s5    
 ;;tww((DK r1   c                    U R                   U R                  :X  Ga^  U R                  R                  (       GdB  SU l         [	        U R                  S5      (       ar  U R                  R
                  [        R                  :X  aI  U R                  [        R                  5        U R                  R
                  [        R                  :X  a  MI  OU R                  R                  (       d  U R                  [        U R                  5      :w  a_  U R                  [        R                  5        U R                  R                  (       d%  U R                  [        U R                  5      :w  a  M_  U R                  U l         U R                  $ )z|
:return: number of compressed bytes read. This includes the bytes it
    took to decompress the header ( if there was one )r   status)r    r#   r   unused_datarQ   rT   r(   Z_OKr?   mmapPAGESIZEr%   rC   r   r:   s    r.   compressed_bytes_read,DecompressMemMapReader.compressed_bytes_read   s    0 88twwtyy'<'<'< DHtyy(++ii&&$))3IIdmm, ii&&$))3
 ))//DIITWW4MIIdmm, ))//DIITWW4M wwDH
 yyr1   SEEK_SETr   c                     US:w  d  U[        [        SS5      :w  a  [        S5      e[        R                  " 5       U l        S=U l        =U l        =U l        U l	        U R                  (       a
  SU l
        U ?gg)z_Allows to reset the stream to restart reading
:raise ValueError: If offset and whence are not 0r   r[   Can only seek to position 0FN)getattros
ValueErrorr(   r)   r   r    r!   r"   r%   r&   r#   r*   offsetwhences      r.   seekDecompressMemMapReader.seek   sm     Q;&GB
A$>>:;; &&(	7888498ty4999DI r1   c                    US:  a  U R                   U R                  -
  nO"[        XR                   U R                  -
  5      nUS:X  a  gSnU R                  (       a  U R                  U:  aG  U R                  R                  U5      nU =R                  U-  sl        U =R                  U-  sl        U$ U R                  R                  5       nXR                  -  nU =R                  U R                  -  sl        SU l        S U l        U R                  R                  nU(       a2  U R                  [        U5      -
  U l
        U R                  U-   U l        O&U R                  nU R                  U l
        XA-   U l        U R                  U R                  -
  S:  a  U R                  S-   U l        U R                  U R                  U R                   nU R                  [        U5      -   U l        U R                  R                  XQ5      n[        [        S[        R                  5      S;   a4  [         R"                  S:X  d   [        U R                  R                  5      nO?[        U R                  R                  5      [        U R                  R$                  5      -   nU =R&                  [        U5      U-
  -  sl        U =R                  [        U5      -  sl        U(       a  X&-   nU(       aT  [        U5      [        U5      -
  U:  a9  U R                  U R                   :  a  X`R                  U[        U5      -
  5      -  nU$ )Nr>   r   r1      ZLIB_RUNTIME_VERSION)z1.2.7z1.2.5darwin)r#   r    minr   r   r?   r   unconsumed_tailr"   rC   r!   r   
decompressr^   r(   ZLIB_VERSIONsysplatformrU   r%   )r*   r-   dattailcwsindatadcompdatunused_datalens           r.   r?   DecompressMemMapReader.read   sj   !877TXX%DtWWtxx/0D 19 99||t#iinnT*$D 
iinn&$DLL(  	 yy(( 		CI-DI		D(DI))C		DI
DI 99tyy 1$		ADI 499- IIF+	99''5 4/1B1BCGYYbebnbnrzbz !:!:;N !:!:;c$))BWBW>XXN 			S[>11	CM!~H XS1T9dhh>P		$X"677Hr1   )r    r   r   r%   r$   r"   r!   r   r&   r#   r   r9   F))__name__
__module____qualname____firstlineno____doc__	__slots__max_read_sizer/   r6   r;   r3   classmethodrK   rN   r   rY   r^   r_   rd   r?   __static_attributes__ r1   r.   r   r   .   sf    : !I M( ": 
 
 -b #*"j!"< ir1   r   c                       \ rS rSrSrSrSrS rS rS r	\
(       d  \	rO\rSS	 jr\" \S
S5      4S jr\S 5       r\S 5       r\S 5       r\S 5       rSrg)r   iB  ay  A reader which dynamically applies pack deltas to a base object, keeping the
memory demands to a minimum.

The size of the final object is only obtainable once all deltas have been
applied, unless it is retrieved from a pack index.

The uncompressed Delta has the following layout (MSB being a most significant
bit encoded dynamic size):

* MSB Source Size - the size of the base against which the delta was created
* MSB Target Size - the size of the resulting data after the delta was applied
* A list of one byte commands (cmd) which are followed by a specific protocol:

 * cmd & 0x80 - copy delta_data[offset:offset+size]

  * Followed by an encoded offset into the delta data
  * Followed by an encoded size of the chunk to copy

 *  cmd & 0x7f - insert

  * insert cmd bytes from the delta buffer into the output stream

 * cmd == 0 - invalid operation ( or error in delta stream )
)_bstream	_dstreams
_mm_target_sizer    ic                 x    [        U5      S:  d   S5       eUS   U l        [        USS 5      U l        SU l        g)zInitialize this instance with a list of streams, the first stream being
the delta to apply on top of all following deltas, the last stream being the
base object onto which to apply the deltasr>   z+Need at least one delta and one base streamrx   Nr   )rC   r   tupler   r    )r*   stream_lists     r.   r/   DeltaApplyReader.__init__h  sB     ;!#R%RR##B{3B/0r1   c                    [        U R                  5      S:X  a  U R                  U5      $ [        U R                  5      nUR	                  5       S:X  a  SU l        [        S5      U l        g UR	                  5       U l        [        U R
                  5      U l        [        U R                  R                  5      n[        U R                  R                  UR                  U R                  R                  S[        R                  -  5        U R                  R                  nUR                  X45        U R                  R!                  S5        g )Nr>   r      )rC   r   _set_cache_brute_r   rboundr   r	   r   r   r-   r   r?   r   rW   rX   applyrd   )r*   r5   dclbbufr   s        r.   _set_cache_too_slow_without_c.DeltaApplyReader._set_cache_too_slow_without_cr  s     t~~!#))$//
 T^^, ::<1DJ-a0DO ZZ\
)$**5t}}112DMM&&

DMM4F4FdmmH[\ %%		$Qr1   c           	         [        5       nSnU R                   HO  nUR                  S5      n[        U5      u  pg[        XV5      u  phUR	                  XVS XgU45        [        X85      nMQ     U R                  R                  n	Un[        U R                  5      S:  a  [        X5      =p[        U	5      n
[        U R                  R                  U
R                  U	S[        R                  -  5        [        U5      nSn[        [        U5      [        U R                  5      5       H  u  u  ppxn[        UR                  U-
  5      nUR                  U5        [        UR                  UR                  UR                  S[        R                  -  5        S[!        5       ;   a  [#        XU5        O![%        XU[        U5      UR                  5        XpU
R'                  S5        UR'                  S5        UnM     Xl        Xl        g)z*If we are here, we apply the actual deltasr   i   Nr>   r   c_apply_delta)listr   r?   r   appendmaxr   r-   rC   r	   r   r   rW   rX   zipreversedglobalsr   r   rd   r   r   )r*   r5   buffer_info_listmax_target_sizedstreambufrb   src_sizetarget_size	base_sizer   tbuffinal_target_sizedbufddatas                  r.   r   "DeltaApplyReader._set_cache_brute_  s   
  6~~G,,s#C'}F"*3"7F##S\6[$QR!/?O & MM&&	% t~~"&))&EEI
 y)DMM&&

IsT]]?RS {+ !>A(K[B\^fgkgugu^v>w:1T87 $GLL6$9:EKKekk7<<t}}ATU ')+d40 E
DJJO
 $IIaLIIaL +/ ?x: &
r1   r   c                     U R                   U R                  -
  nUS:  d  X:  a  UnU R                  R                  U5      nU =R                  [	        U5      -  sl        U$ )Nr>   )r   r    r   r?   rC   )r*   countblrN   s       r.   r?   DeltaApplyReader.read  sQ    ZZ$(("19
E ##E*CIr1   r[   c                     US:w  d  U[        [        SS5      :w  a  [        S5      eSU l        U R                  R                  S5        g)z`Allows to reset the stream to restart reading

:raise ValueError: If offset and whence are not 0r   r[   r]   N)r^   r_   r`   r    r   rd   ra   s      r.   rd   DeltaApplyReader.seek  s@     Q;&GB
A$>>:;;Qr1   c                     [        U5      S:  a  [        S5      eUS   R                  [        ;   a  [        SUS   R                  -  5      eU " U5      $ )a  
Convert the given list of streams into a stream which resolves deltas
when reading from it.

:param stream_list: two or more stream objects, first stream is a Delta
    to the object that you want to resolve, followed by N additional delta
    streams. The list's last stream must be a non-delta stream.

:return: Non-Delta OPackStream object whose stream can be used to obtain
    the decompressed resolved data
:raise ValueError: if the stream list cannot be handled   zNeed at least two streamsrx   zNCannot resolve deltas if there is no base object stream, last one was type: %s)rC   r`   type_idr   type)clsr   s     r.   rK   DeltaApplyReader.new  sb     {a899 r?""k1`cnoqcrcwcwwy y ;r1   c                 .    U R                   R                  $ r9   )r   r   r:   s    r.   r   DeltaApplyReader.type  s    }}!!!r1   c                 .    U R                   R                  $ r9   )r   r   r:   s    r.   r   DeltaApplyReader.type_id  s    }}$$$r1   c                     U R                   $ )z3:return: number of uncompressed bytes in the stream)r   r:   s    r.   r-   DeltaApplyReader.size  s     zzr1   )r    r   r   r   r   Nr   )ry   rz   r{   r|   r}   r~   k_max_memory_mover/   r   r   has_perf_modr6   r?   r^   r_   rd   r   rK   propertyr   r   r-   r   r   r1   r.   r   r   B  s    0I *  DH'V '3 #*"j!"<      4 " " % %  r1   r   c                   2    \ rS rSrSrSrS rS rS	S jrSr	g)
r   i,  zlSimple stream writer which produces a sha whenever you like as it degests
everything it is supposed to writesha1c                 "    [        5       U l        g r9   )r   r   r:   s    r.   r/   Sha1Writer.__init__2  s    J	r1   c                 N    U R                   R                  U5        [        U5      $ )zk:raise IOError: If not all bytes could be written
:param data: byte object
:return: length of incoming data)r   updaterC   r*   rN   s     r.   r   Sha1Writer.write7  s     
 			4yr1   c                 x    U(       a  U R                   R                  5       $ U R                   R                  5       $ )zU:return: sha so far
:param as_hex: if True, sha will be hex-encoded, binary otherwise)r   	hexdigestdigest)r*   as_hexs     r.   shaSha1Writer.shaD  s-     99&&((yy!!r1   )r   Nrw   )
ry   rz   r{   r|   r}   r~   r/   r   r   r   r   r1   r.   r   r   ,  s    *I
"r1   r   c                   (    \ rS rSrSrSrS rS rSrg)r   iN  zVWriter producing a sha1 while passing on the written bytes to the given
write functionwriterc                 :    [         R                  U 5        Xl        g r9   )r   r/   r   )r*   r   s     r.   r/   FlexibleSha1Writer.__init__T  s    D!r1   c                 P    [         R                  X5        U R                  U5        g r9   )r   r   r   r   s     r.   r   FlexibleSha1Writer.writeX  s    $Dr1   )r   N)	ry   rz   r{   r|   r}   r~   r/   r   r   r   r1   r.   r   r   N  s    Ir1   r   c                   V    \ rS rSrSrSrS rS rS rS r	\
" \SS	5      4S
 jrS rSrg)r   i]  z=Remembers everything someone writes to it and generates a sha)r   r   c                     [         R                  U 5        [        5       U l        [        R
                  " [        R                  5      U l        g r9   )r   r/   r   r   r(   compressobjZ_BEST_SPEEDr   r:   s    r.   r/   ZippedStoreShaWriter.__init__b  s1    D!9##D$5$56r1   c                 .    [        U R                  U5      $ r9   )r^   r   r4   s     r.   __getattr__ ZippedStoreShaWriter.__getattr__g  s    txx&&r1   c                     [         R                  X5      nU R                  R                  U R                  R	                  U5      5        U$ r9   )r   r   r   r   compress)r*   rN   alens      r.   r   ZippedStoreShaWriter.writej  s6    +txx((./r1   c                 j    U R                   R                  U R                  R                  5       5        g r9   )r   r   r   flushr:   s    r.   r   ZippedStoreShaWriter.closep  s    txx~~'(r1   r[   r   c                     US:w  d  U[        [        SS5      :w  a  [        S5      eU R                  R	                  S5        g)zXSeeking currently only supports to rewind written data
Multiple writes are not supportedr   r[   r]   N)r^   r_   r`   r   rd   ra   s      r.   rd   ZippedStoreShaWriter.seeks  s7     Q;&GB
A$>>:;;ar1   c                 6    U R                   R                  5       $ )zA:return: string value from the current stream position to the end)r   getvaluer:   s    r.   r   ZippedStoreShaWriter.getvalue{  s    xx  ""r1   N)ry   rz   r{   r|   r}   r~   r/   r   r   r   r^   r_   rd   r   r   r   r1   r.   r   r   ]  s6    GI7
') #*"j!"< #r1   r   c                   L   ^  \ rS rSrSrSr\" S5      rU 4S jrS r	S r
SrU =r$ )	r   i  zDigests data written to it, making the sha available, then compress the
data and write it to the file descriptor

**Note:** operates on raw file descriptors
**Note:** for this to work, you have to use the close-method of this instance)fdr   r   z+Failed to write all bytes to filedescriptorc                    > [         TU ]  5         Xl        [        R                  " [        R
                  5      U l        g r9   )superr/   r   r(   r   r   r   )r*   r   	__class__s     r.   r/   FDCompressedSha1Writer.__init__  s+    ##D$5$56r1   c                     U R                   R                  U5        U R                  R                  U5      n[	        U R
                  U5      nU[        U5      :w  a  U R                  e[        U5      $ )zR:raise IOError: If not all bytes could be written
:return: length of incoming data)r   r   r   r   r   r   rC   exc)r*   rN   cdatabytes_writtens       r.   r   FDCompressedSha1Writer.write  sY     			!!$'dggu-CJ&((N4yr1   c                     U R                   R                  5       n[        U R                  U5      [	        U5      :w  a  U R
                  e[        U R                  5      $ r9   )r   r   r   r   rC   r   r   )r*   	remainders     r.   r   FDCompressedSha1Writer.close  s@    HHNN$	)$I6((NTWW~r1   )r   r   )ry   rz   r{   r|   r}   r~   IOErrorr   r/   r   r   r   __classcell__)r   s   @r.   r   r     s1    U
 &I ?
@C7
 r1   r   c                   D    \ rS rSrSrSrS rS rSS jrS r	S r
S	 rSrg
)r   i  zA simple wrapper providing the most basic functions on a file descriptor
with the fileobject interface. Cannot use os.fdopen as the resulting stream
takes ownership_fd_posc                     Xl         SU l        g Nr   r   )r*   r   s     r.   r/   FDStream.__init__  s    	r1   c                     U =R                   [        U5      -  sl         [        R                  " U R                  U5        g r9   )r   rC   r_   r   r   r   s     r.   r   FDStream.write  s&    		SY	
4 r1   c                     US:X  a)  [         R                  R                  U R                  5      n[         R                  " U R
                  U5      nU =R                  [        U5      -  sl        U$ r   )r_   pathgetsize	_filepathr?   r   r   rC   )r*   r   bytess      r.   r?   FDStream.read  sL    A:GGOODNN3E %(		SZ	r1   c                     U R                   $ r9   )r   r:   s    r.   filenoFDStream.fileno  s    xxr1   c                     U R                   $ r9   )r   r:   s    r.   tellFDStream.tell  s    yyr1   c                 .    [        U R                  5        g r9   )r   r   r:   s    r.   r   FDStream.close  s    dhhr1   Nr   )ry   rz   r{   r|   r}   r~   r/   r   r?   r  r
  r   r   r   r1   r.   r   r     s,      I!r1   r   c                   <    \ rS rSrSr\" 5       rSS jrS rS r	Sr
g)	r   i  zRA stream that does nothing but providing a stream interface.
Use it like /dev/nullc                     g)N r   )r*   r-   s     r.   r?   NullStream.read  s    r1   c                     g r9   r   r:   s    r.   r   NullStream.close  s    r1   c                     [        U5      $ r9   )rC   r   s     r.   r   NullStream.write  s    4yr1   r   Nr   )ry   rz   r{   r|   r}   r   r~   r?   r   r   r   r   r1   r.   r   r     s    Ir1   r   )%ior   rW   r_   rn   r(   	gitdb.funr   r   r   r   r   
gitdb.utilr	   r
   r   r   r   gitdb.constr   r   gitdb.utils.encodingr   r   gitdb_speedups._perfr   r   ImportError__all__r   r   r   r   r   r   r   r   r   r1   r.   <module>r     s      	 
    . ,	AL%QY Qh`y `T" "D  #:  #F#Z #P D M  		s   B$ $B-,B-