
(°hƒ  ã               @   sê   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l j j j 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 j e ƒ Z Gd	 d
 „  d
 e
 ƒ Z d S)é    )Úabsolute_importN)ÚCommand)ÚERRORÚSUCCESS)ÚCommandErrorÚPipError)ÚMYPY_CHECK_RUNNING)ÚValues)ÚAnyÚListc               @   s²   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   sH   |  j  j d d d d d d d d d d
 d ƒ|  j j d |  j  ƒ d  S)Nz--formatÚactionÚstoreÚdestÚlist_formatÚdefaultÚhumanÚchoicesÚabspathÚhelpz:Select the output format among: human (default) or abspathr   )r   zabspath)Úcmd_optsÚ
add_optionÚparserÚinsert_option_group)Úself© r   ú;/tmp/pip-build-kmztzv_d/pip/pip/_internal/commands/cache.pyÚadd_options-   s    	zCacheCommand.add_optionsc             C   sô   d |  j  d |  j d |  j d |  j d |  j i } | j sM t j d ƒ t S| sd | d | k r‡ t j d d	 j	 t
 | ƒ ƒ ƒ t S| d } y | | | | d
 d  … ƒ Wn= t k
 rï } z t j | j d ƒ t SWYd  d  } ~ Xn Xt 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 | j ƒ d  S)Nz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 |  j | ƒ ƒ } t |  j | d ƒ ƒ } |  j | d ƒ } |  j | d ƒ } t j | ƒ } t j | ƒ } t j d ƒ j	 d | d | d | d	 | d
 | d | ƒ j
 ƒ  }	 t j |	 ƒ d  S)NzToo many argumentsÚ*Ú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.   r8   Znum_packagesr6   r9   r7   r:   Úmessager   r   r   r%   d   s$    	zCacheCommand.get_cache_infoc             C   sy   t  | ƒ d k r t d ƒ ‚ | r1 | d } n d } |  j | | ƒ } | j d k rh |  j | ƒ n |  j | ƒ d  S)Nr#   zToo many argumentsr   r3   r   )r;   r   r=   r   Úformat_for_humanÚformat_for_abspath)r   r/   r.   ÚpatternÚfilesr   r   r   r&   …   s    zCacheCommand.list_cache_itemsc             C   s•   | s t  j d ƒ d  Sg  } xH | D]@ } t j j | ƒ } t j | ƒ } | j d j | | ƒ ƒ q$ Wt  j d ƒ t  j d j	 t
 | ƒ ƒ ƒ d  S)NzNothing cached.z
 - {} ({})zCache contents:
Ú
)r*   r   ÚosÚpathÚbasenamer?   Zformat_file_sizeÚappendrB   r,   r-   )r   rH   ÚresultsÚfilenameÚwheelÚsizer   r   r   rE   •   s    zCacheCommand.format_for_humanc             C   sN   | s
 d  Sg  } x | D] } | j  | ƒ q Wt j d j t | ƒ ƒ ƒ d  S)NrI   )rM   r*   r   r,   r-   )r   rH   rN   rO   r   r   r   rF   £   s    zCacheCommand.format_for_abspathc             C   sÃ   t  | ƒ d k r t d ƒ ‚ | s0 t d ƒ ‚ |  j | | d ƒ } | d d k ri | |  j | ƒ 7} | s{ t d ƒ ‚ x+ | D]# } t j | ƒ t j d | ƒ q‚ Wt j d t  | ƒ ƒ d  S)	Nr#   zToo many argumentszPlease provide a patternr   r3   zNo matching packagesz
Removed %szFiles removed: %s)	r;   r   r=   r<   rJ   Úunlinkr*   Údebugr   )r   r/   r.   rH   rO   r   r   r   r'   ®   s    zCacheCommand.remove_cache_itemsc             C   s%   | r t  d ƒ ‚ |  j | d g ƒ S)NzToo many argumentsr3   )r   r'   )r   r/   r.   r   r   r   r(   Ä   s    zCacheCommand.purge_cachec             C   s   t  j j | j | ƒ S)N)rJ   rK   r,   r)   )r   r/   Úsubdirr   r   r   r>   Ë   s    zCacheCommand._cache_dirc             C   s"   |  j  | d ƒ } t j | d ƒ S)Nr4   r3   )r>   r?   Ú
find_files)r   r/   Zhttp_dirr   r   r   r<   Ï   s    zCacheCommand._find_http_filesc             C   s>   |  j  | d ƒ } | d | k r' d n d } t j | | ƒ S)Nr5   ú-z*.whlz-*.whl)r>   r?   rU   )r   r/   rG   Ú	wheel_dirr   r   r   r=   Ô   s    zCacheCommand._find_wheelsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zignore_require_venvÚusager   r2   r$   r%   r&   rE   rF   r'   r(   r>   r<   r=   r   r   r   r   r      s   "!r   )Ú
__future__r   ÚloggingrJ   r@   Zpip._internal.utils.filesystemZ	_internalÚutilsr?   Zpip._internal.cli.base_commandr   Zpip._internal.cli.status_codesr   r   Zpip._internal.exceptionsr   r   Zpip._internal.utils.typingr   Úoptparser	   Ztypingr
   r   Ú	getLoggerrX   r*   r   r   r   r   r   Ú<module>   s   