
    wBfB&                     D   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& dZ'd Z(	 ddZ)d Z*d Z+d Z, G d de-      Z. G d d      Z/d Z0d Z1y) z>Environment object describing the conda environment.yaml file.    N)chain)abspath
expanduser
expandvars   )context)commoninstall)groupby_to_dict)unique)yaml_safe_dumpyaml_safe_load)
PrefixData)EnvironmentFileEmptyEnvironmentFileNotFound)download_text)CONDA_SESSION_SCHEMES)History)PackageType)	MatchSpec)PrefixGraph)namedependenciesprefixchannels	variablesc           	         g }| r| j                         ni }| j                         D ]-  }|t        vs|j                  |       |j	                  |       / |ri|j                  d      }t        |      dk7  rdnd}t        |      dk7  rdnd}t        d| d| d	| d
       |D ]  }t        d|         t                | j                  dg       }t        j                  d      fdt        fd|D              }	|D ]=  }
t        |
t              sd|
v s|	rt        d       |d   j                  dd        |S  |S )z7Check for unknown keys, remove them and print a warningfilename   areiss z2
EnvironmentSectionNotValid: The following sectionz on 'z' z invalid and will be ignored:z - r   z[<>~\s=]c                 N    dj                  |       d   j                  d      v S )Npipr   z::)split)depdepsplits    -lib/python3.12/site-packages/conda/env/env.py<lambda>zvalidate_keys.<locals>.<lambda>3   s%    (.."5a"8">">t"DD     c              3   P   K   | ]  }t        |t              r |        y wN)
isinstancedict).0r'   is_pips     r)   	<genexpr>z validate_keys.<locals>.<genexpr>4   s     MCz#t7LF3KMs   &&r%   aE  Warning: you have pip-installed dependencies in your environment file, but you do not list pip itself as one of your conda dependencies.  Conda may not use the correct pip to install your packages, and they may end up in the wrong place.  Please add an explicit pip dependency.  I'm adding one for you, but still nagging you.r   )copykeys
VALID_KEYSappendpopgetlenprintrecompileanyr.   r/   insert)datakwargsinvalid_keysnew_datakeyr   verbpluraldeps	lists_pipr'   r(   r1   s              @@r)   validate_keysrH      s[   L"tyy{Hyy{ j $LL
 ::j)L)Q.uDL)Q.BA& JzD6!>@	
   	CCu+	88NB'Dzz+&HDFM4MMI 
c4 Uc\)3 ^$++Au5O
 Or+   c           	      h   t        |d      }|j                         }|rft        |      j                         }|j	                         D cg c]  }t        |       }	}t        | |	t        t        j                        ||      S t        t        |j                               j                        }
t        d |
      }t        g |j!                  dd      |j!                  t"        j$                  d      |j!                  t"        j&                  d      d       }t        g |j!                  t"        j(                  d      |j!                  t"        j*                  d      |j!                  t"        j,                  d      d	       }|r5|D cg c])  }d
j/                  |j0                  |j2                  f      + }}n?|D cg c]4  }d
j/                  |j0                  |j2                  |j4                  f      6 }}|r:|j7                  d|D cg c]  }|j0                   d|j2                    c}i       t        t        j                        }|s4|D ]/  }|j8                  j:                  }||vs|j=                  d|       1 t        | ||||      S c c}w c c}w c c}w c c}w )aj  
        Get ``Environment`` object from prefix
    Args:
        name: The name of environment
        prefix: The path of prefix
        no_builds: Whether has build requirement
        ignore_channels: whether ignore_channels
        from_history: Whether environment file should be based on explicit specs in history

    Returns:     Environment object
    T)pip_interop_enabled)r   r   r   r   r   c                     | j                   S r-   )package_typexs    r)   r*   z"from_environment.<locals>.<lambda>`   s
    ann r+   N c                     | j                   S r-   r   rM   s    r)   r*   z"from_environment.<locals>.<lambda>g   
    aff r+   )rC   c                     | j                   S r-   rQ   rM   s    r)   r*   z"from_environment.<locals>.<lambda>p   rR   r+   =r%   z==r   )r   get_environment_env_varsr   get_requested_specs_mapvaluesstrEnvironmentlistr   r   tupler   iter_recordsgraphgroupbysortedr8   r   NOARCH_GENERICNOARCH_PYTHONVIRTUAL_PYTHON_WHEELVIRTUAL_PYTHON_EGG_MANAGEABLEVIRTUAL_PYTHON_EGG_UNMANAGEABLEjoinr   versionbuildr6   channelcanonical_namer>   )r   r   	no_buildsignore_channelsfrom_historypdr   historypackagerF   precsgrouped_precsconda_precs	pip_precsar   r   precri   s                      r)   from_environmentrv   C   su    
F	5B++-I&/99;,3NN,<=G=='**+
 	
 +boo/0667E4e<M	
tR(	
{992>	
 {88"=	

 K 	
{??D	
{HH"M	
 {JJBO	

 I ?JK!!&&!))!45KKHST1!&&!))QWW!=>TTUy$Q!xr!))%=$QRSG$$%H 	3D!\\88NX->2	3 ! U >< LT$Qs   J .J%
