U
    7cU-                     @   s  d Z ddlZddlZddlZddlZddl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 erddlmZmZmZmZmZ ddlmZ ddlmZ ee Z!dd Z"G dd de#Z$G dd de$Z%G dd de%Z&G dd de$Z'dS )zCache Management
    N)interpreter_nameinterpreter_version)canonicalize_name)InvalidWheelFilename)Link)Wheel)TempDirectory)MYPY_CHECK_RUNNING)path_to_url)OptionalSetListAnyDict)Tag)FormatControlc                 C   s&   t j| dddd}t|d S )z'Return a stable sha224 of a dictionary.T),:)	sort_keys
separatorsensure_asciiascii)jsondumpshashlibsha224encode	hexdigest)ds r    7/tmp/pip-unpacked-wheel-xvghy_sv/pip/_internal/cache.py
_hash_dict    s    r"   c                       sX   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )Cachea  An abstract class - provides cache directories for data from links


        :param cache_dir: The root of the cache.
        :param format_control: An object of FormatControl class to limit
            binaries being read from the cache.
        :param allowed_formats: which formats of files the cache should store.
            ('binary' and 'source' are the only allowed values)
    c                    sX   t t|   |r"tj|s"t|p(d | _|| _|| _	ddh}| j	
||ksTtd S )Nsourcebinary)superr#   __init__ospathisabsAssertionError	cache_dirformat_controlallowed_formatsunion)selfr,   r-   r.   _valid_formats	__class__r    r!   r'   2   s    
zCache.__init__c                 C   s   |j g}|jdk	r4|jdk	r4|d|j|jg d|}t|  }|dd |dd |dd |dd g}|S )zGet parts of part that must be os.path.joined with cache_dir

        Legacy cache key (pip < 20) for compatibility with older caches.
        N=#         )	url_without_fragment	hash_namehashappendjoinr   r   r   r   )r0   link	key_partskey_urlhashedpartsr    r    r!   _get_cache_path_parts_legacy=   s    

,z"Cache._get_cache_path_parts_legacyc                 C   s   d|j i}|jdk	r*|jdk	r*|j||j< |jr:|j|d< t |d< t |d< t|}|dd |dd |dd |dd g}|S )	zEGet parts of part that must be os.path.joined with cache_dir
        urlNsubdirectoryr   r   r6   r7   r8   )r9   r:   r;   subdirectory_fragmentr   r   r"   )r0   r>   r?   rA   rB   r    r    r!   _get_cache_path_partsY   s    



,zCache._get_cache_path_partsc           	      C   s   | j  p| p| }|rg S | j|}| j|s8g S g }| |}tj|rpt	|D ]}|
||f q\| |}tj|rt	|D ]}|
||f q|S N)r,   r-   get_allowed_formatsr.   intersectionget_path_for_linkr(   r)   isdirlistdirr<   get_path_for_link_legacy)	r0   r>   canonical_package_namecan_not_cacheformats
candidatesr)   	candidatelegacy_pathr    r    r!   _get_candidates|   s,    

zCache._get_candidatesc                 C   s
   t  d S rH   NotImplementedErrorr0   r>   r    r    r!   rN      s    zCache.get_path_for_link_legacyc                 C   s
   t  dS )z>Return a directory to store cached items in for link.
        NrV   rX   r    r    r!   rK      s    zCache.get_path_for_linkc                 C   s
   t  dS )zaReturns a link to a cached item if it exists, otherwise returns the
        passed link.
        NrV   )r0   r>   package_namesupported_tagsr    r    r!   get   s    
z	Cache.getc                 C   s   d S rH   r    r0   r    r    r!   cleanup   s    zCache.cleanup)__name__
__module____qualname____doc__r'   rC   rG   rU   rN   rK   r[   r]   __classcell__r    r    r2   r!   r#   '   s   
#r#   c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
SimpleWheelCachez+A cache of wheels for future installs.
    c                    s   t t| ||dh d S Nr%   )r&   rc   r'   r0   r,   r-   r2   r    r!   r'      s
    
  zSimpleWheelCache.__init__c                 C   s    |  |}tjj| jdf| S )Nwheels)rC   r(   r)   r=   r,   r0   r>   rB   r    r    r!   rN      s    
z)SimpleWheelCache.get_path_for_link_legacyc                 C   s    |  |}tjj| jdf| 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.
        rf   )rG   r(   r)   r=   r,   rg   r    r    r!   rK      s    
z"SimpleWheelCache.get_path_for_linkc           
   	   C   s   g }|s|S t |}| ||D ]t\}}zt|}W n tk
rL   Y q Y nX t |j|krrtd||| q ||s~q |	|
|||f q |s|S t|\}	}}tttj||S )NzWIgnoring cached wheel {} for {} as it does not match the expected distribution name {}.)r   rU   r   r   nameloggerdebugformat	supportedr<   support_index_minminr   r
   r(   r)   r=   )
r0   r>   rY   rZ   rR   rO   
wheel_name	wheel_dirwheel_r    r    r!   r[      sB     
  
zSimpleWheelCache.get)	r^   r_   r`   ra   r'   rN   rK   r[   rb   r    r    r2   r!   rc      s
   rc   c                       s(   e Zd ZdZ fddZdd Z  ZS )EphemWheelCachezGA SimpleWheelCache that creates it's own temporary cache directory
    c                    s&   t dd| _tt| | jj| d S )Nzephem-wheel-cache)kind)r   	_temp_dirr&   rs   r'   r)   )r0   r-   r2   r    r!   r'   	  s
    
 zEphemWheelCache.__init__c                 C   s   | j   d S rH   )ru   r]   r\   r    r    r!   r]     s    zEphemWheelCache.cleanup)r^   r_   r`   ra   r'   r]   rb   r    r    r2   r!   rs     s   rs   c                       sH   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	  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.
    c                    s0   t t| ||dh t||| _t|| _d S rd   )r&   rv   r'   rc   _wheel_cachers   _ephem_cachere   r2   r    r!   r'     s    
  zWheelCache.__init__c                 C   s   | j |S rH   )rw   rN   rX   r    r    r!   rN   %  s    z#WheelCache.get_path_for_link_legacyc                 C   s   | j |S rH   )rw   rK   rX   r    r    r!   rK   )  s    zWheelCache.get_path_for_linkc                 C   s   | j |S rH   )rx   rK   rX   r    r    r!   get_ephem_path_for_link-  s    z"WheelCache.get_ephem_path_for_linkc                 C   s0   | j j|||d}||k	r|S | jj|||dS )N)r>   rY   rZ   )rw   r[   rx   )r0   r>   rY   rZ   retvalr    r    r!   r[   1  s    zWheelCache.getc                 C   s   | j   | j  d S rH   )rw   r]   rx   r\   r    r    r!   r]   F  s    
zWheelCache.cleanup)r^   r_   r`   ra   r'   rN   rK   ry   r[   r]   rb   r    r    r2   r!   rv     s   rv   )(ra   r   r   loggingr(   pip._vendor.packaging.tagsr   r   pip._vendor.packaging.utilsr   pip._internal.exceptionsr   pip._internal.models.linkr   pip._internal.models.wheelr   pip._internal.utils.temp_dirr   pip._internal.utils.typingr	   pip._internal.utils.urlsr
   typingr   r   r   r   r   r   #pip._internal.models.format_controlr   	getLoggerr^   ri   r"   objectr#   rc   rs   rv   r    r    r    r!   <module>   s.   
 R