U
    d0                     @   sX  d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	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 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$m%Z% erd dl&m'Z' G dd deZ(e	e( Z)e*e+Z,G dd deZ-dee
eee.  ee. f dddZ/dee.dddZ0dS )    N)Values)TYPE_CHECKING	GeneratorListOptionalSequenceTuplecastcanonicalize_name)
cmdoptions)IndexGroupCommand)SUCCESS)CommandError)LinkCollector)PackageFinder)BaseDistributionget_environment)SelectionPreferences)
PipSession)stdlib_pkgs)tabulatewrite_output)DistributionVersionc                   @   s"   e Zd ZU dZeed< eed< dS )_DistWithLatestInfozGive the distribution object a couple of extra fields.

        These will be populated during ``get_outdated()``. This is dirty but
        makes the rest of the code much cleaner.
        latest_versionlatest_filetypeN)__name__
__module____qualname____doc__r   __annotations__str r#   r#   </tmp/pip-install-xwugzb3_/pip/pip/_internal/commands/list.pyr      s   
r   c                   @   s   e Zd ZdZdZdZddddZeee	dd	d
Z
eee edddZdeddddZdeddddZdeddddZdeed dddZdeddddZeee  ee ddddZdS )ListCommandzt
    List installed packages, including editables.

    Packages are listed in a case-insensitive sorted order.
    Tz
      %prog [options]N)returnc                 C   s*  | j jdddddd | j jddddd	d | j jd
ddddd | j jdddddd | j jdddddd | j t  | j jddddd | j jddddddd | j jddddd  | j jd!d"d#d$d  | j jd%dd#d&d'd( | j t  ttj| j}| jd)| | jd)| j  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jsonzSelect the output format among: columns (default), freeze, or json. The 'freeze' format cannot be used with the --outdated option.)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   )	cmd_opts
add_optionr   	list_pathZlist_excludemake_option_groupindex_groupparserinsert_option_group)self
index_optsr#   r#   r$   add_options2   s    

zListCommand.add_options)optionssessionr&   c                 C   s*   t j||d}td|jd}tj||dS )zK
        Create a package finder appropriate to this list command.
        )r@   F)allow_yankedallow_all_prereleases)link_collectorselection_prefs)r   creater   prer   )r=   r@   rA   rD   rE   r#   r#   r$   _build_package_finder   s    z!ListCommand._build_package_finder)r@   argsr&   c                 C   s   |j r|jrtd|j r,|jdkr,tdt| tt}|jrZ|	dd |jD  dd t
|jj|j|j|j|j|dD }|jr| ||}|j r| ||}n|jr| ||}| || tS )	Nz5Options --outdated and --uptodate cannot be combined.r0   z?List format 'freeze' cannot be used with the --outdated option.c                 s   s   | ]}t |V  qd S Nr
   ).0nr#   r#   r$   	<genexpr>   s     z"ListCommand.run.<locals>.<genexpr>c                 S   s   g | ]}t d |qS )r   )r	   )rK   dr#   r#   r$   
<listcomp>   s   z#ListCommand.run.<locals>.<listcomp>)
local_only	user_onlyeditables_onlyinclude_editablesskip)outdatedZuptodater   r.   r   check_list_path_optionsetr   Zexcludesupdater   pathZiter_installed_distributionslocalr+   editabler5   r3   get_not_requiredget_outdatedget_uptodateoutput_package_listingr   )r=   r@   rI   rT   packagesr#   r#   r$   run   s6    

zListCommand.run_ProcessedDistsr`   r@   r&   c                 C   s   dd |  ||D S )Nc                 S   s   g | ]}|j |jkr|qS r#   r   versionrK   distr#   r#   r$   rO      s   z,ListCommand.get_outdated.<locals>.<listcomp>iter_packages_latest_infosr=   r`   r@   r#   r#   r$   r]      s    
zListCommand.get_outdatedc                 C   s   dd |  ||D S )Nc                 S   s   g | ]}|j |jkr|qS r#   rd   rf   r#   r#   r$   rO      s   z,ListCommand.get_uptodate.<locals>.<listcomp>rh   rj   r#   r#   r$   r^      s    
zListCommand.get_uptodatec                    s$   dd |D  t  fdd|D S )Nc                 S   s(   h | ] }|  pd D ]}t|jqqS )r#   )Ziter_dependenciesr   name)rK   rg   depr#   r#   r$   	<setcomp>   s    z/ListCommand.get_not_required.<locals>.<setcomp>c                    s   h | ]}|j  kr|qS r#   canonical_name)rK   pkgZdep_keysr#   r$   rm      s     
 )listrj   r#   rq   r$   r\      s    	zListCommand.get_not_required)r   NNc              	   #   s^   |  J}| | dtd d fdd}t||D ]}|d k	r<|V  q<W 5 Q R X d S )Nr   )rg   r&   c                    sl     | j}js dd |D } j| jd}||}|d krDd S |j}|jjrXd}nd}|| _|| _	| S )Nc                 S   s   g | ]}|j js|qS r#   )re   is_prerelease)rK   	candidater#   r#   r$   rO      s   zOListCommand.iter_packages_latest_infos.<locals>.latest_info.<locals>.<listcomp>)project_namewheelsdist)
