
    wBf+                       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mZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ dd	lmZ dd
lm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m Z m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z' ddlm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/  ee0      Z1dZ2d Z3 ed      	 	 	 	 dd       Z4d Z5e	 	 	 	 	 	 	 	 	 	 d d       Z6ed!d       Z7d Z8 G d d      Z9y)"z.Download logic for conda indices and packages.    )annotationsN)contextmanager)DEBUG	getLogger)basenameexistsjoin)Path   )
CondaError)dals)	stringify)context)time_recorder)BasicClobberErrorChecksumMismatchErrorCondaDependencyErrorCondaHTTPErrorCondaSSLError
ProxyErrormaybe_raise   )rm_rf)lock   )ConnectionError	HTTPErrorInsecureRequestWarningInvalidSchemaRequestsProxyErrorSSLError)get_sessioni @  c                 8    t        j                  dt               y )Nignore)warningssimplefilterr        Blib/python3.12/site-packages/conda/gateways/connection/download.pydisable_ssl_verify_warningr*   0   s    ($:;r(   downloadc           	         t        |      rt        t        || t              t               t        j                  s
t                t        |       5  t        | |||||       d d d        y # 1 sw Y   y xY wN)r   r   r   r   
ssl_verifyr*   download_http_errorsdownload_inner)urltarget_full_pathmd5sha256sizeprogress_update_callbacks         r)   r+   r+   4   se     %&6WEwO"$	c	" 
!36N	

 
 
s   A**A3c                   t         j                  t         j                  f}t        |       }d}|r|s|rd}d}	d}
t	        || |||      5 }t        j                  |j                               }||j                  |k\  r
	 d d d        y i }|r!|j                  dkD  rdd|j                   di}|j                  | d||j                  |      }t        j                  t              r t        j                  t        |d	
             |j!                          |r0|j"                  dk7  r!|j%                  d       |j'                          t)        |j*                  j                  dd            x}}|rO|rM|j*                  j                  dd      }	 t)        |j-                  dd      d   j/                  d      d         }|j5                  t6              D ]i  }|j8                  j;                         }		 |j=                  |       |
tG        |      z  }
|s@d|	cxk  r|k  sLn O|sR ||j                  |	z   |z         k |r |	|k7  rtI        d      }tA        || |||	      d d d        y # t0        t2        f$ r Y w xY w# t>        $ r)}d}tA        ||jB                  |jD                        d }~ww xY w# 1 sw Y   y xY w)NFTr   )r1   r3   r4   r5   Rangezbytes=-)streamheadersproxiestimeout   content_max_len   zContent-LengthzContent-Rangezbytes 0-0/0 r   /)
chunk_sizez5Failed to write to %(target_path)s
  errno: %(errno)d)target_patherrnoz
            Downloaded bytes did not match Content-Length
                url: %(url)s
                target_path: %(target_path)s
                Content-Length: %(content_length)d
                downloaded bytes: %(downloaded_bytes)d
            )r1   rF   content_lengthdownloaded_bytes)%r   remote_connect_timeout_secsremote_read_timeout_secsr"   download_partial_fileosfstatfilenost_sizegetr<   logisEnabledForr   debugr   raise_for_statusstatus_codeseektruncateintr;   splitrsplitLookupError
