
    &`i89                        d dl Z d dlZd dlZd dlZd dl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mZ d dlZd dlmZ d dlZd dlmZmZ d dlmZmZ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'm(Z(m)Z)  ej*        e+          Z,ej-        dk    Z.de/fdZ0d Z1d Z2e,fdeej3                 fdZ4d Z5e,fdeej3                 dee/         fdZ6	 d deeef         de/deee/                  deeef         fdZ7dede/fdZ8dedee/         fdZ9e,fdddeej3                 dee/ef         fdZ: G d de"          Z;dS )!    N)Path)AnyDictListOptional)FileLock)get_or_create_event_looptry_to_create_directory)create_conda_env_if_neededdelete_conda_envget_conda_activate_commandsget_conda_envsget_conda_info_json)RuntimeEnvContext)Protocol	parse_uri)RuntimeEnvPlugin)parse_and_validate_conda)get_directory_size_bytesget_master_wheel_urlget_release_wheel_urlget_wheel_filenamentreturnc                      t           j                            t           j                            t          j                  d                   d         S )Nr   )ospathsplitray__file__     r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/conda.py_resolve_current_ray_pathr$   +   s2     7==s|44Q788;;r"   c                      t                      } t          j                            | d          }t	          j        |          d         S )zFind the Ray setup_spec from the currently running Ray.

    This function works even when Ray is built from source with pip install -e.
    zsetup.py
setup_spec)r$   r   r   joinrunpyrun_path)ray_source_python_pathsetup_py_paths     r#   _get_ray_setup_specr,   5   s;    
 788GLL!7DDM>-((66r"   c                      t                      j        t                      j        d         z   } t          t	          |                     S )z<Find the Ray dependencies when Ray is installed from source.default)r,   install_requiresextraslistset)depss    r#   -_resolve_install_from_source_ray_dependenciesr4   ?   s=     	.1D1F1F1Mi1XX 	 D		??r"   loggerc                    t           r!t          j                            | d          }n t          j                            | d          }t	          j        |ddg                                                                          }t                      }|	                    d| d| d           t          j                            |d          }t          j        
                    |          r,|	                    d	|            t          j        |           t          t          j                            |d
          d          5 }|                    |           ddd           dS # 1 swxY w Y   dS )z:Write the current Ray site package directory to a new sitepythonz
bin/pythonz-cz9import sysconfig; print(sysconfig.get_paths()['purelib'])z
Injecting z to environment site-packages z( because _inject_current_ray flag is on.r   z)Replacing existing ray installation with zray_shared.pthwN)_WIN32r   r   r'   
subprocesscheck_outputdecodestripr$   warningisdirshutilrmtreeopenwrite)
conda_pathr5   python_binarysite_packages_pathray_pathmaybe_ray_dirfs          r#   _inject_ray_to_conda_siterJ   H   s     ?Z::Z>>K	
 	
 
	  )**H
NN	2X 	2 	2=O 	2 	2 	2  
 GLL!3U;;M	w}}]## %M8MMNNNm$$$ 
bgll-/?@@#	F	F !	                 s   E**E.1E.c                  v    d                     t          t          t          j        d d                             S )N.   )r'   mapstrsysversion_infor!   r"   r#   _current_py_versionrR   m   s*    88CS-bqb122333r"   c                    t           j                            d          rat           j                            t          t          j                                                  j	        d         dt                                S t          j        dk    r:t           j                            d          dk    r|                     d           dS d	t          j        v rt                      S t                      S )
a  The pip requirement specifier for the running version of Ray.

    Returns:
        A string which can be passed to `pip install` to install the
        currently running Ray version, or None if running on a version
        built from source locally (likely if you are developing Ray).

    Examples:
        Returns "https://s3-us-west-2.amazonaws.com/ray-wheels/[..].whl"
            if running a stable release, a nightly or a specific commit
    RAY_CI_POST_WHEEL_TESTS   z.whlz{{RAY_COMMIT_SHA}}RAY_RUNTIME_ENV_LOCAL_DEV_MODE1zCurrent Ray version could not be detected, most likely because you have manually built Ray from source.  To use runtime_env in this case, set the environment variable RAY_RUNTIME_ENV_LOCAL_DEV_MODE=1.Ndev)r   environgetr   r'   r   r   r    resolveparentsr   
__commit__r>   __version__r   r   r5   s    r#   current_ray_pip_specifierr`   q   s     
z~~/00 ' w||&&((03V=O=Q=Q
 
 	
 
