
    <BhB                    D   S r SSKJr  SSKrSSKJrJrJr  SSKJ	r	  SSK
JrJrJrJrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKJr  SSKJr  SSKJr  SSK J!r!  \(       a  SSK"J#r#  \	" \$5      r%SSS jjr&SS jr'SS jr(SS jr)SS jr* S     SS jjr+S S jr,g)!z&Tools for managing conda environments.    )annotationsN)EACCESENOENTEROFS)	getLogger)dirnameisdirisfilejoinnormpath)TYPE_CHECKING   )context)is_admin)ensure_text_typeon_win	open_utf8)expand)yield_linesis_conda_environment   )
PrefixData)Iteratorc                .    [        [        U SS5      5      $ )z
Gets the path to the user's environments.txt file.

:param userhome: The home directory of the user.
:type userhome: str
:return: Path to the environments.txt file.
:rtype: str
z.condazenvironments.txt)r   r   )userhomes    7lib/python3.13/site-packages/conda/core/envs_manager.pyget_user_environments_txt_filer      s     $x+=>??    c                   [         R                  (       d  g[        5       n[        U 5      n [	        U 5      n [
        R                  " U5        SU ;   d  SU ;   d  U[
        R                  :X  a  gU [        U5      ;   a  g[
        R                  R	                  U5      n [
        R                  " USS9   [        US5       nUR                  [        U 5      5        UR                  S	5        SSS5        g!    N= f! [         a%  n[        R                  SU SU 35         SnAgSnAff = f! , (       d  f       g= f! [         aA  nUR                   ["        [$        [&        4;   a  [        R                  S
U U5         SnAge SnAff = f)z
Registers an environment by adding it to environments.txt file.

:param location: The file path of the environment to register.
:type location: str
:return: None
Nplacehold_pl	skeleton_T)exist_okz1Unable to register environment. Could not create z
. Reason: a
zlUnable to register environment. Path not writable or missing.
  environment location: %s
  registry file: %s)r   register_envsr   r   r   osmakedirsdevnullr   pathOSErrorlogwarningr   writer   errnor   r   r   )locationuser_environments_txt_filefolderuser_environments_txt_directoryexcfhes          r   register_envr7   '   sh      !?!A!HXF
F
 	("("%3 	;9::&(ggoo6P&Q#
3dC1372HH%h/0HHTN 877"    ?@ Ae	

 	 87  
77vuf--KK& *  
s_   C6 C= 5E  ,D/-E  6C:=
D,D''D,/
D=9E  =E   
F
6FFFc                    [        U 5      (       aQ  [        U S5      n[        U5      (       a5  [        S [        R                  " U5      5       5      n[        U5      S:  a  g[        [        5       U 5        g)a  
Unregisters an environment by removing its entry from the environments.txt file if certain conditions are met.

The environment is only unregistered if its associated 'conda-meta' directory exists and contains no significant files other than 'history'. If these conditions are met, the environment's path is removed from environments.txt.

:param location: The file path of the environment to unregister.
:type location: str
:return: None
z
conda-metac              3  8   #    U  H  oR                   v   M     g 7fN)name.0entrys     r   	<genexpr>!unregister_env.<locals>.<genexpr>o   s     %S>RUjj>R   r   N)r	   r   tupler'   scandirlen_clean_environments_txtr   )r0   meta_dirmeta_dir_contentss      r   unregister_envrH   b   s]     X,/?? %%Sbjj>R%S S$%) :<hGr   c                   ^ [        5       m[        5       (       au  [        (       a:  [        [	        S5      5      n [        S [        R                  " U 5      5       5      nO<SSKJ	n  [        S U" 5       5       5      =(       d    [	        S5      4nO[	        S5      4n[        SU5       H;  n[        U5      n[        U5      (       d  M    TR                  [        U5      5        M=     S [$        R&                  5       nTR                  U4S	 jS
 U5       5       5        TR)                  [$        R*                  5        [-        T5      $ ! [         a    [         R#                  SU 35         M  f = f)zy
Lists all known conda environment prefixes.

:return: A list of all known conda environment prefixes.
:rtype: List[str]
~c              3  8   #    U  H  oR                   v   M     g 7fr:   )r*   r<   s     r   r?   *list_all_known_prefixes.<locals>.<genexpr>   s     Q8Pu

8PrA   r   )getpwallc              3  8   #    U  H  oR                   v   M     g 7fr:   )pw_dir)r=   pwentrys     r   r?   rL      s     Ij7jrA   NzUnable to access c              3  J   #    U  H  n[        U5      (       d  M  Uv   M     g 7fr:   )r	   )r=   envs_dirs     r   r?   rL      s     O*;huX*;s   #	#c              3  x   >#    U  H/  nUT;  d  M  [        U5      R                  5       (       d  M+  Uv   M1     g 7fr:   )r   is_environment)r=   r*   all_env_pathss     r   r?   rL      s@      
D }$	 	 *4D)9)H)H)J	 	
s   
::	:c              3  t   #    U  H.  n[         R                  " U5        H  o"R                  v   M     M0     g 7fr:   )r'   rC   r*   )r=   rR   r>   s      r   r?   rL      s'      
'08bjj>RUJJ>RJys   68)setr   r   r   r   rB   r'   rC   pwdrM   filterr   r
   updaterE   PermissionErrorr,   r-   r   	envs_dirsaddroot_prefixsorted)home_dir_dirsearch_dirsrM   home_direnvironments_txt_filer\   rU   s         @r   list_all_known_prefixesrd   x   s:    EMzz6"6#;/LQ

<8PQQK$IhjII sNK c{n4- >x H'((I $$%<=R%ST . P'*;*;OI 
'0
  g))*-   # I/0E/FGHIs   	E"E-,E-c              #     #    [        5        H4  n[        [        U5      R                  U 5      5      nU(       d  M/  X4v   M6     g7f)z
Queries all known prefixes for a given specification.

:param spec: The specification to query for.
:type spec: str
:return: An iterator of tuples containing the prefix and the query results.
:rtype: Iterator[Tuple[str, Tuple]]
N)rd   rB   r   query)specprefixprefix_recss      r   query_all_prefixesrj      s<      *+Jv.44T:;;%% ,s
   6A
Ac                   ^ [        U 5      (       d  gT(       a  [        T5      m[        [        U 5      5      n[        U4S jU5       5      nX2:w  a  [	        X5        U$ )aM  
Cleans the environments.txt file by removing specified locations.

:param environments_txt_file: The file path of environments.txt.
:param remove_location: Optional location to remove from the file.
:type environments_txt_file: str
:type remove_location: Optional[str]
:return: A tuple of the cleaned lines.
:rtype: Tuple[str, ...]
 c              3  \   >#    U  H!  nUT:w  d  M  [        U5      (       d  M  Uv   M#     g 7fr:   r   )r=   rh   remove_locations     r   r?   *_clean_environments_txt.<locals>.<genexpr>   s/      +,F_$ 	)=f)E 	,s   
,,	,)r
   r   rB   r   _rewrite_environments_txt)rc   rn   environments_txt_linesenvironments_txt_lines_cleaneds    `  r   rE   rE      sa     '(("?3";/D#EF%* +,+ &"
 &?!"7X))r   c                8    [        U S5       nUR                  SR                  U5      5        UR                  S5        SSS5        g! , (       d  f       g= f! [         a5  n[        R                  SU 5        [        R                  SUSS9   SnAgSnAff = f)a  
Rewrites the environments.txt file with the specified prefixes.

:param environments_txt_file: The file path of environments.txt.
:param prefixes: List of prefixes to write into the file.
:type environments_txt_file: str
:type prefixes: List[str]
:return: None
wr%   NzFile not cleaned: %sz%rT)exc_info)r   r.   r   r+   r,   infodebug)rc   prefixesr5   r6   s       r   rp   rp      ss    *,c2bHHTYYx()HHTN 322  *')>?		$D	)*s4   A 2A	 A 	
AA A 
B$+BB)rJ   )r   strreturnry   )r0   ry   rz   None)rz   	list[str])rg   ry   rz   zIterator[tuple[str, tuple]]r:   )rc   ry   rn   z
str | Nonerz   ztuple[str, ...])rc   ry   rx   r|   rz   r{   )-__doc__
__future__r   r'   r/   r   r   r   loggingr   os.pathr   r	   r
   r   r   typingr   base.contextr   
common._osr   common.compatr   r   r   common.pathr   gateways.disk.readr   gateways.disk.testr   prefix_datar   collections.abcr   __name__r,   r   r7   rH   rd   rj   rE   rp   rl   r   r   <module>r      s    - " 	 ' '  : :   " ! ? ?   , 5 #(	@8vH,+!\&" #'*** *<*r   