
    nd$                       d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZmZ dd	lmZ dd
lmZmZ  ej        e          ZddiZ G d de          Z G d d          Z G d de          ZdS )zLazy ZIP over HTTP    )annotationsN)bisect_leftbisect_right)contextmanager)NamedTemporaryFile)AnyIterator)
BadZipfileZipFile)Session)CONTENT_CHUNK_SIZEResponsezAccept-Encodingidentityc                      e Zd ZdS )HTTPRangeRequestUnsupportedN)__name__
__module____qualname__     Blib/python3.11/site-packages/conda_package_streaming/lazy_wheel.pyr   r      s        Dr   r   c                      e Zd ZdZefd3d
Zed4d            Zed4d            Zd5dZ	d6dZ
ed5d            Zd7d8dZd5dZd9d:dZd;dZd<d=dZd5dZd>dZd?d#Zed@d%            Zd6d&ZefdAd-ZdBd1ZdCd2ZdS )DLazyZipOverHTTPa  File-like object mapped to a ZIP file over HTTP.

    This uses HTTP range requests to lazily fetch the file's content,
    which is supposed to be fed to ZipFile.  If such requests are not
    supported by the server, raise HTTPRangeRequestUnsupported
    during initialization.
    urlstrsessionr   
chunk_sizeintreturnNonec                @   d| _         |||c| _        | _        | _        |                     dt
                    }|j        dk    rt          d          t          |j	        d         
                    d          d                   | _        t                      | _        |                     | j                   |                                 5  t          |j	        d	                   }t!          |d
          r|t#          |j                  k    sJ |                     | j        |z
             |                    | j                  D ]}| j                            |           	 d d d            n# 1 swxY w Y   | j        |z
  g| _        | j        dz
  g| _        d S )Nr    )startend   zrange request is not supportedzcontent-range/zcontent-lengthcontent   )_request_count_session_url_chunk_size_stream_responser   status_coder   r   headers	partition_lengthr   _filetruncate_stayhasattrlenr(   seekiter_contentwrite_left_right)selfr   r   r   tailcontent_lengthchunks          r   __init__zLazyZipOverHTTP.__init__&   s     5<c:2ty$"2 $$23E$FF
 s""-.NOOO 4<8BB3GGKLL'))
dl### ZZ\\ 	( 	( .>!?@@NtY'' ;%T\):):::::IIdl^3444**4+;<< ( (
  ''''(	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( "&!> ?
"&,"2!3s   BE33E7:E7c                    dS )z!Opening mode, which is always rb.rbr   r=   s    r   modezLazyZipOverHTTP.modeJ   s	     tr   c                    | j         j        S )zPath to the underlying file.)r3   namerD   s    r   rG   zLazyZipOverHTTP.nameO   s     zr   boolc                    dS )z9Return whether random access is supported, which is True.Tr   rD   s    r   seekablezLazyZipOverHTTP.seekableT       tr   c                8    | j                                          dS )zClose the file.N)r3   closerD   s    r   rM   zLazyZipOverHTTP.closeX   s    
r   c                    | j         j        S )zWhether the file is closed.)r3   closedrD   s    r   rO   zLazyZipOverHTTP.closed\   s     z  r   r'   sizebytesc                    |}|                                  | j        }}|dk     r|nt          ||z   |          }t          d||z
            }|                     ||dz
             | j                            |          S )zRead up to size bytes from the object and return them.

        As a convenience, if size is unspecified or -1,
        all bytes until EOF are returned.  Fewer than
        size bytes may be returned if EOF is reached.
        r   r)   )tellr2   minmax	_downloadr3   read)r=   rP   download_sizer#   lengthstops         r   rW   zLazyZipOverHTTP.reada   s|     		T\vvvs5=+@&'I'IAtm+,,udQh'''zt$$$r   c                    dS )z3Return whether the file is readable, which is True.Tr   rD   s    r   readablezLazyZipOverHTTP.readablep   rK   r   r   offsetwhencec                8    | j                             ||          S )a-  Change stream position and return the new absolute position.

        Seek to offset relative position indicated by whence:
        * 0: Start of stream (the default).  pos should be >= 0;
        * 1: Current position - pos may be negative;
        * 2: End of stream - pos usually negative.
        )r3   r8   )r=   r]   r^   s      r   r8   zLazyZipOverHTTP.seekt   s     zvv...r   c                4    | j                                         S )zReturn the current position.)r3   rS   rD   s    r   rS   zLazyZipOverHTTP.tell~   s    z   r   N
int | Nonec                6    | j                             |          S )zResize the stream to the given size in bytes.

        If size is unspecified resize to the current position.
        The current stream position isn't changed.

        Return the new file size.
        )r3   r4   )r=   rP   s     r   r4   zLazyZipOverHTTP.truncate   s     z""4(((r   c                    dS )zReturn False.Fr   rD   s    r   writablezLazyZipOverHTTP.writable   s    ur   c                8    | j                                          | S N)r3   	__enter__rD   s    r   rg   zLazyZipOverHTTP.__enter__   s    
r   excr   bool | Nonec                      | j         j        | S rf   )r3   __exit__)r=   rh   s     r   rk   zLazyZipOverHTTP.__exit__   s    "tz"C((r   Iterator[None]c              #     K   |                                  }	 dV  |                     |           dS # |                     |           w xY w)zyReturn a context manager keeping the position.

        At the end of the block, seek back to original position.
        N)rS   r8   )r=   poss     r   r5   zLazyZipOverHTTP._stay   sJ       iikk	EEEIIcNNNNNDIIcNNNNs	   3 A
