B
    '\                 @   s   d Z ddlmZmZ ddlZ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 eeZd	d
 Zdd ZG dd deZdddZedZdd ZdddZdS )zy
Functions related to core conda functionality that relates to pip

NOTE: This modules used to in conda, as conda/pip.py
    )absolute_importprint_functionN)	getLogger   )CondaEnvException)any_subprocess)on_winc             C   s   t rtj|d}ntj|dd}|ddg|  }t|||d\}}}td t| td t| |d	kr~td
 t| d S )Nz
python.exebinZpythonz-mZpip)cwdz"Ran pip subprocess with arguments:zPip subprocess output:r   zPip subprocess error:)r   ospathjoinr   print)argsprefixr
   Zpython_pathZrun_argsstdoutstderrZrc r   1lib/python3.7/site-packages/conda_env/pip_util.pypip_subprocess   s    r   c             C   s8   t dg| \}}td|}|s*tdn
|d}|S )Nz-Vzpip\ (\d+\.\d+\.\d+)z+Failed to find pip version string in outputr   )r   researchr   group)r   r   r   pip_versionr   r   r   get_pip_version*   s    

r   c               @   s   e Zd Zdd ZdS )
PipPackagec             C   s6   d| kr"d| d | d | d f S d| d | d f S )Nr   z%s (%s)-%s-<pip>nameversionz%s-%s-<pip>r   )selfr   r   r   __str__5   s    zPipPackage.__str__N)__name__
__module____qualname__r   r   r   r   r   r   4   s   r   Tc             c   s  t | }t|ddd }tj }dg}|dkrB|ddg7 }ntd|td	< yt|| |d
\}}W n  tk
r   |rt	d d S X |dkrt
|}xd|D ]\}	|	d  |	d< d|	d kr|	d dd\}
}|
dd}
|
|	d< ||	d< tf |	V  qW ntd}x| D ]}| }|s0q||}|d krZ|rt	d|  q| \}}
| }||
d}	d|
kr|
d\}
}|
dd}
|	||
d tf |	V  qW d S )N.r   r   list	   z--formatjsonZlegacyZ
PIP_FORMAT)r   envz(# Warning: subprocess call to pip failedr   z, r   - r   z([\w.-]+)\s+\((.+)\)z+Could not extract name and version from: %r)r   r   )r   r   )r   intsplitr   environcopystrr   	Exceptionr   r&   loadslowerreplacer   r   compile
splitlinesstripmatchgroupsupdate)r   outputr   Zpip_major_versionr'   r   Z
pip_stdoutr   Zpkgskwargsr   r   Zpatlinemr   r   r   r   	installed;   sX    








r=   z[-_.]+c             C   s   t d|  S )Nr(   )_canonicalize_regexsubr1   )r   r   r   r   _canonicalize_name   s    r@   c             C   sd   t |tr| }dd |D }x@t| |dD ]0}t|d }||krNd|krNq,|t| q,W d S )Nc             S   s   h | ]}t |jqS r   )r@   r   ).0Zrecr   r   r   	<setcomp>   s    z$add_pip_installed.<locals>.<setcomp>)r9   r   r   )
isinstanceboolr=   r@   addr.   )r   Zinstalled_pkgsr&   r9   Zconda_namesZpip_pkgZpip_namer   r   r   add_pip_installed   s    
rF   )T)NT)__doc__Z
__future__r   r   r&   Zloggingr   r   r   
exceptionsr   Zconda.gateways.subprocessr   Zconda.exportsr   r    logr   r   dictr   r=   r3   r>   r@   rF   r   r   r   r   <module>   s    

P
