
    vg(                     d   d 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	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mZ ddlmZ  ej>                  e       Z!dZ"de	e#e#f   de#fdZ$ G d d      Z% G d de%      Z& G d de&      Z' G d d      Z( G d de%      Z)y)zCache Management
    N)Path)AnyDictListOptional)Taginterpreter_nameinterpreter_version)canonicalize_name)InvalidWheelFilename)	DirectUrl)Link)Wheel)TempDirectorytempdir_kinds)path_to_urlzorigin.jsondreturnc                     t        j                  | ddd      }t        j                  |j	                  d            j                         S )z'Return a stable sha224 of a dictionary.T),:)	sort_keys
separatorsensure_asciiascii)jsondumpshashlibsha224encode	hexdigest)r   ss     X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/pip/_internal/cache.py
_hash_dictr$      s7    

1$OA>>!((7+,6688    c                        e Zd ZdZdeddf fdZdedee   fdZdededee	   fd	Z
dedefd
Zdedee   dee   defdZ xZS )CachezuAn abstract class - provides cache directories for data from links

    :param cache_dir: The root of the cache.
    	cache_dirr   Nc                 ~    t         |           |r!t        j                  j	                  |      sJ |xs d | _        y N)super__init__ospathisabsr(   selfr(   	__class__s     r#   r,   zCache.__init__&   s2    i 888"*dr%   linkc                 (   d|j                   i}|j                  %|j                  |j                  ||j                  <   |j                  r|j                  |d<   t	               |d<   t               |d<   t        |      }|dd |dd |dd |dd g}|S )	z<Get parts of part that must be os.path.joined with cache_dirurlNsubdirectoryr	   r
            )url_without_fragment	hash_namehashsubdirectory_fragmentr	   r
   r$   )r1   r3   	key_partshashedpartss        r#   _get_cache_path_partszCache._get_cache_path_parts+   s     D556	>>%$))*?(,		Idnn%%%(,(B(BIn% )9(:	$%+>+@	'( I&
 VAa[&1+vabzBr%   canonical_package_namec                     | j                    xs | xs | }|rg S | j                  |      }t        j                  j	                  |      r%t        j
                  |      D cg c]  }||f c}S g S c c}w r*   )r(   get_path_for_linkr-   r.   isdirlistdir)r1   r3   rB   can_not_cacher.   	candidates         r#   _get_candidateszCache._get_candidatesL   sq     NN*T2H.HTPTHI%%d+77==79zz$7GH)Y%HH	 Is   #A3c                     t               )z5Return a directory to store cached items in for link.NotImplementedErrorr1   r3   s     r#   rD   zCache.get_path_for_linkV   s    !##r%   package_namesupported_tagsc                     t               )zaReturns a link to a cached item if it exists, otherwise returns the
        passed link.
        rK   )r1   r3   rN   rO   s       r#   getz	Cache.getZ   s     "##r%   )__name__
__module____qualname____doc__strr,   r   r   rA   r   rI   rD   r   r   rQ   __classcell__r2   s   @r#   r'   r'       s    
+# +$ +
$ 49 BD # $s) $d $s $	$	$ sm	$ S			$
 
	$r%   r'   c                   `     e Zd ZdZdeddf fdZdedefdZdedee   d	e	e
   defd
Z xZS )SimpleWheelCachez&A cache of wheels for future installs.r(   r   Nc                 $    t         |   |       y r*   )r+   r,   r0   s     r#   r,   zSimpleWheelCache.__init__i   s    #r%   r3   c                     | j                  |      }| j                  sJ t        j                  j                  | j                  dg| S )a  Return a directory to store cached wheels for link

        Because there are M wheels for any one sdist, we provide a directory
        to cache them in, and then consult that directory when looking up
        cache hits.

        We only insert things into the cache if they have plausible version
        numbers, so that we don't contaminate the cache with things that were
        not unique. E.g. ./package might have dozens of installs done for it
        and build a version of 0.0...and if we built and cached a wheel, we'd
        end up using the same wheel even if the source has been edited.

        :param link: The link of the sdist for which this will cache wheels.
        wheels)rA   r(   r-   r.   join)r1   r3   r@   s      r#   rD   z"SimpleWheelCache.get_path_for_linkl   s>     **40~~~ww||DNNH=u==r%   rN   rO   c                    g }|s|S t        |      }| j                  ||      D ]w  \  }}	 t        |      }t        |j                        |k7  rt
        j                  d|||       C|j                  |      sU|j                  |j                  |      ||f       y |s|S t        |      \  }	}}t        t        t        j                  j                  ||                  S # t        $ r Y w xY w)NzWIgnoring cached wheel %s for %s as it does not match the expected distribution name %s.)r   rI   r   r   nameloggerdebug	supportedappendsupport_index_minminr   r   r-   r.   r^   )
r1   r3   rN   rO   
candidatesrB   
wheel_name	wheel_dirwheel_s
             r#   rQ   zSimpleWheelCache.get   s    
