U
    5øi¤)  ã                   @   sÞ   d dl mZ d dlZd dl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 d d
lmZ d dlmZmZmZ d dlmZ d dlmZ e e¡ZG dd„ deƒZdd„ Z dd„ Z!dd„ Z"dS )é    )Úabsolute_importN)Úsix)Úzip_longest)Ú
cmdoptions)ÚIndexGroupCommand)ÚCommandError)ÚPackageFinder)ÚSelectionPreferences)Úmake_link_collector)Údist_is_editableÚget_installed_distributionsÚwrite_output)Úget_installer)Úparsec                       sd   e Zd 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dd„ Z‡  ZS )ÚListCommandzt
    List installed packages, including editables.

    Packages are listed in a case-insensitive sorted order.
    z
      %prog [options]c                    s  t t| ƒj||Ž | j}|jdddddd� |jddddd	d� |jd
ddddd� |jdddddd� | jjdddddd� | t ¡ ¡ |jddddd� |jddddddd� |jddddd � |jd!d"d#d$d � |jd%dd#d&d'd(� t tj| j	¡}| j	 
d)|¡ | j	 
d)|¡ d S )*Nz-oz
--outdatedÚ
store_trueFzList outdated packages)ÚactionÚdefaultÚhelpz-uz
--uptodatezList uptodate packagesz-ez
--editablezList editable projects.z-lz--localzSIf in a virtualenv that has global access, do not list globally-installed packages.z--userÚuserz,Only output packages installed in user-site.)Údestr   r   r   z--prezYInclude pre-release and development versions. By default, pip only finds stable versions.z--formatÚstoreÚlist_formatÚcolumns)r   ÚfreezeÚjsonzBSelect the output format among: columns (default), freeze, or json)r   r   r   Úchoicesr   z--not-requiredÚnot_requiredz>List packages that are not dependencies of installed packages.)r   r   r   z--exclude-editableÚstore_falseÚinclude_editablez%Exclude editable package from output.z--include-editablez%Include editable package from output.T)r   r   r   r   r   )Úsuperr   Ú__init__Úcmd_optsÚ
add_optionr   Ú	list_pathÚmake_option_groupÚindex_groupÚparserÚinsert_option_group)ÚselfÚargsÚkwr"   Ú
index_opts©Ú	__class__© ú?/tmp/pip-unpacked-wheel-xstzcrsb/pip/_internal/commands/list.pyr!   (   sš     ü ü ü üûüú
üüû ÿzListCommand.__init__c                 C   s(   t ||d�}td|jd�}tj||d�S )zK
        Create a package finder appropriate to this list command.
        )ÚoptionsF)Úallow_yankedÚallow_all_prereleases)Úlink_collectorÚselection_prefs)r
   r	   Úprer   Úcreate)r)   r1   Úsessionr4   r5   r/   r/   r0   Ú_build_package_finderx   s    þþz!ListCommand._build_package_finderc                 C   s‚   |j r|jrtdƒ‚t |¡ t|j|j|j|j	|j
