
    Je                         d Z ddlmZ ddlZddlZddlmZ ddlZddlm	Z	m
Z
mZ ddlmZ ej                  dk(  Zi ad Zd Zdd	Zdd
ZddZddZd Zd Zd Zej4                  fdZd Zy)zConda/anaconda utilities.    )globN)find_programrun_programrun_shell_command)get_spyder_umamba_pathntc                      i } t         j                  dk(  r3ddg}|D ]*  \  }}t         j                  j                  ||      }|| |<   , | S )z
    Required environment variables for Conda to work as expected.

    Notes
    -----
    This is needed on Windows since Conda 23.9.0
    r   )	HOMEDRIVEzC:)HOMEPATHz\Users\xxxxx)osnameenvironget)envenv_varsvardefaultvalues        4/usr/lib/python3/dist-packages/spyder/utils/conda.py_env_for_condar      sV     C	ww$')GH$ 	LCJJNN30ECH	 J    c                 @    d| v rd| vrdnd}dj                  ||       S )z+Return quotes if needed for spaces on path. " z{quotes}{path}{quotes})quotespath)format)r   r   s     r   
add_quotesr   )   s+    4KCtOSF#**&t*DDr   c                    ||j                  dd      }| || r|rt        d      |r| t        |      j                  dd      } t        j                  j                  t        j                  j                  | d            S )z@Check if prefix or python executable are in a conda environment.\/z-Only `prefix` or `pyexec` should be provided!
conda-meta)replace
ValueErrorget_conda_env_pathr   r   existsjoin)prefixpyexecs     r   is_conda_envr+   /   sq    c*6>v&HII&.#F+33D#>77>>"'',,v|<==r   c                     | t         j                  }nt        |       }|j                  dd      }d}|j	                  |      dk7  r|j                  |      d   }n|}|rt        |      }|S )z~
    Return conda prefix from pyexec path

    If `quote` is True, then quotes are added if spaces are found in the path.
    r!   r"   z/envs/r   )sysr)   r&   r$   rfindsplitr   )r*   quoteconda_env_prefixenv_keyroot_prefixs        r   get_conda_root_prefixr5   =   sv     ~::-f5'//c:Gg&",&,,W5a8& -r   c                 P   t               xs
 t               }t        j                  |      j	                  d      r|}nIt        j
                  t        j
                  |            }t        rd}nd}t        j                  ||      }|j                  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.
    
micromambazScripts/activatezbin/activater!   r"   )
r   
find_condaospbasename
startswithdirnameWINDOWSr(   r$   r   )r1   exescript_pathconda_exe_rootactivates        r   get_conda_activation_scriptrB   V   s     !
"
2jlC
||C##L1 S[[%56)H%Hhh~x8%%dC0K -r   c                 "   | j                  dd      } t        j                  dk(  r t        j                  j	                  |       }n<t        j                  j	                  t        j                  j	                  |             }|rt        |      }|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   r<   r   )r*   r1   	conda_envs      r   r&   r&   s   sc     ^^D#&F	ww$GGOOF+	GGOOBGGOOF$;<	y)	r   c                      t         j                  j                  d      xs t         j                  j                  d      } | t        rdnd}t	        |      } | S )zFind conda executable.	CONDA_EXE	MAMBA_EXEz	conda.batconda)r   r   r   r=   r   )rH   
conda_execs     r   r8   r8      sE     JJNN;'F2::>>++FE}$+[
Z(Lr   c                  R   i } t               }|| S dj                  |dddg      }	 t        |t                     j	                         \  }}|j                         }t        j                  |      }|d   D ]  }|j                  t        j                        d   }t        rt        j                  |d      nt        j                  |d	d
      }t        j                  |      so	 t        |dg      j	                         \  }}|j                         }|j                         j!                  d      s|j                         j!                  d      rdn|}dj#                  |      }||j%                         f| |<    | a| S # t        $ r dg i}Y "w xY w# t        $ r d}Y w xY w)z6Return the list of all conda envs found in the system.r   r   list--jsonr   envsr-   z
python.exebinpythonz	--versionr   anaconda	minicondabasez	conda: {})r8   r(   r   r   communicatedecodejsonloads	Exceptionr0   r9   sepr=   isfiler   lowerr;   r   stripCONDA_ENV_LIST_CACHE)	env_listrH   cmdstrout__r   r   r   versions	            r   get_list_conda_envsrc      s    HLE}XXueVX67F#F0@AMMORjjljjo 6{ 1yy!"%.5sxx\*388<" zz$	%d[M:FFHKGRnn&G **,11*=

''4 :> 	!!$'0%1( $O1  rl  	G	s$   AF '.FFFF&%F&c                      t         S )z5Return a cache of envs to avoid computing them again.)r]    r   r   get_list_conda_envs_cacherf      s    r   c                     t        |       r5t        j                  | d      }t        | t        j
                   d      ryy)z1Detect if the anaconda meta package is installed.r#   zanaconda-[0-9]*.jsonTF)r+   r9   r(   r   r   rY   )r)   
conda_metas     r   is_anaconda_pkgri      s8    FXXfl3
:,rvvh&:;<r   c                  z   t               } | yt        t        j                        }dj	                  | dddd|g      }	 t        |t                     j                         \  }}|j                         }t        j                  |      }|D ]  }|d	   dk(  s|d
   }|d   } dv rd}|fS # t        $ r Y yw xY w)z6Get the conda channel from which Spyder was installed.NNNr   rK   spyderrL   z--prefixrM   r   channelbase_urlz	<develop>)r8   r&   r.   
executabler(   r   r   rT   rU   rV   rW   rX   )rH   r   r_   r`   ra   package_inform   channel_urls           r   get_spyder_conda_channelrr      s    LE}
S^^
,CXXufh*cJKF#F0@AMMORjjljjo  38+"9-G&z2K3
 k!K  s   AB. .	B:9B:rk   )NF)F)__doc__r   rV   r   os.pathr   r9   r.   spyder.utils.programsr   r   r   spyder.config.baser   r   r=   r]   r   r   r+   r5   rB   r&   r8   rc   rf   r)   ri   rr   re   r   r   <module>rw      sy        	  
 N N 5
''T/ $E>2:$'T 
 ::  r   