
    .Ph                     >   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
mZmZ d dlmZ  ej        e          ZdZ	 dZ	 d	Z	 d
edefdZdeedd
ededee         dededefdZd
edededefdZd
edefdZdededdfdZdedee         fdZdS )    N)datetime)Path)Optional)FileLockTimeout)user_cache_pathi  i,  g{Gzt?urlreturnc                 h    t          j        | ddi          }|                                 |j        S )z.The simple equivalent to cached_download_file.
User-Agent
conda-lockheaders)requestsgetraise_for_statuscontent)r	   ress     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/conda_lock/lookup_cache.pyuncached_download_filer      s5    
,s\<$@
A
A
AC;    )
cache_rootmax_age_seconds dont_check_if_newer_than_secondscache_subdir_namer   r   r   c                   |t          dd          }|dz  |z  }|                    dd           t          ||           |t          |           z                      d	          }	 	 t          t          |          d
          5  t          | ||          cddd           S # 1 swxY w Y   n.# t          $ r! t          
                    d| d           Y nw xY wy)a  Download a file and cache it in the user cache directory.

    If the file is already cached, return the cached contents.
    If the file is not cached, download it and cache the contents
    and the ETag.

    Protect against multiple processes downloading the same file.
    Nr   F)	appauthorcacheT)parentsexist_ok)r   z.lock   )timeout)r   r   zFailed to acquire lock on z?, it is likely being downloaded by another process. Retrying...)r   mkdirclear_old_files_from_cachecached_filename_for_urlwith_suffixr   str_download_to_or_read_from_cacher   loggerwarning)r	   r   r   r   r   r   destination_locks          r   cached_download_filer,   &   st     $\UCCC
 #44E	KKtK,,,uoFFFF 7 < <<II'RR
	#.//;;;  65U                    	 	 	NND-= D D D    	s6   +B4 	B(B4 (B,,B4 /B,0B4 4(CCr   c                   |t          |           z  }|                    d          }ddi}|                                rt          |          }|t	          d|           d|cxk    r|k     r8n n5t
                              d| d| d	           |                                S |                                r+|                                	                                }||d
<   t
                              d|             t          j        | |          }|j        dk    r t
                              |  d|            nr|                                 |                    |j                   d|j        v r!|                    |j        d                    nt
                              d           t
                              d|  d|            |                                S )a  Download a file to the cache directory and return the contents.

    This function is designed to be called from within a FileLock context to avoid
    multiple processes downloading the same file.

    If the file is newer than `dont_check_if_newer_than_seconds`, then immediately
    return the cached contents. Otherwise we pass the ETag from the last download
    in the headers to avoid downloading the file if it hasn't changed remotely.
    z.etagr   r   NzError checking age of r   zUsing cached mapping  of age zs without checking for updateszIf-None-MatchzRequesting r   i0  z, has not changed since last download, using ETagzNo ETag in response headerszDownloaded z to )r%   r&   is_fileget_age_secondsRuntimeErrorr)   debug
read_bytes	read_textstripr   r   status_coder   write_bytesr   r   
write_textr*   )	r	   r   r   destinationdestination_etagrequest_headersage_secondsold_etagr   s	            r   r(   r(   P   s    1#666K"..w77#\2O 8%k22EEEFFF>>>>>>>>>>LL0 0 0[ 0 0 0   ))+++ ##%% 	8'113399;;H/7OO,
LL$s$$%%%
,sO
4
4
4C
#VVVVWWWW,,,S[  ''F(;<<<<NN8999
LL5s5555666!!###r   c                    t          j        |                                                                           dd         }|                     d          d         }t          |          dk    rt          j        d|          r| d| S | S )u	  Return a filename for a URL that is probably unique to the URL.

    The filename is a 4-character hash of the URL, followed by the extension.
    If the extension is not alphanumeric or too long, it is omitted.

    >>> cached_filename_for_url("https://example.com/foo.json")
    'a5d7.json'
    >>> cached_filename_for_url("https://example.com/foo")
    '5ea6'
    >>> cached_filename_for_url("https://example.com/foo.bär")
    '2191'
    >>> cached_filename_for_url("https://example.com/foo.baaaaaar")
    '1861'
    N   .   z^[a-zA-Z0-9]+$)hashlibsha256encode	hexdigestsplitlenrematch)r	   url_hash	extensions      r   r%   r%      s     ~cjjll++5577;H		#r"I
9~~rx(8)DD((Y(((}r   c                b   | j         j        dk    s"t          d|  dd| j         j         d          |                                 D ]g}t	          |          }||dk     s||k    rG	 |                                 t                              d| d	| d
           W# t          $ r Y cw xY whdS )a  Remove files in the cache directory older than `max_age_seconds`.

    Also removes any files that somehow have a modification time in the future.

    For safety, this raises an error if `cache` is not a subdirectory of
    a directory named `"cache"`.
    r   zExpected cache directory, got z. Parent should be 'cache' znot ''Nr   zRemoved old cache file r.   s)	parentname
ValueErroriterdirr1   unlinkr)   r3   FileNotFoundError)r   r   filer=   s       r   r$   r$      s     <''OUOOO(EL%(((
 
 	
  
 
%d++1__ > >StSS[SSSTTTT$   	 !?
 
s   (5B
B+*B+pathc                     	 t          j                                                    |                                 j        z
  }n# t
          $ r Y dS w xY wt          j                    dk    r
|t          z   S |S )zReturn the age of a file in seconds.

    On Windows, the age of a new file is sometimes slightly negative, so we add a small
    offset to ensure that the age is positive.
    NWindows)	r   now	timestampstatst_mtimerV   platformsystemWINDOWS_TIME_EPSILON)rX   raw_ages     r   r1   r1      sv    ,..**,,tyy{{/CC   ttI%%---s   ?A 
AA)rD   loggingr_   rJ   r   pathlibr   typingr   r   filelockr   r   platformdirsr   	getLogger__name__r)   CLEAR_CACHE_AFTER_SECONDS DONT_CHECK_IF_NEWER_THAN_SECONDSra   r'   bytesr   floatr,   r(   r%   r$   r1    r   r   <module>ro      s      				                    & & & & & & & & ( ( ( ( ( ( 
	8	$	$ -  3#)   V 
      "&6.N' ' '	' ' 	'
 ' ',' ' ' ' 'T-$	-$-$@E-$
-$ -$ -$ -$`     .d  $    4$ 8E?      r   