U
    „~$h«  ã                   @   s˜   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 d dl	m
Z
mZ d dlmZmZ d dlmZ d dlmZ d d	lmZ eeƒZG d
d„ deƒZdS )é    N)ÚValues)ÚAnyÚList)ÚCommand)ÚERRORÚSUCCESS)ÚCommandErrorÚPipError)Ú
filesystem)Ú	getLogger)Úformat_sizec                   @   s  e Zd ZdZdZdZddœdd„Zeee	 e
dœd	d
„Zeee ddœdd„Zeee ddœdd„Zeee ddœdd„Zee	 ddœdd„Zee	 ddœdd„Zeee ddœdd„Zeee ddœdd„Zee	e	dœdd„Zeee	 dœdd„Zee	ee	 d œd!d"„ZdS )#ÚCacheCommandaw  
    Inspect and manage pip's wheel cache.

    Subcommands:

    - dir: Show the cache directory.
    - info: Show information about the cache.
    - list: List filenames of packages stored in the cache.
    - remove: Remove one or more package from the cache.
    - purge: Remove all items from the cache.

    ``<pattern>`` can be a glob expression or a package name.
    Tz˜
        %prog dir
        %prog info
        %prog list [<pattern>] [--format=[human, abspath]]
        %prog remove <pattern>
        %prog purge
    N)Úreturnc                 C   s,   | j jddddddd� | j d| j ¡ d S )	Nz--formatÚstoreÚlist_formatÚhuman)r   Úabspathz:Select the output format among: human (default) or abspath)ÚactionÚdestÚdefaultÚchoicesÚhelpr   )Úcmd_optsÚ
add_optionÚparserÚinsert_option_group)Úself© r   ú@/tmp/pip-unpacked-wheel-g27rp152/pip/_internal/commands/cache.pyÚadd_options(   s    ú	zCacheCommand.add_options)ÚoptionsÚargsr   c              
   C   sº   | j | j| j| j| jdœ}|js.t d¡ tS |r>|d |krXt dd 	t
|ƒ¡¡ tS |d }z|| ||dd … ƒ W n: tk
r´ } zt |jd ¡ t W Y ¢S d }~X Y nX tS )N)ÚdirÚinfoÚlistÚremoveÚpurgez<pip cache commands can not function since cache is disabled.r   zNeed an action (%s) to perform.z, é   )Úget_cache_dirÚget_cache_infoÚlist_cache_itemsÚremove_cache_itemsÚpurge_cacheÚ	cache_dirÚloggerÚerrorr   ÚjoinÚsortedr	   r!   r   )r   r    r!   Úhandlersr   Úer   r   r   Úrun4   s,    û
þzCacheCommand.runc                 C   s   |rt dƒ‚t |j¡ d S )NúToo many arguments)r   r.   r#   r-   ©r   r    r!   r   r   r   r(   T   s    zCacheCommand.get_cache_dirc              	   C   s¢   |rt dƒ‚t|  |¡ƒ}t|  |d¡ƒ}|  |d¡}|  |d¡}|  |d¡}t t |¡t |¡ ¡}t |¡}	t	 
d¡j|||||||	d� ¡ }
t |
¡ d S )Nr5   Ú*úhttp-v2ÚhttpÚwheelsa  
                    Package index page cache location (pip v23.3+): {http_cache_location}
                    Package index page cache location (older pips): {old_http_cache_location}
                    Package index page cache size: {http_cache_size}
                    Number of HTTP files: {num_http_files}
                    Locally built wheels location: {wheels_cache_location}
                    Locally built wheels size: {wheels_cache_size}
                    Number of locally built wheels: {package_count}
                )Úhttp_cache_locationÚold_http_cache_locationÚhttp_cache_sizeÚnum_http_filesÚwheels_cache_locationZpackage_countÚwheels_cache_size)r   ÚlenÚ_find_http_filesÚ_find_wheelsÚ
_cache_dirr
   r   Zdirectory_sizeZformat_directory_sizeÚtextwrapÚdedentÚformatÚstripr.   r#   )r   r    r!   r>   Znum_packagesr;   r<   r?   r=   r@   Úmessager   r   r   r)   Z   s4    ÿÿ
ÿîÿzCacheCommand.get_cache_infoc                 C   sV   t |ƒdkrtdƒ‚|r"|d }nd}|  ||¡}|jdkrH|  |¡ n
|  |¡ d S )Nr'   r5   r   r7   r   )rA   r   rC   r   Úformat_for_humanÚformat_for_abspath)r   r    r!   ÚpatternÚfilesr   r   r   r*   „   s    

zCacheCommand.list_cache_items)rM   r   c                 C   sp   |st  d¡ d S g }|D ]2}tj |¡}t |¡}| d|› d|› d�¡ qt  d¡ t  d t	|ƒ¡¡ d S )NzNo locally built wheels cached.z - z (ú)zCache contents:
Ú
)
r.   r#   ÚosÚpathÚbasenamer
   Zformat_file_sizeÚappendr0   r1   )r   rM   ÚresultsÚfilenameÚwheelÚsizer   r   r   rJ   “   s    


zCacheCommand.format_for_humanc                 C   s   |rt  d t|ƒ¡¡ d S )NrO   )r.   r#   r0   r1   )r   rM   r   r   r   rK       s    zCacheCommand.format_for_abspathc                 C   sÀ   t |ƒdkrtdƒ‚|s tdƒ‚|  ||d ¡}d}|d dkrP||  |¡7 }n|d|d › d�7 }|srt |¡ d}|D ]*}|t |¡j7 }t 	|¡ t 
d	|¡ qzt d
t |ƒt|ƒ¡ d S )Nr'   r5   zPlease provide a patternr   zNo matching packagesr7   z for pattern "ú"z
Removed %szFiles removed: %s (%s))rA   r   rC   rB   r.   ÚwarningrP   ÚstatÚst_sizeÚunlinkÚverboser#   r   )r   r    r!   rM   Zno_matching_msgZbytes_removedrU   r   r   r   r+   ¤   s"    

zCacheCommand.remove_cache_itemsc                 C   s   |rt dƒ‚|  |dg¡S )Nr5   r7   )r   r+   r6   r   r   r   r,   ¿   s    zCacheCommand.purge_cache)r    Úsubdirr   c                 C   s   t j |j|¡S )N)rP   rQ   r0   r-   )r   r    r^   r   r   r   rD   Å   s    zCacheCommand._cache_dir)r    r   c                 C   s0   |   |d¡}|   |d¡}t |d¡t |d¡ S )Nr9   r8   r7   ©rD   r
   Z
find_files)r   r    Zold_http_dirZnew_http_dirr   r   r   rB   È   s     ÿzCacheCommand._find_http_files)r    rL   r   c                 C   s,   |   |d¡}|d|krdnd }t ||¡S )Nr:   ú-z*.whlz-*.whlr_   )r   r    rL   Ú	wheel_dirr   r   r   rC   Ï   s    zCacheCommand._find_wheels)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úignore_require_venvÚusager   r   r   ÚstrÚintr4   r   r(   r)   r*   rJ   rK   r+   r,   rD   rB   rC   r   r   r   r   r      s    *r   )rP   rE   Úoptparser   Útypingr   r   Úpip._internal.cli.base_commandr   Úpip._internal.cli.status_codesr   r   Úpip._internal.exceptionsr   r	   Zpip._internal.utilsr
   Úpip._internal.utils.loggingr   Úpip._internal.utils.miscr   rb   r.   r   r   r   r   r   Ú<module>   s   