ValueErroriter_content
CHUNK_SIZErawtellwriteOSErrorr   namerG   lenr   )r1   r2   r3   r4   r5   r6   r=   sessionpartialstreamed_bytessize_buildertargetstat_resultr;   resprH   total_content_lengthcontent_rangechunkemessages                        r)   r0   r0   H   s   1173S3SSG#GGNL 
cs6
 H	hhv}}/ 3 3t ;H H {**Q.&)<)<(=Q ?@G{{gwPW  
 E"IIic:; t''3.KKNOO03LL-q11
 	
- w !LL,,_mLM'*!''Q/299#>rB($ &&*&= 	E "XX]]_NRU# CJ&L#^(M~(M+,$,,~=AUU	" n>G ,-!/ EH HF  ,   RR fkkQQRYH Hsm   
6J8	DJ80I.3J85JJ8J8#J8)<J8.J =J8?J  J8	J5$J00J55J88Kc             #     K   t                 j                  }t               j                  }| d}||z  } fd}		 |j                  d      5 }
t	        |
      5  |
  |	|
       ddd       ddd       	 |j                          y# 1 sw Y   $xY w# 1 sw Y   (xY w# t
        $ r]}	 |j                  j                  }n# t        $ r d}Y nw xY wt        |t              rd|cxk  rdk  rn  |j                           d}~wt        $ r |j                           w xY w# t        $ r$  j                          |j                          Y yw xY ww)z
    Create or open locked partial download file, moving onto target_full_path
    when finished. Preserve partial file on exception.
    z.partialc                F   | j                  d       srrdnd}rn}t        j                  |      }| j                  d       | j                  t              x}r)|j                  |       | j                  t              x}r)|j                         }||k7  r(t        j                  d|||       t        
|||      	Zt        j                  | j                               j                  }|	k7  r't        j                  d|	       t        
d	|      y y )Nr   r4   r3   z'%s mismatch for download: %s (%s != %s)z)size mismatch for download: %s (%s != %s)r5   )rW   hashlibnewreadr_   update	hexdigestrR   rT   r   rM   rN   rO   rP   )rj   checksum_typechecksumhasherrv   actual_checksumactual_sizer3   r4   r5   r2   r1   s          r)   checkz$download_partial_file.<locals>.check   s)   A&(.HEM!'vSH[[/FKKN ++j11$1d# !++j11$1 %..0O(*		=!# ,)=(O  ((6==?3;;Kd"		?	 ,)64  # r(   za+b)modeNi  i  )r
   parentrd   openr   r   responserV   AttributeError
isinstancerY   unlinkr   renamerc   )r2   r1   r4   r3   r5   r   rd   partial_namepartial_pathr~   rg   rp   rV   s   `````        r)   rL   rL      s^     ,-$$F !&&DV8$LL(L" "HE* 	gtG} 	M'N	 	".,-%	 	 	 	  		**00K 	K	k3'C;,D,D !    .!,-.s   A E B. B"&B3B";B. D0 E B	B""B+'B. .	D-8CDCDC2DD--E 0*EE EE c              #  z  K   	 d y# t         $ r}t        j                  d|z          d}~wt        $ r t	               t
        $ r)}dt        |      v rt        d      }t        |       d}~wt        $ rG}	 ddl
}t        t        d| d            # t        $ r t        t        d| d            w xY wd}~wt        t        f$ rk}t        d	      }t        || t!        |j"                  d
d      t!        |j"                  dd      t!        |j"                  dd      |j"                  |      d}~ww xY ww)z+Exception translator used inside download()Nz%s, trying againSOCKSaQ  
                Requests has identified that your current working environment is configured
                to use a SOCKS proxy, but pysocks is not installed.  To proceed, remove your
                proxy configuration, run `conda install pysocks`, and then you can re-enable
                your proxy configuration.
                r   z}
                    Encountered an SSL error. Most likely a certificate verification issue.

                    Exception: z
                    z
                    OpenSSL appears to be unavailable on this machine. OpenSSL is required to
                    download and install packages.

                    Exception: z
        An HTTP error occurred when trying to retrieve this URL.
        HTTP errors are often intermittent, and a simple retry will get you on your way.
        rV   reasonelapsed)	caused_by)ConnectionResetErrorrR   rT   r    r   r   strr   r   r!   sslr   ImportErrorr   r   r   getattrr   )r1   rp   rq   r   help_messages        r)   r/   r/      s^    B
 		$q() l c!fG 'w// 	    !"s #   
	  !"s #		 	
	, Y' 

 AJJt4AJJ$/AJJ	40JJ
 	

sY   D;	 D;	D8+D8	$A--D8:B>B;"B88B;;D8A&D33D88D;c                   t         j                  s
t                t        |       5  t         j                  t         j
                  f}t        |       }|j                  | d|j                  |      }t        j                  t              r t        j                  t        |d             |j                          d d d        |j                  S # 1 sw Y   j                  S xY w)NT)r:   r<   r=   r>   r?   )r   r.   r*   r/   rJ   rK   r"   rQ   r<   rR   rS   r   rT   r   rU   text)r1   r=   rf   r   s       r)   download_textr   7  s    "$	c	" $55w7W7WWc";;goow  
 E"IIi#>?!!#$ ==$ ==s   BCC"c                  $    e Zd ZdZddZd Zd Zy)TmpDownloadz2Context manager to handle downloads to a tempfile.c                     || _         || _        y r-   )r1   verbose)selfr1   r   s      r)   __init__zTmpDownload.__init__I  s    r(   c                    d| j                   vrd | _        | j                   S t        j                         | _        t	        | j                  t        | j                               }t        | j                   |       |S )Nz://)r1   tmp_dirtempfilemkdtempr	   r   r+   )r   dsts     r)   	__enter__zTmpDownload.__enter__M  sZ     DL88O#++-DLt||Xdhh%78CTXXs#Jr(   c                H    | j                   rt        | j                          y y r-   )r   r   )r   exc_type	exc_value	tracebacks       r)   __exit__zTmpDownload.__exit__X  s    <<$,, r(   N)T)__name__
__module____qualname____doc__r   r   r   r'   r(   r)   r   r   F  s    <	 r(   r   )NNNN)
r2   z
str | Pathr1   r   r4   r   r3   r   r5   rY   )r1   r   ):r   
__future__r   rt   rM   r   r%   
contextlibr   loggingr   r   os.pathr   r   r	   pathlibr
    r   
auxlib.ishr   auxlib.logzr   base.contextr   	common.ior   
exceptionsr   r   r   r   r   r   r   disk.deleter   	disk.lockr   r   r   r   r   r    r!   rf   r"   r   rR   r_   r*   r+   r0   rL   r/   r   r   r'   r(   r)   <module>r      s   5 "  	   % $ * *    $ # &       ! 
< z 		!
 
&Ur G. G.*-G.7:G.ADG.LOG. G.T G
 G
T   r(   