a
    ü`ƒ  ã                   @   s²   d dl mZ d dlZd dlZd dlZd dlm  m  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 er”d dlmZ d dlmZmZ e e¡ZG d	d
„ d
e
ƒZdS )é    )Úabsolute_importN)ÚCommand)ÚERRORÚSUCCESS)ÚCommandErrorÚPipError)ÚMYPY_CHECK_RUNNING)ÚValues)ÚAnyÚListc                   @   sx   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z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
    c                 C   s,   | j jddddddd | j d| j ¡ d S )	Nz--formatZstoreÚlist_formatÚhuman)r   Úabspathz:Select the output format among: human (default) or abspath)ÚactionÚdestÚdefaultÚchoicesÚhelpr   )Zcmd_optsZ
add_optionÚparserZinsert_option_group)Úself© r   ú>/usr/lib/python3/dist-packages/pip/_internal/commands/cache.pyÚadd_options-   s    ú	zCacheCommand.add_optionsc              
   C   s¸   | j | j| j| j| jdœ}|js.t d¡ tS |r>|d |vrXt dd 	t
|ƒ¡¡ tS |d }z|| ||dd … ƒ W n8 ty² } z t |jd ¡ tW  Y d }~S d }~0 0 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   Úargsr   )r   Úoptionsr*   Úhandlersr   Úer   r   r   Úrun;   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    ]   s    zCacheCommand.get_cache_dirc           
      C   s„   |rt dƒ‚t|  |¡ƒ}t|  |d¡ƒ}|  |d¡}|  |d¡}t |¡}t |¡}t d¡j	||||||d 
¡ }	t |	¡ d S )Nr/   Ú*ÚhttpÚwheelsaN  
            Package index page cache location: {http_cache_location}
            Package index page cache size: {http_cache_size}
            Number of HTTP files: {num_http_files}
            Wheels location: {wheels_cache_location}
            Wheels size: {wheels_cache_size}
            Number of wheels: {package_count}
        )Ú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_dirÚ
filesystemZformat_directory_sizeÚtextwrapÚdedentÚformatÚstripr&   r   )
r   r+   r*   r6   Znum_packagesr4   r7   r5   r8   Úmessager   r   r   r!   d   s&    
ÿ
ó
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   r/   r   r1   r   )r9   r   r;   r   Úformat_for_humanÚformat_for_abspath)r   r+   r*   ÚpatternÚfilesr   r   r   r"   …   s    

zCacheCommand.list_cache_itemsc                 C   sj   |st  d¡ d S g }|D ],}tj |¡}t |¡}| d ||¡¡ qt  d¡ t  d 	t
|ƒ¡¡ d S )NzNothing cached.z
 - {} ({})zCache contents:
Ú
)r&   r   ÚosÚpathÚbasenamer=   Zformat_file_sizeÚappendr@   r(   r)   )r   rF   ÚresultsÚfilenameZwheelÚsizer   r   r   rC   •   s    


zCacheCommand.format_for_humanc                 C   s8   |sd S g }|D ]}|  |¡ qt d t|ƒ¡¡ d S )NrG   )rK   r&   r   r(   r)   )r   rF   rL   rM   r   r   r   rD   £   s    zCacheCommand.format_for_abspathc                 C   sŠ   t |ƒdkrtdƒ‚|s tdƒ‚|  ||d ¡}|d dkrJ||  |¡7 }|sVtdƒ‚|D ]}t |¡ t d|¡ qZt dt |ƒ¡ d S )	Nr   r/   zPlease provide a patternr   r1   zNo matching packagesz
Removed %szFiles removed: %s)	r9   r   r;   r:   rH   Úunlinkr&   Údebugr   )r   r+   r*   rF   rM   r   r   r   r#   ®   s    
zCacheCommand.remove_cache_itemsc                 C   s   |rt dƒ‚|  |dg¡S )Nr/   r1   )r   r#   r0   r   r   r   r$   Ä   s    zCacheCommand.purge_cachec                 C   s   t j |j|¡S )N)rH   rI   r(   r%   )r   r+   Zsubdirr   r   r   r<   Ë   s    zCacheCommand._cache_dirc                 C   s   |   |d¡}t |d¡S )Nr2   r1   ©r<   r=   Z
find_files)r   r+   Zhttp_dirr   r   r   r:   Ï   s    zCacheCommand._find_http_filesc                 C   s,   |   |d¡}|d|v rdnd }t ||¡S )Nr3   ú-z*.whlz-*.whlrQ   )r   r+   rE   Z	wheel_dirr   r   r   r;   Ô   s    zCacheCommand._find_wheelsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zignore_require_venvZusager   r.   r    r!   r"   rC   rD   r#   r$   r<   r:   r;   r   r   r   r   r      s   "!r   )Z
__future__r   ZloggingrH   r>   Zpip._internal.utils.filesystemZ	_internalZutilsr=   Zpip._internal.cli.base_commandr   Zpip._internal.cli.status_codesr   r   Zpip._internal.exceptionsr   r   Zpip._internal.utils.typingr   Zoptparser	   Útypingr
   r   Z	getLoggerrS   r&   r   r   r   r   r   Ú<module>   s   
