a
    a,                     @   s   d Z ddlZddlZddlmZ ddlZddlZddlm	Z	m
Z
 ddlmZmZmZ ejdkZi add Zddd	ZdddZdddZdddZdd Zdd Zdd ZdS )zConda/anaconda utilities.    N)running_in_mac_appget_home_dir)find_programrun_programrun_shell_commandntc                 C   s&   d| v rd| vrdnd}dj || dS )z+Return quotes if needed for spaces on path. " z{quotes}{path}{quotes})quotespath)format)r   r    r   1lib/python3.9/site-packages/spyder/utils/conda.py
add_quotes   s    r   c                 C   sf   |dur| dd}| du r$|du s,| r4|r4td|rP| du rPt| dd} tjtj| dS )z@Check if prefix or python executable are in a conda environment.N\/z-Only `prefix` or `pyexec` should be provided!z
conda-meta)replace
ValueErrorget_conda_env_pathosr   existsjoin)prefixpyexecr   r   r   is_conda_env   s    r   Fc                 C   sZ   | du rt j}nt| }|dd}d}||dkrF||d }n|}|rVt|}|S )z~
    Return conda prefix from pyexec path

    If `quote` is True, then quotes are added if spaces are found in the path.
    Nr   r   z/envs/r   )sysr   r   r   rfindsplitr   )r   quoteZconda_env_prefixZenv_keyZroot_prefixr   r   r   get_conda_root_prefix-   s    r!   c                 C   sB   t jdkrd}nd}t jt| dd|dd}|r>t|}|S )z
    Return full path to conda activation script.

    If `quote` is True, then quotes are added if spaces are found in the path.
    r   zScripts/activatezbin/activateF)r    r   r   )r   namer   r   r!   r   r   )r   r    ZactivateZscript_pathr   r   r   get_conda_activation_scriptF   s    
r#   c                 C   sH   |  dd} tjdkr$tj| }ntjtj| }|rDt|}|S )z
    Return the full path to the conda environment from give python executable.

    If `quote` is True, then quotes are added if spaces are found in the path.
    r   r   r   )r   r   r"   r   dirnamer   )r   r    Z	conda_envr   r   r   r   Z   s    
r   c                  C   s   t rdnd} t| }|S )zFind conda executable.z	conda.batconda)WINDOWSr   )Z
conda_execr%   r   r   r   
find_condal   s    r'   c            	   	   C   s*  i } t  }|du r| S d|dddg}z*t|i d \}}| }t|}W n tyl   dg i}Y n0 |d D ]}|t	j
d }trt	|d	nt	|d
d}z t|dg \}}| }W n ty   d}Y n0 | ds| drdn|}d|}|| f| |< qv| a| S )z6Return the list of all conda envs found in the system.Nr   envlistz--json)r(   Zenvsr   z
python.exebinZpythonz	--versionr
   ZanacondaZ	minicondabasez	conda: {})r'   r   r   Zcommunicatedecodejsonloads	Exceptionr   ospsepr&   r   lower
startswithr   stripCONDA_ENV_LIST_CACHE)	Zenv_listr%   Zcmdstrout__r(   r"   r   versionr   r   r   get_list_conda_envss   s<    

r9   c                   C   s   t S )z5Return a cache of envs to avoid computing them again.)r5   r   r   r   r   get_list_conda_envs_cache   s    r:   )NN)NF)NF)F)__doc__r-   r   os.pathr   r0   
subprocessr   Zspyder.config.baser   r   Zspyder.utils.programsr   r   r   r"   r&   r5   r   r   r!   r#   r   r'   r9   r:   r   r   r   r   <module>   s"   




%