c                J   | j         dz
  }t          t          d|| j                            D ]t}|                     ||           |                                 5  	 t          |            	 ddd            dS # t          $ r Y nw xY w	 ddd           n# 1 swxY w Y   udS )z1Check and download until the file is a valid ZIP.r)   r   N)r2   reversedranger-   rV   r5   r   r
   )r=   r$   r#   s      r   
_check_zipzLazyZipOverHTTP._check_zip   s   lQeAsD,<==>> 
	 
	ENN5#&&&   DMMM         
 "   D              
	 
	s6   BA:+B:
BBBBB	B	r#   	int | strr$   base_headersdict[str, str]r   c                   |                                 }d| d| |d<   t                              d|d                    d|d<   | xj        dz  c_        | j                            | j        |d	          }|                                 |S )
zReturn HTTP response to a range request from start to end.

        :param start: if "", request ``end` bytes from end of file.zbytes=-Rangez%szno-cachezCache-Controlr)   T)r0   stream)copylogdebugr*   r+   getr,   raise_for_status)r=   r#   r$   rt   r0   responses         r   r.   z LazyZipOverHTTP._stream_response   s     ##%%1E11C11		$()))#- q =$$TY$MM!!###r   leftrightIterator[tuple[int, int]]c              #  X  K   | j         ||         | j        ||         }}t          |g|dd         z             x}}t          |g|dd         z             }t	          ||          D ]\  }}	||k    r	||dz
  fV  |	dz   }||k    r||fV  |g|gc| j         ||<   | j        ||<   dS )a/  Return an iterator of intervals to be fetched.

        Args:
            start (int): Start of needed interval
            end (int): End of needed interval
            left (int): Index of first overlapping downloaded data
            right (int): Index after last overlapping downloaded data
        Nr)   r'   )r;   r<   rT   rU   zip)
r=   r#   r$   r   r   lslicersliceijks
             r   _mergezLazyZipOverHTTP._merge   s       DJ/T%Z1H&!*,---E3%&+%&&'' 	 	DAq1uuQhAAA88S&LLL;@'C57
4:DJ 7 7 7r   c                   |                                  5  t          | j        |          }t          | j        |          }|                     ||||          D ]g\  }}|                     ||          }|                     |           |                    | j	                  D ]}| j
                            |           h	 ddd           dS # 1 swxY w Y   dS )z-Download bytes from start to end inclusively.N)r5   r   r<   r   r;   r   r.   r8   r9   r-   r3   r:   )r=   r#   r$   r   r   r   r@   s          r   rV   zLazyZipOverHTTP._download   s%   ZZ\\ 	, 	,t{E22D S11E"kk%dEBB , ,
s00<<		%   %2243CDD , ,EJ$$U++++,,	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   B+CCC)r   r   r   r   r   r   r   r    )r   r   )r   rH   )r   r    )r'   )rP   r   r   rQ   )r   )r]   r   r^   r   r   r   )r   r   rf   )rP   ra   r   r   )r   r   )rh   r   r   ri   )r   rl   )r#   rs   r$   r   rt   ru   r   r   )
r#   r   r$   r   r   r   r   r   r   r   )r#   r   r$   r   r   r    )r   r   r   __doc__r   rA   propertyrE   rG   rJ   rM   rO   rW   r\   r8   rS   r4   rd   rg   rk   r   r5   rr   HEADERSr.   r   rV   r   r   r   r   r      s         =O"4 "4 "4 "4 "4H    X    X       ! ! ! X!% % % % %   / / / / /! ! ! !) ) ) ) )      ) ) ) ) 	 	 	 ^	     JQ     I I I I,	, 	, 	, 	, 	, 	,r   r   c                      e Zd Zd ZdS )	LazyCondac                0   d| d}|                                  5  t          j        |           }|                                }t	          |          D ]\  }}|j        |k    r|j        }	 ||dz            j        }n# t          $ r
 |j        }Y nw xY w| 	                    |           | 
                    ||z
             t                              d|j        |            nt                              d           ddd           dS # 1 swxY w Y   dS )a  
        Conda fork specific. Prefetch the `.info` range from the remote archive.
        Reduces number of Range requests to 2 or 3 (1 or 2 for the directory, 1
        for the file).

        conda_file_id: name of .conda without path or `.conda` extension
        zinfo-z.tar.zstr)   zprefetch %s-%szno zip prefetchN)r5   zipfiler   infolist	enumeratefilenameheader_offset
IndexError	start_dirr8   rW   r{   r|   )	r=   conda_file_idtarget_filezfr   r   infor#   r$   s	            r   prefetchzLazyConda.prefetch   s~    6m555ZZ\\ 	- 	-&&B{{}}H$X.. - -4=K// !.E+&q1uo; & + + + l+IIe$$$IIcEk***II(*  
 E) 0, 		+,,,7	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s7   AD+A<;D<BDBA.DDDN)r   r   r   r   r   r   r   r   r      s#        $- $- $- $- $-r   r   )r   
__future__r   loggingr   bisectr   r   
contextlibr   tempfiler   typingr   r	   r
   r   requestsr   requests.modelsr   r   	getLoggerr   r{   r   	Exceptionr   r   r   r   r   r   <module>r      st     " " " " " "   , , , , , , , , % % % % % % ' ' ' ' ' '                 ' ' ' ' ' ' ' '       8 8 8 8 8 8 8 8
 g!! j
)	 	 	 	 	) 	 	 	D, D, D, D, D, D, D, D,N%- %- %- %- %- %- %- %- %- %-r   