K!2<!@%)%9%9$@V%W 	!J	j) !,0FFH  ??>2++N;#	2 K#&z? :yKY
 CDEE5 ( s   C##	C/.C/)rR   rS   rT   rU   rV   r,   r   rD   r   r   r   rQ   rW   rX   s   @r#   rZ   rZ   f   sd    0$# $$ $>d >s >()F)F sm)F S		)F
 
)Fr%   rZ   c                   $     e Zd ZdZd fdZ xZS )EphemWheelCachezBA SimpleWheelCache that creates it's own temporary cache directoryc                     t        t        j                  d      | _        t        |   | j                  j                         y )NT)kindglobally_managed)r   r   EPHEM_WHEEL_CACHE	_temp_dirr+   r,   r.   )r1   r2   s    r#   r,   zEphemWheelCache.__init__   s4    &00!

 	,,-r%   )r   N)rR   rS   rT   rU   r,   rW   rX   s   @r#   rm   rm      s    L. .r%   rm   c                       e Zd ZdedefdZy)
CacheEntryr3   
persistentc                 v   || _         || _        d | _        t        | j                   j                        j
                  t        z  }|j                         r,	 t        j                  |j                  d            | _        y y # t        $ r,}t        j                  d||j                  |       Y d }~y d }~ww xY w)Nutf-8encodingz7Ignoring invalid cache entry origin file %s for %s (%s))r3   ru   originr   	file_pathparentORIGIN_JSON_NAMEexistsr   	from_json	read_text	Exceptionra   warningfilename)r1   r3   ru   origin_direct_url_pathes        r#   r,   zCacheEntry.__init__   s    
 	$+/!%dii&9&9!:!A!ADT!T!((*
'11*44g4F +
  M*MM	 s   *B 	B8"B33B8N)rR   rS   rT   r   boolr,    r%   r#   rt   rt      s     r%   rt   c            	            e Zd ZdZdeddf fdZdedefdZdedefdZded	e	e   d
e
e   defdZded	e	e   d
e
e   de	e   fdZedededdfd       Z xZS )
WheelCachezWraps EphemWheelCache and SimpleWheelCache into a single Cache

    This Cache allows for gracefully degradation, using the ephem wheel cache
    when a certain link is not found in the simple wheel cache first.
    r(   r   Nc                 b    t         |   |       t        |      | _        t	               | _        y r*   )r+   r,   rZ   _wheel_cacherm   _ephem_cacher0   s     r#   r,   zWheelCache.__init__   s(    #,Y7+-r%   r3   c                 8    | j                   j                  |      S r*   )r   rD   rM   s     r#   rD   zWheelCache.get_path_for_link         22488r%   c                 8    | j                   j                  |      S r*   )r   rD   rM   s     r#   get_ephem_path_for_linkz"WheelCache.get_ephem_path_for_link   r   r%   rN   rO   c                 H    | j                  |||      }||S |j                  S r*   )get_cache_entryr3   )r1   r3   rN   rO   cache_entrys        r#   rQ   zWheelCache.get   s/     **4~NKr%   c                     | j                   j                  |||      }||urt        |d      S | j                  j                  |||      }||urt        |d      S y)zReturns a CacheEntry with a link to a cached item if it exists or
        None. The cache entry indicates if the item was found in the persistent
        or ephemeral cache.
        )r3   rN   rO   T)ru   FN)r   rQ   rt   r   )r1   r3   rN   rO   retvals        r#   r   zWheelCache.get_cache_entry   s}     ""&&%) ' 

 f66""&&%) ' 

 f77r%   download_infoc                    t        |       t        z  }|j                         rl	 t        j                  |j                  d            }|j                  |j                  k7  r-t        j                  d|j                  | |j                         	 |j                  |j                         d       y # t        $ r!}t        j                  d||       Y d }~Gd }~ww xY w)Nrw   rx   zOrigin URL %s in cache entry %s does not match download URL %s. This is likely a pip bug or a cache corruption issue. Will overwrite it with the new value.zPCould not read origin file %s in cache entry (%s). Will attempt to overwrite it.)r   r}   r~   r   r   r   r5   ra   r   r   
write_textto_json)r(   r   origin_pathrz   r   s        r#   record_download_originz!WheelCache.record_download_origin	  s    9o(88",,[-B-BG-B-TU ::!2!22NN@ 

!%)) 	}446I'  4	 s   %B1 1	C:CC)rR   rS   rT   rU   rV   r,   r   rD   r   r   r   r   rQ   rt   r   staticmethodr   r   rW   rX   s   @r#   r   r      s    .# .$ .
9d 9s 99D 9S 9	 	  sm	  S			 
 
	  sm S		
 
*	8 J# Ji JD J Jr%   r   )*rU   r   r   loggingr-   pathlibr   typingr   r   r   r   pip._vendor.packaging.tagsr   r	   r
   pip._vendor.packaging.utilsr   pip._internal.exceptionsr   pip._internal.models.direct_urlr   pip._internal.models.linkr   pip._internal.models.wheelr   pip._internal.utils.temp_dirr   r   pip._internal.utils.urlsr   	getLoggerrR   ra   r}   rV   r$   r'   rZ   rm   rt   r   r   r%   r#   <module>r      s       	  , , Q Q 9 9 5 * , E 0			8	$  9$sCx. 9S 9C$ C$LCFu CFL	.& 	. 0RJ RJr%   