9J*"J/
c                     t        |       }|j                  d      }|t        |      t        ||      }||j	                         D ]
  \  }}|||<    t        |       t        di |S )z>Load and return a ``Environment`` from a given ``yaml`` stringr   rO   )r   r8   r   rH   items_expand_channelsrY   )yamlstrr@   r?   r   rC   values         r)   	from_yamlr|      st    '"Dzz*%H|"8,,v&D ,,. 	JCDI	Tr+   c                     | j                  dg       D cg c]!  }t        j                  j                  |      # c}| d<   yc c}w )zMExpands ``Environment`` variables for the channels found in the ``yaml`` datar   N)r8   ospathr   )r?   rh   s     r)   ry   ry      s=     4888J3K(/7#D s   &Ac                    | j                  dd      d   }|t        v rt        |       }nat        j                  j                  |       st        |       t        | d      5 }|j                         }	 |j                  d      }ddd       t        |       S # t        $ r |j                  d      }Y 1w xY w# 1 sw Y   6xY w)	z4Load and return an ``Environment`` from a given file://r   r   rbzutf-8zutf-16N)r   )r&   r   r   r~   r   existsr   openreaddecodeUnicodeDecodeErrorr|   )r   
url_schemerz   fpyamlbs        r)   	from_filer      s    q)!,J**)WW^^H%%h//(D! 	1RGGIE1,,w/	1 Wx00 & 1,,x01		1 	1s*    B82BB52B84B55B88Cc                   .     e Zd ZdZ fdZd Zd Z xZS )DependencieszNA ``dict`` subclass that parses the raw dependencies into a conda and pip listc                 R    t        |   |i | || _        | j                          y r-   )super__init__rawparse)selfr   argsr@   	__class__s       r)   r   zDependencies.__init__   s%    $)&)

r+   c                 f   | j                   sy| j                  dg i       | j                   D ]K  }t        |t              r| j                  |       %| d   j	                  t        j                  |             M d| v r3| d   s| d= t        d | d   D              s| d   j	                  d       yyy)z4Parse the raw dependencies into a conda and pip listNcondar%   c              3   L   K   | ]  }t        |      j                  d k(    yw)r%   N)r   r   )r0   r"   s     r)   r2   z%Dependencies.parse.<locals>.<genexpr>   s     Iay|((E1Is   "$)r   updater.   r/   r6   r	   arg2specr=   )r   lines     r)   r   zDependencies.parse   s    xxWbM"HH 	<D$%D!W$$V__T%:;		< D=;KI4=IIW$$U+ J r+   c                 Z    | j                   j                  |       | j                          y)z$Add a package to the ``Environment``N)r   r6   r   )r   package_names     r)   addzDependencies.add   s    %

r+   )__name__
__module____qualname____doc__r   r   r   __classcell__)r   s   @r)   r   r      s    X
,(r+   r   c                   F    e Zd ZdZ	 	 	 	 	 	 d	dZd Zd Zd
dZd
dZd Z	y)rY   z1A class representing an ``environment.yaml`` fileNc                 r    || _         || _        || _        t        |      | _        || _        |g }|| _        y r-   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   s          r)   r   zEnvironment.__init__   s?     	 (6"H r+   c                 t    t        t        t        j                  || j                  f                  | _        y)z#Add channels to the ``Environment``N)rZ   r   r   from_iterabler   )r   r   s     r)   add_channelszEnvironment.add_channels   s'    VE$7$74==8Q$RSTr+   c                     g | _         y)z,Remove all channels from the ``Environment``N)r   )r   s    r)   remove_channelszEnvironment.remove_channels   s	    r+   c                 \   d| j                   i}| j                  r| j                  |d<   | j                  r| j                  j                  |d<   | j                  r| j                  |d<   | j
                  r| j
                  |d<   ||S |j                  t        j                  |             y)zDConvert information related to the ``Environment`` into a dictionaryr   r   r   r   r   N)	r   r   r   r   r   r   writejsondumps)r   streamds      r)   to_dictzEnvironment.to_dict   s    TYY== MMAjM $ 1 1 5 5An>>!^^AkN;;++AhK>HTZZ]#r+   c                 D    | j                         }t        ||      }||S y)zIConvert information related to the ``Environment`` into a ``yaml`` stringN)r   r   )r   r   r   outs       r)   to_yamlzEnvironment.to_yaml   s(    LLNQ'>J r+   c                 ~    t        | j                  d      5 }| j                  |       ddd       y# 1 sw Y   yxY w)z0Save the ``Environment`` data to a ``yaml`` filewb)r   N)r   r   r   )r   r   s     r)   savezEnvironment.save  s5    $--& 	$"LLL#	$ 	$ 	$s   3<)NNNNNNr-   )
r   r   r   r   r   r   r   r   r   r   rO   r+   r)   rY   rY      s8    ; !&U$$r+   rY   c                 z    | j                  dd      d   }|t        v r| S t        t        t	        |                   S )z3Expand filename if local path or return the ``url``r   r   r   )r&   r   r   r   r   )r   r   s     r)   get_filenamer     s:    q)!,J**z*X"6788r+   c                    t         j                  rS|d   |d   t        j                  d       y|d   |d   }ni }|d   |d   |d<   t        j                  ||       yt	        j
                  | j                  xs |       y)z(Print the result of an install operationr   Nr%   z)All requested packages already installed.)messagePIP)r   actions)r   r   r	   stdout_json_successr
   print_activater   )r   r   resultr   s       r)   print_resultr     s    ||'?"ve}'<&&C g* /e}(!'&&fgFtyy2F3r+   )FFF)2r   r   r~   r;   	itertoolsr   os.pathr   r   r   base.contextr   clir	   r
   common.iteratorsr   r^   r   common.serializer   r   core.prefix_datar   
exceptionsr   r   gateways.connection.downloadr   gateways.connection.sessionr   rn   r   models.enumsr   models.match_specr   models.prefix_graphr   r5   rH   rv   r|   ry   r   r/   r   rY   r   r   rO   r+   r)   <module>r      s    E  	 	  3 3 " ! 9 % = ) F 8 ?  & ) -H
$P HMCL1"4 D7$ 7$t94r+   