
    wBf>                    8   d Z ddlmZ 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 ddlmZ ddlZddlZddlmZ ddlmZ d	d
lmZ ddlmZmZmZ ddlmZ erddlZddlmZ ddlm Z m!Z! ddlm"Z"  ejF                  e$      Z%dZ&dZ'dZ(dZ)dZ*dZ+h dZ,d Z- G d de.      Z/ G d de.      Z0 G d d e1      Z2d/d0d!Z3d1d"Z4	 d2	 d3d#Z5d$ Z6d% Z7d& Z8d' Z9e
d(        Z:d4d)Z; G d* d+ejx                        Z=	 	 d5	 	 	 	 	 	 	 d6d,Z>d7d-Z?	 d8	 	 	 	 	 	 	 	 	 d9d.Z@y):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>   content-typecontent-rangecontent-lengthcontent-encodingdateetagcache-controllast-modifiedc                 "    t        t              S )zOrdinary hash.)digest_size)r   DIGEST_SIZE     Blib/python3.12/site-packages/conda/gateways/repodata/jlap/fetch.pyhashr*   ;   s    {++r(   c                      e Zd Zy)Jlap304NotModifiedN__name__
__module____qualname__r'   r(   r)   r,   r,   @       r(   r,   c                      e Zd Zy)JlapSkipZstNr-   r'   r(   r)   r3   r3   D   r1   r(   r3   c                      e Zd Zy)JlapPatchNotFoundNr-   r'   r(   r)   r5   r5   H   r1   r(   r5   c                     j                   dk(  r
t               d fd}t        j                   |       ||      }|d   \  }}}t	        j
                  |      } j                  j                         D ci c],  \  }}|j                         t        v r|j                         |. c}}|d   d   ||d}	||	fS c c}}w )N0  c               3  F   K    j                  d      E d {    y 7 w)N   
)	delimiter)
iter_linesresponses   r)   linesz$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)
r=   rC   rB   r>   bufferrD   _kv	new_states
   `         r)   process_jlap_responserR   L   s     s" ""8 __UWb#.F BZNCZZF !((..0
1wwyM) GGIqL

 Rjn
I 9
s   81B<c                >    t        | ||||      }t        |||      S )N)rC   r!   ignore_etagsession)rC   rB   )request_jlaprR   )urlrC   r!   rB   rT   rU   r=   s          r)   
fetch_jlaprX   j   s(    4['H !sr::r(   c                j   i }|r	d| d|d<   |r|s||d<   t         j                  dt        |       |       |J t        j                  t        j
                  f}|j                  | d||      }|j                          |j                  r>t         j                  dt        j                  |j                  j                               nt         j                  d	       t         j                  d
t        j                  |j                  j                         D 	ci c]  \  }}	|j                         t        v s||	! c}	}             t         j                  d|j                         d|v r(|j                  dvrt!        d|j                   |      t         j#                  d|       |S c c}	}w )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)   r7   i    z+Unexpected response code for range request r<   z%s)logdebugr   r
   remote_connect_timeout_secsremote_read_timeout_secsgetraise_for_statusrequestpprintpformatr   rJ   rK   rL   rF   r   info)
rW   rC   r!   rT   rU   r   r_   r=   rO   rP   s
             r)   rV   rV   q   sy    G
#C5?K#' IIg*3/91173S3SSG{{3tWg{NH		'8H8H8P8P)QR		-.II&..446Uda!'')}:TQTU	
 IIlH001' ';;=h>R>R=ST! 
 HHT8O! Vs   F/:F/c                    | dd dz   S )zAbbreviate hash for formatting.N   u   …r'   )r*   s    r)   format_hashrn      s    9000r(   c                    g }t        |       D ](  }||k(  r n!|d   |k(  s|j                  |       |d   }* ||k7  r8t        j                  dt	        |              t        dt	        |             |S )NtofromzNo patch from local revision )reversedappendrb   rc   rn   r5   )patcheshavewantapplypatchs        r)   find_patchesry      s    E'" !4<;$LL=D! t|		1+d2C1DEF"?D@Q?R STTLr(   c           
        |r|j                         }t        j                  t        |d          dt        |d          dt	        |d          d       t        j                  |d         j                  | d      } |r~y y )	Nrq       → rp   z, rx   z stepsT)in_place)poprb   rc   rn   len	jsonpatch	JsonPatchrw   )datarw   rx   s      r)   apply_patchesr      s    
				5=)**@USW[AY@ZZ\5>"#6+	
 ""5>2888M r(   c                0    t        j                  d||       S )Nz(\.\w+)$)resub)rW   exts     r)   withextr      s    66+sC((r(   c              #     K   t        j                         }d  t        j                         }t        j                  d| ||z
         y w)Nz%sTook %0.02fs)time	monotonicrb   rc   )messagebeginends      r)   timemer      s6     NNE	
..
CIIu5s   AA
c                Z    i }| j                         r|j                  d      }|r||d<   |S )z%Caching headers for a path and state._etagr\   )existsrf   )	json_pathstater   r!   s       r)   build_headersr      s3    Gyy!'+GO$Nr(   c                       e Zd Zd ZddZd Zy)
HashWriterc                     || _         || _        y N)backinghasher)selfr   r   s      r)   __init__zHashWriter.__init__   s    r(   c                n    | j                   j                  |       | j                  j                  |      S r   )r   updater   write)r   bs     r)   r   zHashWriter.write   s)    1||!!!$$r(   c                8    | j                   j                          y r   )r   close)r   s    r)   r   zHashWriter.close   s    r(   N)r   bytes)r.   r/   r0   r   r   r   r'   r(   r)   r   r      s    %r(   r   c                @   ||}|xs
 t               }t        ||      }t        j                  t        j                  f}|j                  |d||      }	t        j                  d||	j                         |	j                          d}
|	j                  dk(  r|rAt        j                         }|j                  t        |j                  d      |       d      }nt        |j                  d      |       }|5 }|	j!                  d	
      D ]  }|j#                  |        	 ddd       |	j$                  rD	 t'        |	j                  d         }
t        j/                  d|
|	j$                  j                         |	S # 1 sw Y   [xY w# t(        t*        t,        f$ r Y Ow xY w)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)r^   r_   r   r]   r      wb)closefdi @  )
chunk_sizezContent-LengthzDownload %d bytes %r)r   r   r
   rd   re   rf   rb   rc   r   rg   rF   	zstandardZstdDecompressorstream_writerr   openiter_contentr   rh   intKeyError
ValueErrorAttributeErrorrk   )r   rW   r   rU   r   is_zst	dest_pathr   r_   r=   lengthdecompressorwriterrepodatablocks                  r)   download_and_hashr      s    	$]_EIu-G1173S3SSG{{3tWg{NHIIgsH,,-Fs"$557L!//9>>$/8 0 F
  	t 4f=F 	&x!..'.B &u%&	& 	))*:;<F 	'1A1A1I1IJO	& 	& *n5 		s   6)E:5F :FFFc                v    | j                   y| j                   j                  }d|cxk  xr dk  nc xr |dk7  S )zW
    Determine whether the `HTTPError` is an HTTP 400 error code (except for 416).
    Fi  i  ra   )r=   rF   )erF   s     r)   _is_http_error_most_400_codesr     s:     	zz**((K+##:s(::r(   c               <   |j                  t        i       }|j                  t        i       }|j                  }t	               }	|s*t
        |v r"|j                         r|j                  d      sQt               }
t        d|  d      5  t
        |vr,|j                  t        d        |j                  t        d        	 |j                  d      rt        |
t        | d      ||||d      }n
t               	 |j2                  j                  d      |d<   |j2                  j                  d      |d<   |j2                  j                  d      |d<   d d d        j4                  dk(  r |
j7                         x|t
        <   |t8        <   |t
           }t	        g ddt;        j<                  t>        |i      dgg dg      }	n|t
           }d}	 |j                  dd      }|j                  dd      }|j                  t        d       }t        | d      }t(        jA                  dt-        |      ||       tC        |||tD        jG                  |      |d	      \  }	}|j1                  dd       d	}|r	 tC        t        | d      |       \  }	}||t        <   t        d$      5  tU        d% |	jV                  D              }|	jX                  \  }}}t;        jZ                  |      d&   }	 t]        |||      }t(        jK                  d't_        |       d(ta        |       d)ta        |              |r1t        d*      5  t;        jZ                  |jc                               }||jd                  j                  t
              k7  r't(        jg                  d+       	 d d d        d d d        y 	 d d d        ti        |       t        d,      5  |jk                  d-      5 }t               }
tm        ||
      jo                  t;        j<                  |d./      jq                  d0             |
j7                         |t8        <   ||t
        <   |cd d d        cd d d        cd d d        S |t
           |k(  sJ d d d        y # t        t         t"        j$                  f$ r}t'        |t"        j$                        r*t(        j+                  dt-        t        | d            |       t'        |t               rt/        |      s t'        |t              s|j1                  dd	       t        |
t        | d
      ||||      }Y d }~@d }~ww xY w# 1 sw Y   xY w# tH        $ r t(        jK                  d       Y tL        $ r t(        jO                  d       Y t         $ rh}t/        |      r(|j1                  dd	       tQ        | |d|||      cY d }~S t(        jK                  d|jR                  j4                         Y d }~d }~ww xY w# tH        tL        f$ rb}t(        jO                  d!|"       t	        g ddt;        j<                  t>        d#i      dgg dg      }	|j1                  dd	       Y d }~d }~ww xY w# 1 sw Y   xY w# 1 sw Y   nxY wd d d        *# 1 sw Y   4xY w# tr        t:        jt                  f$ rR}t'        |tr              rt(        jK                  d1       |rJ d2       tQ        | |d|||      cY d }~cd d d        S d }~ww xY w# 1 sw Y   y xY w)3Nr   zDownload complete  zstz	.json.zstT)r   rU   r   r   z9Could not decompress %s as zstd. Fall back to .json. (%s)Fz.json)r   rU   r   r#   _modr!   r   r"   _cache_controlr   )rA    r   r   r   )r   r   r   rB   rC   z.jlapzFetch %s from iv=%s, pos=%s)rC   r!   rB   rU   rT   z@Checksum not OK on JLAP range request. Retry with complete JLAP.z&IndexError reading JLAP. Invalid file?)full_downloadrU   cache	temp_pathzAResponse code %d on JLAP range request. Retry with complete JLAP.)rU   zError parsing jlap)exc_info 00000000000000000000000000000000zApply Patches c              3  N   K   | ]  \  }}}t        j                  |        y wr   )rH   rI   ).0rN   rx   s      r)   	<genexpr>z)request_url_jlap_state.<locals>.<genexpr>  s     H[Qqtzz%(Hs   #%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);rf   JLAP_KEYHEADERScache_path_jsonr   NOMINAL_HASHr   should_check_formatr*   r   r}   r   r   r   r   r3   r   r   	ZstdError
isinstancerb   warningr   r   set_has_formatr   rF   	hexdigestON_DISK_HASHrH   dumpsLATESTrc   rX   r   fromhexr   rk   
IndexError	exceptionrequest_url_jlap_stater=   listbodypenultimaterI   ry   r~   rn   loadr   warnr   r   r   r   encoder5   JSONDecodeError)rW   r   r   rU   r   r   
jlap_stater   r   rM   r   r=   r   ru   	need_jlapiv_hexrC   r!   jlap_urlrt   rN   rD   rv   rw   repodata_jsonr   s                             r)   r   r     s    8R(JnnWb)G%%IVF 	%)*:*:*<((0(Q/0 ,	L 5(		(D)		+T2 ,,U30[1!"+ '## H &-' B %,,00AE&M%--11&9E'N&.&6&6&:&:?&KE"#Y,	L^ 3&8>8H8H8JJE,%"5\" |aVTN)CR%H+VW \" 	*	^^D"-F..*C;;x.DsG,HII-#H-	 ",==(!"FJ   .I. 4%/W0Ew%W"
 %h	 	! D HFKKHH))61zz&!(+:	 $5EHHUIt$%%;K<M;NP
 G_ 
$ %)JJuzz|$<M u{{|<<!LM#
$%D D4 =
$ mU3,- )y~~d/C )x!VFx066

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

&& 	* 
+ 42Q?!Atzz682D'Er#JKX $$VU3348
$ 
$) ) ) ) )" "4#7#78 	!./
 _ %7&77$)"# {D Db	cD Ds  5T?>8Q66AT?B"U 1X  A\%AZ$9AY4Z$'Z$ZA+Z	Z	Z$Z$6T<BT71T?7T<<T??U	W=-W=W=-W8W=*W88W= Y1AY,,Y14Y>	9Z$Z
Z
Z$Z!	Z$$\=>\
;\<\
\\\)r   r(   )r=   r   )r   Nr(   TN)r   NTN)rU   zSession | None)r   pathlib.Pathr   r   )FN)r   r   rU   r   r   zRepodataState | Noner   zpathlib.Path | None)r   r   rE   bool)F)
r   r   rU   r   r   r   r   r   rE   zdict | None)A__doc__
__future__r   iorH   loggingri   r   r   
contextlibr   hashlibr   typingr   r   r   requestsr   conda.common.urlr   base.contextr
   r   r   r   r   corer   pathlibr   
connectionr   r   r   	getLoggerr.   rb   r&   r   r   r   r   r   rL   r*   	Exceptionr,   r3   LookupErrorr5   rR   rX   rV   rn   ry   r   r   r   r   	RawIOBaser   r   r   r   r'   r(   r)   <module>r      s    " 	    	  %       0 $ 9 9 / g! 
#		,
	 		) 		 	<; HL)6D)X1
 N) 6 6
 
& %)* * 	*
  * #*Z; QQ
 Q Q Q Qr(   