/	/	/:>>:;;sBBNN4   t	#/	!	!#%%%$&&&r"   
conda_dict
py_versionpip_dependenciesc                    |g }|                      d          g | d<   | d         }|                    d|            d|vr|                    d           d}|D ]Y}t          |t                    rB|                     d          r-t          |d         t                    r||d         z   |d<   d} nZ|s|                    d|i           | S )a  Add Ray, Python and (optionally) extra pip dependencies to a conda dict.

    Args:
        conda_dict: A dict representing the JSON-serialized conda
            environment YAML file.  This dict will be modified and returned.
        py_version: A string representing a Python version to inject
            into the conda dependencies, e.g. "3.7.7"
        pip_dependencies (List[str]): A list of pip dependencies that
            will be prepended to the list of pip dependencies in
            the conda dict.  If the conda dict does not already have a "pip"
            field, one will be created.
    Returns:
        The modified dict.  (Note: the input argument conda_dict is modified
        and returned.)
    Ndependencieszpython=pipFT)rZ   append
isinstancedictr1   )ra   rb   rc   r3   found_pip_dictdeps         r#   inject_dependenciesrl      s   ( ~~n%%-%'
>" n%D 	KK&*&&'''DE N  c4   	SWWU^^ 	
3u:t8T8T 	)CJ6CJ!NE /U,-...r"   c                     t          j        | d          }t          j        |                    d                                                    }|S )NT)	sort_keyszutf-8)jsondumpshashlibsha1encode	hexdigest)ra   serialized_conda_spechashs      r#   _get_conda_env_hashrw      sF     JzTBBB<-44W==>>HHJJDKr"   runtime_envc                    |                      d          }|ft          |t                    rd}nPt          |t                    rdt	          |           }n't          dt          |          j         d          d}|S )zDReturn `"conda://<hashed_dependencies>"`, or None if no GC required.condaNzconda://)ra   zAconda field received by RuntimeEnvAgent must be str or dict, not rL   )rZ   rh   rO   ri   rw   	TypeErrortype__name__)rx   rz   uris      r#   get_urir      s    OOG$$EeS!! 
	 CCt$$ 	D0EBBBDDCC<$(KK$8< < <  
 Jr"   
RuntimeEnvc                    t          j        |                                           }|J t          |          }|r|dg}n&|                     d          rt                      }ng }t          |t                      |          }|S )zEReturns the conda spec with the Ray and `python` dependency inserted.Nr_   zray[default]_inject_current_ray)ro   loadsconda_configr`   get_extensionr4   rl   rR   )rx   r5   ra   ray_pipextra_pip_dependenciess        r#   !_get_conda_dict_with_ray_insertedr      s    
 K446677J!!!'v666G $")>!:		"	"#8	9	9 $!N!P!P!#$'))+A J r"   c                       e Zd ZdZdefdZdedefdZdddee         fd	Ze	fd
ede
ej                 defdZe	fd
e
e         dddedej        def
dZe	fdee         dddede
ej                 fdZdS )CondaPluginrz   resources_dirc                     t           j                            |d          | _        t	          | j                   t           j                            | j        d          | _        t                      | _        d S )Nrz   z%ray-conda-installs-and-deletions.lock)r   r   r'   _resources_dirr
   !_installs_and_deletions_file_lockr2   _validated_named_conda_env)selfr   s     r#   __init__zCondaPlugin.__init__   sb     gll='BB 3444 24!H2
 2
. +.%%'''r"   rv   r   c                 L    t           j                            | j        |          S )a  Generate a path from the hash of a conda or pip spec.

        The output path also functions as the name of the conda environment
        when using the `--prefix` option to `conda create` and `conda remove`.

        Example output:
            /tmp/ray/session_2021-11-03_16-33-59_356303_41018/runtime_resources
                /conda/ray-9a7972c3a75f55e976e620484f58410c920db091
        )r   r   r'   r   )r   rv   s     r#   _get_path_from_hashzCondaPlugin._get_path_from_hash  s     w||D/666r"   rx   r   c                 8    |                                 }|r|gS g S )zFReturn the conda URI from the RuntimeEnv if it exists, else return [].)	conda_uri)r   rx   r   s      r#   get_uriszCondaPlugin.get_uris  s(    ))++	 	;	r"   r~   r5   c                    |                     d|            t          |          \  }}|t          j        k    rt	          d| d|           |                     |          }t          |          }t          | j                  5  t          ||          }ddd           n# 1 swxY w Y   |s|
                    d| d           dS |S )	z2Delete URI and return the number of bytes deleted.zGot request to delete URI zICondaPlugin can only delete URIs with protocol conda.  Received protocol z, URI prefixr5   NzError when deleting conda env z. r   )infor   r   CONDA
ValueErrorr   r   r   r   r   r>   )r   r~   r5   protocolrv   conda_env_pathlocal_dir_size
successfuls           r#   
delete_urizCondaPlugin.delete_uri"  sM    	666777"3$x~%%C-5C C=@C C  
 11$771.AAd<== 	P 	P)OOOJ	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	NNNNNNNOOO1s   B&&B*-B*contextc                     K                                    sdS  fd}t                      }|                    d |           d {V S )Nr   c            	         t          
                    d                    t          t                    rxj        v rdS t                      } t          |           }t          fd|D                       st          d d
 d d          j        	                               dS 	
                    d
                                            t                    \  }}                    |          }t          
		          }	                    d

            t!          j                  5  	 t$          j                            j        d          }t-          |d          5 }t/          j        ||           d d d            n# 1 swxY w Y   t3          ||	           t%          j        |           n# t%          j        |           w xY w
                    d          rt9          |	           d d d            n# 1 swxY w Y   	                    d|            t;          |          S )Nrz   r   c              3   H   K   | ]}|d          k    p|d         k    V  dS )r      Nr!   ).0envresults     r#   	<genexpr>z6CondaPlugin.create.<locals>._create.<locals>.<genexpr>S  s:      PPC6SV+?vQ/?PPPPPPr"   zThe given conda environment 'z' from the runtime env z doesn't exist from the output of `conda info --json`. You can only specify an env that already exists. Please make sure to create an env  z"Setting up conda for runtime_env: r_   z"Setting up conda environment with zenvironment.ymlr8   r   r   )rD   r5   z'Finished creating conda environment at )r   rZ   rh   rO   r   r   r   anyr   adddebug	serializer   r   r   r   r   r   r   r   r'   r   rB   yamldumpr   remover   rJ   r   )
conda_infoenvsr   rv   conda_env_namera   conda_yaml_filefiler   r5   rx   r   r~   s           @r#   _createz#CondaPlugin.create.<locals>._createC  s   -koog.F.FGGF&#&&  T<<<1022
%j11 PPPP4PPPPP $G G G0;G G >D	G G G   /33F;;;qLLQ8M8M8O8OQQ   's^^NHd!55d;;N:;vVVVJKKJ[JJKKK$@AA X X
/&(gll+->' 'O os33 4t	*d3334 4 4 4 4 4 4 4 4 4 4 4 4 4 4.'v    Io....BIo....,,-BCC X-PVWWWWX X X X X X X X X X X X X X X KKR.RRSSS+N;;;sN    H5G7FGF	G F	!G6HG!!)HHH)	has_condar	   run_in_executor)r   r~   rx   r   r5   r   loops   ``` `  r#   createzCondaPlugin.create9  s       $$&& 	14	< 4	< 4	< 4	< 4	< 4	< 4	< 4	<l ())))$888888888r"   urisc                 >   |                                 sd S |                                r|                                }n9t          |                                          \  }}|                     |          }d|_        |xj        t          |          z  c_        d S )Nr7   )r   r   r   r   r   py_executablecommand_prefixr   )r   r   rx   r   r5   r   r   rv   s           r#   modify_contextzCondaPlugin.modify_context|  s     $$&& 	F%%'' 	<(7799NN&{'<'<'>'>??NHd!55d;;N ("=n"M"MMr"   N)r}   
__module____qualname__namerO   r   r   r   r   default_loggerr   loggingLoggerintr   r   r   r   r!   r"   r#   r   r      ss       D0c 0 0 0 0$
7 
7 
7 
7 
7 
7L T#Y     <J  ( 8	   8 "0A9 A9c]A9 "A9 #	A9
 A9 
A9 A9 A9 A9P ,:N N3iN "N #	N
 (N N N N N Nr"   r   )N)<rq   ro   r   r   r(   r@   r:   rP   pathlibr   typingr   r   r   r   r   filelockr   r   ray._common.utilsr	   r
   $ray._private.runtime_env.conda_utilsr   r   r   r   r    ray._private.runtime_env.contextr   "ray._private.runtime_env.packagingr   r   ray._private.runtime_env.pluginr   #ray._private.runtime_env.validationr   ray._private.utilsr   r   r   r   	getLoggerr}   r   r   r9   rO   r$   r,   r4   r   rJ   rR   r`   rl   rw   r   r   r   r!   r"   r#   <module>r      s      				       



       , , , , , , , , , , , ,        



                     ? > > > > > B B B B B B B B < < < < < < H H H H H H            #"8,,	D<3 < < < <7 7 7   4B" " 0" " " "J4 4 4
 (6#' #'W^$#'c]#' #' #' #'R -1/ /S#X// tCy)/ 
#s(^	/ / / /dD S     (3-    , (6 W^$ 
#s(^   *SN SN SN SN SN" SN SN SN SN SNr"   