
    <Bh>                    X   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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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#  \RH                  " \%5      r&Sr'Sr(Sr)Sr*Sr+Sr,1 Skr-S r. " S S\/5      r0 " S S\/5      r1 " S S \25      r3S/S0S! jjr4S1S" jr5 S2 S3S# jjr6S$ r7S% r8S& r9S' r:\
S( 5       r;S4S) jr< " S* S+\Rz                  5      r>  S5       S6S, jjr?S7S- jr@ S8         S9S. jjrAg):zJLAP consumer.    )annotationsN)contextmanager)blake2b)TYPE_CHECKING)	HTTPError)mask_anaconda_token   )context   )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)JlapSkipZstD   r'   Nr.   r'   r(   r)   r6   r6   D   r4   r(   r6   c                      \ rS rSrSrg)JlapPatchNotFoundH   r'   Nr.   r'   r(   r)   r9   r9   H   r4   r(   r9   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>.linesR   s     &&&777s   )r   ivposfooter)returnzIterator[bytes])
status_coder,   r   
from_linesjsonloadsr   itemslowerSTORE_HEADERS)
rB   rI   rH   rC   bufferrJ   _kv	new_states
   `         r)   process_jlap_responserX   L   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)rI   r   ignore_etagsession)rI   rH   )request_jlaprX   )urlrI   r   rH   rZ   r[   rB   s          r)   
fetch_jlapr^   j   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   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 %sT)streamr   timeoutzrequest headers: %szresponse without request.zresponse headers: %sz
status: %d)   r<   i    z+Unexpected response code for range request rA   z%s)logdebugr   r
   remote_connect_timeout_secsremote_read_timeout_secsgetraise_for_statusrequestpprintpformatr   rP   rQ   rR   rL   r   info)
r]   rI   r   rZ   r[   r   re   rB   rU   rV   s
             r)   r\   r\   q   sy    G
#C5?K#' IIg*3/9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   ,GGc                    U SS S-   $ )zAbbreviate hash for formatting.N   u   …r'   )r*   s    r)   format_hashrt      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   rt   r9   )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 )	Nrw       → rv   z, r~   z stepsT)in_place)poprh   ri   rt   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   )existsrl   )	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)r/   r0   r1   r2   r   r   r   r3   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
   rj   rk   rl   rh   ri   r   rm   rL   	zstandardZstdDecompressorstream_writerr   openiter_contentr   rn   intKeyError
ValueErrorAttributeErrorrq   )r   r]   r   r[   r   is_zst	dest_pathr   re   rB   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   )rB   rL   )erL   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:X  d   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NVS 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! [p        [:        Rr                  4 aY  n['        U[p        5      (       a  [(        RK                  S15        U(       a   S25       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)3Nr   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   )rG    r   r   r   )r   r   r   rH   rI   z.jlapzFetch %s from iv=%s, pos=%s)rI   r   rH   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   )rN   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-8zICurrent repodata.json %s not found in patchset. Re-download repodata.jsonzRecursion error):rl   JLAP_KEYHEADERScache_path_jsonr   NOMINAL_HASHr   should_check_formatr*   r   r   r   r   r   r   r6   r   r   	ZstdError
isinstancerh   warningr   r   set_has_formatr   rL   	hexdigestON_DISK_HASHrN   dumpsLATESTri   r^   r   fromhexr   rq   
IndexError	exceptionrequest_url_jlap_staterB   listbodypenultimaterO   r   r   rt   loadr   r   r   r   r   encoder9   JSONDecodeError)r]   r   r   r[   r   r   
jlap_stater   r   rS   r   rB   r   r{   	need_jlapiv_hexrI   r   jlap_urlrz   rT   rJ   r|   r}   repodata_jsonr   s                             r)   r   r     su    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 
"	!^ \*d222_ 
"	!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 	!.//
 _ %7&77$)"# { 
"	!b	c 
"	!s;  7U)/R U)
RAU)"BU; X4 1A]7A['AZ'-['?['[0A)Z9	["	['5[']U&-B.U!U)!U&&U))
U8;X1X1>	X1/X,6X1<*X,,X14Z$AZZ$'
Z6	1['9
[[
[']
[$	['"]$['']A]]]]]
]+)r   r(   )rB   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   rK   bool)F)
r   r   r[   r   r   r   r   r   rK   zdict | None)B__doc__
__future__r   iorN   loggingro   r   r   
contextlibr   hashlibr   typingr   r   r   requestsr   conda.common.urlr   base.contextr
   r   r   r   r   corer   pathlibcollections.abcr   
connectionr   r   r   	getLoggerr/   rh   r&   r   r   r   r   r   rR   r*   	Exceptionr,   r6   LookupErrorr9   rX   r^   r\   rt   r   r   r   r   r   	RawIOBaser   r   r   r   r'   r(   r)   <module>r
     s    " 	    	  %       0 $ 9 9 (/ ! 
#		,
	 		) 		 	<; HL)6D)X1
 N) 6 6
 
& %)* * 	*
  * #*Z; QQ
 Q Q Q Qr(   