
    si?                    \   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  SSKrSSKrSSKJr  SS	KJr  SS
KJr  SSKJr  SSKJrJrJr  SSKJr  \(       a  SSKrSSKJ r   SSK!J"r"J#r#  SSKJ$r$  \RJ                  " \&5      r'Sr(Sr)Sr*Sr+Sr,Sr-1 Skr.S r/ " S S\05      r1 " S S\05      r2 " S  S!\35      r4S0S1S" jjr5S2S# jr6 S3 S4S$ jjr7S% r8S& r9S' r:S( r;\	S) 5       r<S5S* jr= " S+ S,\R|                  5      r?  S6       S7S- jjr@S8S. jrA S9         S:S/ jjrBg);zJLAP consumer.    )annotationsN)contextmanager)blake2b)TYPE_CHECKING)	HTTPError   )context)json)mask_anaconda_token   )ETAG_KEYLAST_MODIFIED_KEYRepodataState   )JLAP)Iterator   )ResponseSession)RepodataCache    jlapheadersblake2_256_nominal
blake2_256latest>   dateetagcontent-typecache-controlcontent-rangelast-modifiedcontent-lengthcontent-encodingc                     [        [        S9$ )zOrdinary hash.)digest_size)r   DIGEST_SIZE     Blib/python3.13/site-packages/conda/gateways/repodata/jlap/fetch.pyhashr+   :   s    {++r)   c                      \ rS rSrSrg)Jlap304NotModified?   r(   N__name__
__module____qualname____firstlineno____static_attributes__r(   r)   r*   r-   r-   ?       r)   r-   c                      \ rS rSrSrg)JlapSkipZstC   r(   Nr/   r(   r)   r*   r7   r7   C   r5   r)   r7   c                      \ rS rSrSrg)JlapPatchNotFoundG   r(   Nr/   r(   r)   r*   r:   r:   G   r5   r)   r:   c                  ^  T R                   S:X  a
  [        5       eSU 4S jjn[        R                  " U" 5       X!5      nUS   u  pn[        R
                  " U5      nT R                  R                  5        VVs0 s H0  u  pxUR                  5       [        ;   d  M  UR                  5       U_M2     snnUS   S   UUS.n	XI4$ s  snnf )N0  c               3  @   >#    T R                  SS9 S h  vN   g  N7f)N   
)	delimiter)
iter_linesresponses   r*   lines$process_jlap_response.<locals>.linesQ   s     &&&777s   )r   ivposfooter)returnzIterator[bytes])
status_coder-   r   
from_linesr
   loadsr   itemslowerSTORE_HEADERS)
