a
    vb                     @   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	Z	ddl
m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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_win)contextc                 C   s   t rtj|d}ntj|dd}|ddg|  }t|||d\}}}tjsrtjsrtd t| td t| |d	krtd
t	j
d t|t	j
d td||fS )Nz
python.exebinZpythonz-mZpip)cwdz"Ran pip subprocess with arguments:zPip subprocess output:r   zPip subprocess error:filez
Pip failed)r   ospathjoinr   r   quietjsonprintsysstderrr   )argsprefixr
   Zpython_pathZrun_argsstdoutr   Zrc r   1lib/python3.9/site-packages/conda_env/pip_util.pypip_subprocess   s    r   c                 C   s*   t d| }|r"|d  S dS dS )zEReturn the list of pip packages installed based on the command outputzSuccessfully installed\ (.*)   N)researchgroupstripsplit)r   mr   r   r   get_pip_installed_packages0   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   r   r   r   r   )r   r   r   pip_versionr   r   r   get_pip_version9   s    

r$   c                   @   s   e Zd Zdd ZdS )
PipPackagec                 C   s6   d| v 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__D   s    zPipPackage.__str__N)__name__
__module____qualname__r)   r   r   r   r   r%   C   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	< zt|| |d
\}}W n& ty   |rt	dt
jd Y d S 0 |dkrt|}|D ]`}	|	d  |	d< d|	d v r|	d dd\}
}|
dd}
|
|	d< ||	d< tf i |	V  qntd}| D ]}| }|s4q||}|d u rd|rt	d| t
jd q| \}}
| }||
d}	d|
v r|
d\}
}|
dd}
|	||
d tf i |	V  qd S )N.r   r   list	   z--formatr   ZlegacyZ
PIP_FORMAT)r   envz(# Warning: subprocess call to pip failedr   r&   z, r'   - r   z([\w.-]+)\s+\((.+)\)z+Could not extract name and version from: %r)r&   r'   )r   r'   )r$   intr    r   environcopystrr   	Exceptionr   r   r   r   loadslowerreplacer%   r   compile
splitlinesr   matchgroupsupdate)r   outputr#   Zpip_major_versionr0   r   Z
pip_stdoutr   Zpkgskwargsr'   r   patliner!   r&   r   r   r   	installedJ   s\    






rD   z[-_.]+c                 C   s   t d|  S )Nr1   )_canonicalize_regexsubr9   )r&   r   r   r   _canonicalize_name   s    rG   c                 C   s`   t |tr| }dd |D }t| |dD ]0}t|d }||v rLd|vrLq*|t| q*d S )Nc                 S   s   h | ]}t |jqS r   )rG   r&   ).0Zrecr   r   r   	<setcomp>       z$add_pip_installed.<locals>.<setcomp>)r@   r&   r   )
isinstanceboolrD   rG   addr6   )r   Zinstalled_pkgsr   r@   Zconda_namesZpip_pkgZpip_namer   r   r   add_pip_installed   s    
rN   )T)NT)__doc__Z
__future__r   r   r   Zloggingr   r   r   r   Zconda.exceptionsr   Zconda.gateways.subprocessr   Zconda.exportsr   Zconda.base.contextr   r*   logr   r"   r$   dictr%   rD   r;   rE   rG   rN   r   r   r   r   <module>   s&   	

P
