
    &`i6%                        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mZm	Z	m
Z
 	 dZej        dk    Zdedee         fdZdedefd	Zd
edefdZ	 ddededeej                 ddfdZddedeej                 defdZdefdZdefdZdedee	eef                  fdZ G d de          Z	 ddee         dedeej                 de
ee	eeef         f         fdZ	 d dee         dej        dede	eef         fdZdS )!    N)ListOptionalTupleUnionRAY_CONDA_HOMEntconda_env_namereturnc                 :   t           s^dt          j        v st          t          j        v r=t	          d          }dt          j                            |           ddg}|dd| gz  }n0t	          d          }t           sd|| g}nt	          d          }|d| g}|ddgz   S )	zQ
    Get a list of commands to run to silently activate the given conda env.
    	CONDA_EXEconda.z/../etc/profile.d/conda.shz&&activatesourcez1>&2)_WIN32osenvironr   get_conda_bin_executablepathdirname)r	   
conda_pathactivate_conda_envactivate_paths       x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/conda_utils.pyget_conda_activate_commandsr      s    
  J{bj00Nbj4P4P-g66
wz**FFF

 	w
NCC 1<< 	J"*M>!J1'::J",j.!I..    executable_namec                    t           j                            t                    }|rt          rt           j                            |d| z            }t           j                            |          r|S t           j                            |d| z            }t           j                            |          r|S n%t           j                            |d| z            S d}dt           j        v rt           j                            t           j        d                   }t          rt           j                            |d| z            }t           j                            |          r|S t           j                            |d| z            }t           j                            |          r|S n t           j                            ||           S t          r| dz   S | S )a  
    Return path to the specified executable, assumed to be discoverable within
    a conda installation.

    The conda home directory (expected to contain a 'bin' subdirectory on
    linux) is configurable via the ``RAY_CONDA_HOME`` environment variable. If
    ``RAY_CONDA_HOME`` is unspecified, try the ``CONDA_EXE`` environment
    variable set by activating conda. If neither is specified, this method
    returns `executable_name`.
    z%s.exez%s.batzbin/%sr   r   z.bat)	r   r   getr   r   r   joinexistsr   )r   
conda_home	candidateconda_bin_dirs       r   r   r   +   s    //J  	HZO1KLLIw~~i(( !  ZO1KLLIw~~i(( !  ! 7<<
H,FGGG
bj  
;(?@@ 	@ZO1KLLIw~~i(( !  ZO1KLLIw~~i(( !  ! 7<<??? (''r   conda_env_pathc                     t          |                                           }dt          j        |                    d                                                    z  S )Nzray-%szutf-8)openreadhashlibsha1encode	hexdigest)r%   conda_env_contentss     r   _get_conda_env_namer.   T   sL    n--2244gl#5#<#<W#E#EFFPPRRRRr   conda_yaml_fileprefixloggerc                    |t          j        t                    }t          d          }	 t	          |dgd           n0# t
          t          f$ r t          d| dt           d          w xY wt	          |d	d
dg          \  }}}t          j
        ||                    d          d                   d         }||v r|                    d| d           dS |d	dd| d|g}|                    d|            t          ||          \  }}	|dk    rHt          j                            |          rt#          j        |           t'          d| d|	           dS )a  
    Given a conda YAML, creates a conda environment containing the required
    dependencies if such a conda environment doesn't already exist.
    Args:
        conda_yaml_file: The path to a conda `environment.yml` file.
        prefix: Directory to install the environment into via
            the `--prefix` option to conda create.  This also becomes the name
            of the conda env; i.e. it can be passed into `conda activate` and
            `conda remove`
    Nr   --helpFthrow_on_errorz$Could not find Conda executable at 'z'. Ensure Conda is installed as per the instructions at https://conda.io/projects/conda/en/latest/user-guide/install/index.html. You can also configure Ray to look for a specific Conda executable by setting the z: environment variable to the path of the Conda executable.envlist--json{envszConda environment z already exists.createz--filez--prefixzCreating conda environment r   z$Failed to install conda environment z
:
Output:
)logging	getLogger__name__r   exec_cmdEnvironmentErrorFileNotFoundError
ValueErrorr   jsonloadsindexinfoexec_cmd_stream_to_loggerr   r   r!   shutilrmtreeRuntimeError)
r/   r0   r1   r   _stdoutr:   
create_cmd	exit_codeoutputs
             r   create_conda_env_if_neededrP   Y   s    ~"8,,)'22J
*h'>>>>>/0 	
 	
 	
H: H H
 0>H H H
 
 	
	
 ZABBLAvq:fV\\#..001226:D~~AAAABBB 	J KK6f667771*fEEIvA~~7>>&!! 	"M&!!!O6OOvOO
 
 	
 ~s   A   -A-c                    |t          j        t                    }|                    d|             t	          d          }|dd| ddg}t          ||          \  }}|dk    r|                    d|  d	|            d
S dS )NzDeleting conda environment r   removez-pz--allz-yr   z#Failed to delete conda environment z:
FT)r<   r=   r>   rF   r   rG   debug)r0   r1   r   
delete_cmdrN   rO   s         r   delete_conda_envrU      s    ~"8,,
KK6f66777)'22JhfgtDJ1*fEEIvA~~N6NNfNNOOOu4r   c                      t          d          } 	 t          | dgd           n!# t          $ r t          d|  d          w xY wt          | ddd	g          \  }}}t	          j        |          d
         }|S )z+
    Get conda env list in full paths.
    r   r3   Fr4   #Could not find Conda executable at r   r6   r7   r8   r:   r   r?   r@   rB   rC   rD   )r   rK   rL   r:   s       r   get_conda_env_listrY      s     *'22JN*h'>>>>> N N NLzLLLMMMNZABBLAvq:ff%DK	   % Ac                      t          d          } 	 t          | dgd           n!# t          $ r t          d|  d          w xY wt          | ddg          \  }}}t	          j        |          S )	aO  
    Get `conda info --json` output.

    Returns dict of conda info. See [1] for more details. We mostly care about these
    keys:

    - `conda_prefix`: str The path to the conda installation.
    - `envs`: List[str] absolute paths to conda environments.

    [1] https://github.com/conda/conda/blob/main/conda/cli/main_info.py
    r   r3   Fr4   rW   r   rF   r8   rX   )r   rK   rL   s      r   get_conda_info_jsonr\      s     *'22JN*h'>>>>> N N NLzLLLMMMNZ:;;LAvq:frZ   
conda_infoc                     | d         }g }| d         D ]T}||k    r|                     d|f            |                     t          j                            |          |f           U|S )zH
    Gets the conda environments, as a list of (name, path) tuples.
    conda_prefixr:   base)appendr   r   basename)r]   r0   retr6   s       r   get_conda_envsrd      sx     'F
C&! 5 5&==JJ}%%%%JJ((--s34444Jr   c                       e Zd ZdS )ShellCommandExceptionN)r>   
__module____qualname__ r   r   rf   rf      s        Dr   rf   Tcmdr5   c                    t          j        | t           j        t           j        t           j        d          }|                                \  }}|                                }|r|dk    rt          d|d|d|          |||fS )a  
    Runs a command as a child process.

    A convenience wrapper for running a command from a Python script.

    Note on the return value: A tuple of the exit code,
    standard output and standard error is returned.

    Args:
        cmd: the command to run, as a list of strings
        throw_on_error: if true, raises an Exception if the exit code of the
            program is nonzero
    T)rL   stdinstderruniversal_newlinesr   zNon-zero exit code: z


STDOUT:
z	

STDERR:)
subprocessPopenPIPEcommunicatewaitrf   )rj   r5   r1   childrL   rm   rN   s          r   r?   r?      s      o  E ((**VV

I 
)q..##yy&&&&&*
 
 	
 ff$$r   2   n_linesc                    d|v r)t           r"dd |j        D             vrt          d          t          j        | fdt          j        t          j        d|}g }|j        5  t          |j        j	        d          D ]}|
                                }| nq|                                }|s1|                    |                                           || d         }|                    |                                           ddd           n# 1 swxY w Y   |                                }|d	                    |          fS )
zRuns a command as a child process, streaming output to the logger.

    The last n_lines lines of output are also returned (stdout and stderr).
    r6   PATHc                 6    g | ]}|                                 S ri   )upper).0xs     r   
<listcomp>z-exec_cmd_stream_to_logger.<locals>.<listcomp>   s     4T4T4T1QWWYY4T4T4Tr   z*On windows, Popen requires 'PATH' in 'env'T)rn   rL   rm   r   N
)r   keysrB   ro   rp   rq   STDOUTrL   iterreadlinepollstripra   rF   rs   r    )rj   r1   rv   kwargsrt   last_n_lineslinerN   s           r   rG   rG      s    6f4T4T4T4T4T&T&TEFFF 	 
  E L	 
& 
&.44 		& 		&D

I$::<<D 

---'		2LKK

%%%%
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 

Idii----s   !B&DDD)N)TN)ru   ) r)   rC   r<   r   rH   ro   typingr   r   r   r   r   namer   strr   r   r.   LoggerrP   boolrU   r7   rY   dictr\   rd   	Exceptionrf   intr?   rG   ri   r   r   <module>r      s      				      / / / / / / / / / / / / J "	D/ /S	 / / / /2&c &c & & & &RS S S S S S KO6
 6
6
"%6
/7/G6
	6
 6
 6
 6
r S (7>*B d    "D    T    *t U38_(=    	 	 	 	 	I 	 	 	
 UY% %	c%$(%9A'.9Q%
3c3m$$%% % % %D <>. .	c.#N.58.
38_. . . . . .r   