rC   rJ   rI   rD   bufferrK   _kv	new_states
   `         r*   process_jlap_responserX   K   s     s" ""8 __UWb.F BZNCZZF !((..0
0wwyM) AGGIqL0

 Rjn
I 
s   ;CCc                ,    [        XX$US9n[        XaUS9$ )N)rJ   r   ignore_etagsession)rJ   rI   )request_jlaprX   )urlrJ   r   rI   rZ   r[   rC   s          r*   
fetch_jlapr^   i   s"    4'H !r::r)   c                   0 nU(       a	  SU S3US'   U(       a  U(       d  X%S'   [         R                  S[        U 5      U5        Uc  [        S5      e[        R
                  [        R                  4nUR                  U SXVS9nUR                  5         UR                  (       a?  [         R                  S	[        R                  " UR                  R                  5      5        O[         R                  S
5        [         R                  S[        R                  " UR                  R                  5        VV	s0 s H!  u  pUR                  5       [        ;   d  M  X_M#     sn	n5      5        [         R                  SUR                   5        SU;   a'  UR                   S;  a  [#        SUR                    3US9e[         R%                  SU5        U$ s  sn	nf )z>Return the part of the remote .jlap file we are interested in.zbytes=-rangeif-none-match%s %szsession cannot be NoneT)streamr   timeoutzrequest headers: %szresponse without request.zresponse headers: %sz
status: %d)   r=   i    z+Unexpected response code for range request rB   z%s)logdebugr   RuntimeErrorr	   remote_connect_timeout_secsremote_read_timeout_secsgetraise_for_statusrequestpprintpformatr   rP   rQ   rR   rM   r   info)
r]   rJ   r   rZ   r[   r   re   rC   rU   rV   s
             r*   r\   r\   p   s~    G
#C5?K#' IIg*3/93441173S3SSG{{3tW{NH		'8H8H8P8P)QR		-.II&..446U6da!'')}:TTQT6U	
 IIlH001' ';;=h>R>R=ST! 
 HHT8O! Vs   6GGc                    U SS S-   $ )zAbbreviate hash for formatting.N   u   …r(   )r+   s    r*   format_hashru      s    9000r)   c                    / n[        U 5       H+  nX:X  a    O%US   U:X  d  M  UR                  U5        US   nM-     X:w  a8  [        R                  S[	        U5       35        [        S[	        U5       35      eU$ )NtofromzNo patch from local revision )reversedappendrh   ri   ru   r:   )patcheshavewantapplypatchs        r*   find_patchesr      s~    E'"<;$LL=D # |		1+d2C1DEF"?D@Q?R STTLr)   c           
        U(       a  UR                  5       n[        R                  [        US   5       S[        US   5       S[	        US   5       S35        [
        R                  " US   5      R                  U SS9n U(       a  M  g g )	Nrx       → rw   z, r   z stepsT)in_place)poprh   ri   ru   len	jsonpatch	JsonPatchr~   )datar~   r   s      r*   apply_patchesr      s    
				5=)**@USW[AY@ZZ\5>"#6+	
 ""5>2888M %r)   c                0    [         R                  " SX5      $ )Nz(\.\w+)$)resub)r]   exts     r*   withextr      s    66+s((r)   c              #     #    [         R                  " 5       nS v   [         R                  " 5       n[        R                  SXU-
  5        g 7f)Nz%sTook %0.02fs)time	monotonicrh   ri   )messagebeginends      r*   timemer      s3     NNE	
..
CIIu5s   A	Ac                l    0 nU R                  5       (       a  UR                  S5      nU(       a  X2S'   U$ )z%Caching headers for a path and state._etagrb   )existsrm   )	json_pathstater   r   s       r*   build_headersr      s4    Gyy!'+O$Nr)   c                  *    \ rS rSrS rSS jrS rSrg)
HashWriter   c                    Xl         X l        g Nbackinghasher)selfr   r   s      r*   __init__HashWriter.__init__   s    r)   c                n    U R                   R                  U5        U R                  R                  U5      $ r   )r   updater   write)r   bs     r*   r   HashWriter.write   s)    1||!!!$$r)   c                8    U R                   R                  5         g r   )r   close)r   s    r*   r   HashWriter.close   s    r)   r   N)r   bytes)r0   r1   r2   r3   r   r   r   r4   r(   r)   r*   r   r      s    %r)   r   c                X   Uc  UnU=(       d
    [        5       n[        X$5      n[        R                  [        R                  4nUR                  USXS9n	[        R                  SXR                  5        U	R                  5         Sn
U	R                  S:X  a  U(       a?  [        R                  " 5       nUR                  [        UR                  S5      U 5      SS9nO[        UR                  S5      U 5      nU nU	R!                  S	S
9 H  nUR#                  U5        M     SSS5        U	R$                  (       aC   ['        U	R                  S   5      n
[        R/                  SXR$                  R                  5        U	$ ! , (       d  f       Nd= f! [(        [*        [,        4 a     NTf = f)zDownload url if it doesn't exist, passing bytes through hasher.update().

json_path: Path of old cached data (ignore etag if not exists).
dest_path: Path to write new data.
NT)rd   re   r   rc   r      wb)closefdi @  )
chunk_sizezContent-LengthzDownload %d bytes %r)r   r   r	   rk   rl   rm   rh   ri   r   rn   rM   	zstandardZstdDecompressorstream_writerr   openiter_contentr   ro   intKeyError
ValueErrorAttributeErrorrr   )r   r]   r   r[   r   is_zst	dest_pathr   re   rC   lengthdecompressorwriterrepodatablocks                  r*   download_and_hashr      sn    	$]_EI-G1173S3SSG{{3tW{NHIIgs,,-Fs"$557L!//9>>$/8 0 F
  	t 4f=Fx!..'.Bu% C  	))*:;<F 	'1A1A1I1IJO V *n5 		s   :(F <F  
FF)(F)c                    U R                   c  gU R                   R                  nSUs=:*  =(       a    S:  Os  =(       a    US:g  $ )zO
Determine whether the `HTTPError` is an HTTP 400 error code (except for 416).
Fi  i  rg   )rC   rM   )erM   s     r*   _is_http_error_most_400_codesr     s=     	zz**((K+###:s(::r)   c                  UR                  [        0 5      nUR                  [        0 5      nUR                  n[	        5       n	U(       d6  [
        U;   a,  UR                  5       (       a  UR                  S5      (       GdV  [        5       n
[        SU  S35         [
        U;  a,  UR                  [        S 5        UR                  [        S 5         UR                  S5      (       a  [        U
[        U S5      UUUUSS9nO
[        5       eUR2                  R                  S5      US'   UR2                  R                  S5      US'   UR2                  R                  S5      US'   S S S 5        WR4                  S:X  a   U
R7                  5       =U[
        '   U[8        '   U[
           n[	        / SQS[:        R<                  " [>        U05      S// SQ/5      n	OU[
           nSn UR                  SS5      nUR                  SS5      nUR                  [        S 5      n[        U S5      n[(        RA                  S[-        U5      UU5        [C        UUU[D        RG                  U5      US	S9u  pUR1                  SS5        S	nU(       a   [C        [        U S5      US 9u  pXa[        '   [        S$5         [U        S% U	RV                   5       5      nU	RX                  u  nnn[:        RZ                  " U5      S&   n []        UUU5      n[(        RK                  S'[_        U5       S([a        U5       S)[a        U5       35        U(       Ga0  [        S*5         [:        RZ                  " URc                  5       5      nXRd                  R                  [
        5      :w  a'  [(        R+                  S+5         S S S 5        S S S 5        g  S S S 5        [g        WU5        [        S,5         URi                  S-5       n[        5       n
[k        UU
5      Rm                  [:        R<                  " US.S/9Ro                  S05      5        U
R7                  5       U[8        '   UU[
        '   UsS S S 5        sS S S 5        sS S S 5        $ U[
           U:w  a  [q        [
         S1U S2U[
            S335      e S S S 5        g ! [        [         ["        R$                  4 a  n['        U["        R$                  5      (       a*  [(        R+                  S[-        [        U S5      5      U5        ['        U[         5      (       a  [/        U5      (       d  e ['        U[        5      (       d  UR1                  SS	5        [        U
[        U S
5      UUUUS9n S nAGNtS nAff = f! , (       d  f       GN)= f! [H         a    [(        RK                  S5         GN;[L         a    [(        RO                  S5         GN\[          aj  n[/        U5      (       a%  UR1                  SS	5        [Q        U USUUUS9s S nA$ [(        RK                  SURR                  R4                  5         S nAGNS nAff = f! [H        [L        4 a`  n[(        RO                  S!US"9  [	        / SQS[:        R<                  " [>        S#05      S// SQ/5      n	UR1                  SS	5         S nAGNS nAff = f! , (       d  f       GN= f! , (       d  f       O= fS S S 5        GMP  ! , (       d  f       GN_= f! [r        [:        Rt                  4 a\  n['        U[r        5      (       a  [(        RK                  S45        U(       a
  [w        5       e[Q        U USUUUS9s S nAsS S S 5        $ S nAff = f! , (       d  f       g = f)5Nr   zDownload complete  zstz	.json.zstT)r   r[   r   r   z9Could not decompress %s as zstd. Fall back to .json. (%s)Fz.json)r   r[   r   r"   _modr   r   r    _cache_controlr   )rH    r   r   r   )r   r   r   rI   rJ   z.jlapzFetch %s from iv=%s, pos=%s)rJ   r   rI   r[   rZ   z@Checksum not OK on JLAP range request. Retry with complete JLAP.z&IndexError reading JLAP. Invalid file?)full_downloadr[   cache	temp_pathzAResponse code %d on JLAP range request. Retry with complete JLAP.)r[   zError parsing jlap)exc_info 00000000000000000000000000000000zApply Patches c              3  T   #    U  H  u  pn[         R                  " U5      v   M      g 7fr   )r
   rO   ).0rT   r   s      r*   	<genexpr>)request_url_jlap_state.<locals>.<genexpr>  s      HK[Qqtzz%((Ks   &(r   zApply z	 patches r   zLoad z)repodata cache changed during jlap fetch.zWrite changed r   ),:)
separatorszutf-8z mismatch. Expected z, got .zICurrent repodata.json %s not found in patchset. Re-download repodata.json)<rm   JLAP_KEYHEADERScache_path_jsonr   NOMINAL_HASHr   should_check_formatr+   r   r   r   r   r   r   r7   r   r   	ZstdError
isinstancerh   warningr   r   set_has_formatr   rM   	hexdigestON_DISK_HASHr
   dumpsLATESTri   r^   r   fromhexr   rr   
IndexError	exceptionrequest_url_jlap_staterC   listbodypenultimaterO   r   r   ru   loadr   r   r   r   r   encoderj   r:   JSONDecodeErrorRecursionError)r]   r   r   r[   r   r   
jlap_stater   r   rS   r   rC   r   r|   	need_jlapiv_hexrJ   r   jlap_urlr{   rT   rK   r}   r~   repodata_jsonr   s                             r*   r   r     s    8R(JnnWb)G%%IVF 	%)*:*:*<*<((00(Q/0 5(		(D)		+T2 ,,U330[1!"+ '## H &-'. %,,00AE&M%--11&9E'N&.&6&6&:&:?&KE"#Y 1^ 3&8>8H8H8JJE,%"5\" |aVTN)CR%H+VW \" 	*	^^D"-F..*C;;x.DsG,HII-#H-	 ",==(!"F   .I. 4%/W0Ew%W" %h	 	! HFKKHH))61zz&!(+>	 $5EHHUIt$%%;K<M;NP
 G_ %)JJuzz|$<M {{|<<$OP# %% 
"	!4 = % mU3,-y~~d/Cx!VFx066

=ZHOOPWX
 +1*:*:*<E,' +/E,' ) 0D/C--A 
"	!^ &$.&'.(<TF&|I\H]]^_  /_ 
"	!I  I,?,?@ a!4!455KKS+GC,EF
 a++4QRS4T4T!![11((6,C)'#) 10j  	YHHWX 	DMMBC 	 -Q//$$VU3-"&#'  HHS

&& 	* 
+ 42Q?!Atzz682D'Er#JKX $$VU3348 %_ 0D/C----( "4#7#78 	!.//
 _ $&&)"# C 
"	!h	i 
"	!s;  7V/R, V
R,AV"BV Y 1A]>8A\A[.\ \[31A)[	[3#	\6,\"]>,VB.U?9V?VV
VY;Y	Y%/Y
Y*Y

Y["AZ==[
[	\
[%![3(\0]>3
\	=\ ]>\];A]6'];(]>6];;]>>
^)r   r)   )rC   r   )r   Nr)   TN)r   NTN)r[   zSession | None)r   pathlib.Pathr   r   )FN)r   r   r[   r   r   zRepodataState | Noner   zpathlib.Path | None)r   r   rL   bool)F)
r   r   r[   r   r   r   r   r   rL   zdict | None)C__doc__
__future__r   iologgingrp   r   r   
contextlibr   hashlibr   typingr   r   r   requestsr   base.contextr	   common.serializer
   
common.urlr   r   r   r   r   corer   pathlibcollections.abcr   
connectionr   r   r   	getLoggerr0   rh   r'   r   r   r   r   r   rR   r+   	Exceptionr-   r7   LookupErrorr:   rX   r^   r\   ru   r   r   r   r   r   	RawIOBaser   r   r   r   r(   r)   r*   <module>r     s    " 	   	  %       $ % . 9 9 (/ ! 
#		,
	 		) 		 	<; HL*6D*Z1
 N) 6 6
 
& %)* * 	*
  * #*Z; UU
 U U U Ur)   