d�}|jrL|  ||¡}|j r`|  ||¡}n|jrr|  ||¡}|  ||¡ d S )Nz5Options --outdated and --uptodate cannot be combined.)Ú
local_onlyÚ	user_onlyÚeditables_onlyÚinclude_editablesÚpaths)ÚoutdatedZuptodater   r   Úcheck_list_path_optionr   Úlocalr   Úeditabler   Úpathr   Úget_not_requiredÚget_outdatedÚget_uptodateÚoutput_package_listing)r)   r1   r*   Úpackagesr/   r/   r0   Úrun‰   s&    ÿ
ûzListCommand.runc                 C   s   dd„ |   ||¡D ƒS )Nc                 S   s,   g | ]$}t t|jƒƒt t|jƒƒkr|‘qS r/   ©r   ÚstrÚlatest_versionÚparsed_version©Ú.0Údistr/   r/   r0   Ú
<listcomp>§   s   ÿz,ListCommand.get_outdated.<locals>.<listcomp>©Úiter_packages_latest_infos©r)   rH   r1   r/   r/   r0   rE   ¦   s    
ÿzListCommand.get_outdatedc                 C   s   dd„ |   ||¡D ƒS )Nc                 S   s,   g | ]$}t t|jƒƒt t|jƒƒkr|‘qS r/   rJ   rN   r/   r/   r0   rQ   ­   s   ÿz,ListCommand.get_uptodate.<locals>.<listcomp>rR   rT   r/   r/   r0   rF   ¬   s    
ÿzListCommand.get_uptodatec                    s:   t ƒ ‰ |D ]}ˆ  dd„ | ¡ D ƒ¡ q
‡ fdd„|D ƒS )Nc                 s   s   | ]}|j V  qd S ©N©Úkey)rO   Úrequirementr/   r/   r0   Ú	<genexpr>µ   s     z/ListCommand.get_not_required.<locals>.<genexpr>c                    s   h | ]}|j ˆ kr|’qS r/   rV   )rO   Úpkg©Zdep_keysr/   r0   Ú	<setcomp>¶   s     
 z/ListCommand.get_not_required.<locals>.<setcomp>)ÚsetÚupdateÚrequires)r)   rH   r1   rP   r/   r[   r0   rD   ²   s    zListCommand.get_not_requiredc              	   c   s    |   |¡�Œ}|  ||¡}|D ]t}d}| |j¡}|jsDdd„ |D ƒ}|j|jd�}| |¡}	|	d krfq|	j}
|	j	j
rzd}nd}|
|_||_|V  qW 5 Q R X d S )NÚunknownc                 S   s   g | ]}|j js|‘qS r/   )ÚversionÚis_prerelease)rO   Ú	candidater/   r/   r0   rQ   Á   s    ÿz:ListCommand.iter_packages_latest_infos.<locals>.<listcomp>)Úproject_nameÚwheelÚsdist)Ú_build_sessionr9   Úfind_all_candidatesrW   r6   Úmake_candidate_evaluatorrd   Úsort_best_candidatera   ÚlinkÚis_wheelrL   Úlatest_filetype)r)   rH   r1   r8   ÚfinderrP   ÚtypZall_candidatesZ	evaluatorÚbest_candidateÚremote_versionr/   r/   r0   rS   ¸   s(    ÿ
z&ListCommand.iter_packages_latest_infosc                 C   sœ   t |dd„ d�}|jdkr:|r:t||ƒ\}}|  ||¡ n^|jdkr€|D ]4}|jdkrltd|j|j|jƒ qHtd|j|jƒ qHn|jd	kr˜tt	||ƒƒ d S )
Nc                 S   s
   | j  ¡ S rU   )rd   Úlower)rP   r/   r/   r0   Ú<lambda>Ø   ó    z4ListCommand.output_package_listing.<locals>.<lambda>rV   r   r   é   z%s==%s (%s)z%s==%sr   )
Úsortedr   Úformat_for_columnsÚoutput_package_listing_columnsÚverboser   rd   ra   ÚlocationÚformat_for_json)r)   rH   r1   ÚdataÚheaderrP   r/   r/   r0   rG   Õ   s"    þ

 ÿ
z"ListCommand.output_package_listingc                 C   sb   t |ƒdkr| d|¡ t|ƒ\}}t |ƒdkrL| dd tdd„ |ƒ¡¡ |D ]}t|ƒ qPd S )Nr   ru   ú c                 S   s   d|  S )Nú-r/   )Úxr/   r/   r0   rs   ð   rt   z<ListCommand.output_package_listing_columns.<locals>.<lambda>)ÚlenÚinsertÚtabulateÚjoinÚmapr   )r)   r|   r}   Zpkg_stringsÚsizesÚvalr/   r/   r0   rx   ç   s    z*ListCommand.output_package_listing_columns)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úusager!   r9   rI   rE   rF   rD   rS   rG   rx   Ú__classcell__r/   r/   r-   r0   r      s   Pr   c                 C   s€   t | ƒdkst‚dgtdd„ | D ƒƒ }| D ]}dd„ t||ƒD ƒ}q,g }| D ](}d dd„ t||ƒD ƒ¡}| |¡ qN||fS )Nr   c                 s   s   | ]}t |ƒV  qd S rU   )r�   ©rO   r€   r/   r/   r0   rY   û   s     ztabulate.<locals>.<genexpr>c                 S   s"   g | ]\}}t |tt|ƒƒƒ‘qS r/   )Úmaxr�   rK   ©rO   ÚsÚcr/   r/   r0   rQ   ý   s     ztabulate.<locals>.<listcomp>r~   c                 S   s*   g | ]"\}}|d k	r"t |ƒ |¡nd‘qS )NÚ )rK   Úljustr�   r/   r/   r0   rQ     s   ÿ)r�   ÚAssertionErrorr�   r   r„   Úappend)Úvalsr†   ÚrowÚresultZdisplayr/   r/   r0   rƒ   ö   s    
ÿrƒ   c                 C   sØ   |j }|rddddg}nddg}g }|jdks@tdd„ | D ƒƒrJ| d¡ |jdkr^| d	¡ | D ]l}|j|jg}|rŽ| |j¡ | |j¡ |jdks t|ƒr¬| |j	¡ |jdkrÄ| t
|ƒ¡ | |¡ qb||fS )
z_
    Convert the package data into something usable
    by output_package_listing_columns.
    ÚPackageÚVersionZLatestÚTyperu   c                 s   s   | ]}t |ƒV  qd S rU   )r   rŽ   r/   r/   r0   rY     s     z%format_for_columns.<locals>.<genexpr>ZLocationZ	Installer)r?   ry   Úanyr–   rd   ra   rL   rm   r   rz   r   )Úpkgsr1   Zrunning_outdatedr}   r|   Zprojr˜   r/   r/   r0   rw     s(    



rw   c                 C   sv   g }| D ]b}|j t |j¡dœ}|jdkr@|j|d< t|ƒ|d< |jr`t |j¡|d< |j	|d< | 
|¡ qt |¡S )N)Únamera   ru   rz   Ú	installerrL   rm   )rd   r   Ú	text_typera   ry   rz   r   r?   rL   rm   r–   r   Údumps)rH   r1   r|   rP   Úinfor/   r/   r0   r{   -  s    
þ


r{   )#Ú
__future__r   r   ÚloggingÚpip._vendorr   Zpip._vendor.six.movesr   Zpip._internal.clir   Úpip._internal.cli.req_commandr   Úpip._internal.exceptionsr   Ú"pip._internal.index.package_finderr   Ú$pip._internal.models.selection_prefsr	   Ú!pip._internal.self_outdated_checkr
   Úpip._internal.utils.miscr   r   r   Úpip._internal.utils.packagingr   Zpip._vendor.packaging.versionr   Ú	getLoggerrˆ   Úloggerr   rƒ   rw   r{   r/   r/   r/   r0   Ú<module>   s&   
 Y%