find_all_candidatesro   rG   make_candidate_evaluatorsort_best_candidatere   linkis_wheelr   r   )rg   Zall_candidatesZ	evaluatorbest_candidateremote_versiontypfinderr@   r#   r$   latest_info   s$    
z;ListCommand.iter_packages_latest_infos.<locals>.latest_info)_build_sessionrH   r   map)r=   r`   r@   rA   r   rg   r#   r   r$   ri      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rtt	|| d S )
Nc                 S   s   | j S rJ   rn   )rg   r#   r#   r$   <lambda>      z4ListCommand.output_package_listing.<locals>.<lambda>)keyr/   r0      z%s==%s (%s)z%s==%sr1   )
sortedr.   format_for_columnsoutput_package_listing_columnsverboser   raw_namere   locationformat_for_json)r=   r`   r@   dataheaderrg   r#   r#   r$   r_     s&    

   
z"ListCommand.output_package_listing)r   r   r&   c                 C   sb   t |dkr|d| t|\}}t |dkrL|ddtdd | |D ]}t| qPd S )Nr   r    c                 S   s   d|  S )N-r#   )xr#   r#   r$   r   ,  r   z<ListCommand.output_package_listing_columns.<locals>.<lambda>)leninsertr   joinr   r   )r=   r   r   Zpkg_stringsZsizesvalr#   r#   r$   r   !  s    z*ListCommand.output_package_listing_columns)r   r   r   r    ignore_require_venvusager?   r   r   r   rH   r   r"   intra   r]   r^   r\   r   ri   r_   r   r#   r#   r#   r$   r%   '   sB   Y * 
 
  ' 
 r%   rb   )pkgsr@   r&   c                 C   s   ddg}|j }|r |ddg tdd | D }|r@|d |jdkrT|d	 |jdkrh|d
 g }| D ]}|jt|jg}|r|t|j ||j	 |r||j
pd |jdkr||jpd |jdkr||j || qp||fS )z_
    Convert the package data into something usable
    by output_package_listing_columns.
    ZPackageVersionZLatestZTypec                 s   s   | ]}|j V  qd S rJ   )r[   )rK   r   r#   r#   r$   rM   ?  s     z%format_for_columns.<locals>.<genexpr>zEditable project locationr   ZLocationZ	Installer )rU   extendanyappendr   r   r"   re   r   r   editable_project_locationr   	installer)r   r@   r   Zrunning_outdatedZhas_editablesr   Zprojrowr#   r#   r$   r   2  s2    






r   rc   c                 C   s   g }| D ]r}|j t|jd}|jdkr@|jp0d|d< |j|d< |jr^t|j|d< |j|d< |j	}|rp||d< |
| qt|S )	N)rk   re   r   r   r   r   r   r   r   )r   r"   re   r   r   r   rU   r   r   r   r   r1   dumps)r`   r@   r   rg   infor   r#   r#   r$   r   _  s     


r   )1r1   loggingoptparser   typingr   r   r   r   r   r   r	   pip._vendor.packaging.utilsr   Zpip._internal.clir   pip._internal.cli.req_commandr   pip._internal.cli.status_codesr   pip._internal.exceptionsr   Zpip._internal.index.collectorr   Z"pip._internal.index.package_finderr   Zpip._internal.metadatar   r   $pip._internal.models.selection_prefsr   pip._internal.network.sessionr   pip._internal.utils.compatr   pip._internal.utils.miscr   r   Zpip._internal.metadata.baser   r   rb   	getLoggerr   loggerr%   r"   r   r   r#   r#   r#   r$   <module>   s8   $

   -