3
[x                 @   s   d dl mZ d dl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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 ejeZG dd deZ dd Z!dddZ"dd Z#dS )    )absolute_importN)OrderedDict)pkg_resources)parse)xmlrpc_client)Command)NO_MATCHES_FOUNDSUCCESS)PipXmlrpcTransport)CommandError)PyPI)get_terminal_size)
indent_logc                   s@   e Zd ZdZdZdZdZdZ fddZdd	 Z	d
d Z
  ZS )SearchCommandz@Search for PyPI packages whose name or summary contains <query>.searchz
      %prog [options] <query>zSearch PyPI for packages.Tc                s@   t t| j|| | jjddddtjdd | jjd| j d S )Nz-iz--indexindexURLz3Base URL of Python Package Index (default %default))destmetavardefaulthelpr   )	superr   __init__cmd_opts
add_optionr   pypi_urlparserinsert_option_group)selfargskw)	__class__ >/tmp/pip-install-65viz3iy/pip/pip/_internal/commands/search.pyr   !   s    zSearchCommand.__init__c             C   sT   |st d|}| j||}t|}d }tjj r<t d }t||d |rPtS t	S )Nz)Missing required argument (search query).r   )terminal_width)
r   r   transform_hitssysstdoutisattyr   print_resultsr	   r   )r   optionsr   query	pypi_hitshitsr$   r"   r"   r#   run,   s    

zSearchCommand.runc             C   sH   |j }| j|.}t||}tj||}|j||dd}|S Q R X d S )N)namesummaryor)r   _build_sessionr
   r   ServerProxyr   )r   r+   r*   	index_urlsession	transportpypir-   r"   r"   r#   r   <   s    
zSearchCommand.search)__name__
__module____qualname____doc__r/   usager0   ignore_require_venvr   r.   r   __classcell__r"   r"   )r!   r#   r      s   r   c             C   s   t  }xv| D ]n}|d }|d }|d }||j krH|||gd||< q|| d j| |t|| d kr||| d< qW t|j S )z
    The list from pypi is really a list of versions. We want a list of
    packages with the list of versions stored inline. This converts the
    list from pypi into one we can use.
    r/   r0   version)r/   r0   versionsr@   )r   keysappendhighest_versionlistvalues)r-   packageshitr/   r0   r?   r"   r"   r#   r%   E   s    
r%   c             C   sH  | sd S |d kr&t dd | D d }dd tjD }x
| D ] }|d }|d pVd}t|jdd	g}|d k	r|| d
 }|dkrtj||}dd|d   j|}d|d||f |f }	yjtj	|	 ||kr&tj
|}
t < |
j|krtj	d|
j ntj	d|
j tj	d| W d Q R X W q> tk
r>   Y q>X q>W d S )Nc             S   s.   g | ]&}t |d  t t|jddg qS )r/   r@   -)lenrC   get).0rG   r"   r"   r#   
<listcomp>f   s   z!print_results.<locals>.<listcomp>   c             S   s   g | ]
}|j qS r"   )project_name)rK   pr"   r"   r#   rL   j   s    r/   r0    r@   rH      
   
    z	%-*s - %sz%s (%s)zINSTALLED: %s (latest)zINSTALLED: %szLATEST:    %s)maxr   working_setrC   rJ   textwrapwrapjoinloggerinfoget_distributionr   r?   UnicodeEncodeError)r-   name_column_widthr$   installed_packagesrG   r/   r0   latesttarget_widthlinedistr"   r"   r#   r)   a   s<    


r)   c             C   s   t | tdS )N)key)rV   parse_version)r@   r"   r"   r#   rC      s    rC   )NN)$
__future__r   loggingr&   rX   collectionsr   pip._vendorr   Zpip._vendor.packaging.versionr   rf   pip._vendor.six.movesr   Zpip._internal.cli.base_commandr   Zpip._internal.cli.status_codesr   r	   pip._internal.downloadr
   pip._internal.exceptionsr   pip._internal.models.indexr   Zpip._internal.utils.compatr   pip._internal.utils.loggingr   	getLoggerr8   r[   r   r%   r)   rC   r"   r"   r"   r#   <module>   s&   
,
%