
    &`i.                     4   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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mZ d dlmZ d dlmZ d d	lmZ  ej        e          Zd
e
defdZde
dee         fdZ  G d d          Z! G d de          Z"dS )    N)create_taskget_running_loop)DictListOptional)try_to_create_directory)dependency_utilsvirtualenv_utils)Protocol	parse_uri)RuntimeEnvPlugin)check_output_cmd)get_directory_size_bytespip_dictreturnc                     t          j        | d          }t          j        |                    d                                                    }|S )NT)	sort_keyszutf-8)jsondumpshashlibsha1encode	hexdigest)r   serialized_pip_spechash_vals      p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/pip.py_get_pip_hashr      sF    *X>>>|/66w??@@JJLLHO    runtime_envc                 B   |                      d          }|t          |t                    rdt          |          z   }n^t          |t                    r"dt          t          |                    z   }n't          dt          |          j         d          d}|S )zBReturn `"pip://<hashed_dependencies>"`, or None if no GC required.pipNzpip://)r   )packagesz@pip field received by RuntimeEnvAgent must be list or dict, not .)get
isinstancedictr   list	TypeErrortype__name__)r   r!   uris      r   get_urir,      s    
//%
 
 C
c4   	]C8888CCT"" 	]D#4F4F4FGGGGCC;%)#YY%7; ; ;  
 Jr   c                       e Zd Zefdedddeej                 fdZe	dedee         ded	e
dej        f
d
            Zdededed	e
dej        f
dZdedee         ded	e
dej        f
dZd Zd ZdS )PipProcessor
target_dirr   
RuntimeEnvloggerc                    	 dd l }n+# t          $ r t          dt          j         d          w xY w|                    d|           || _        || _        || _        | j        	                                | _
        t          j                                        | _        | j                            | j                                                   d S )Nr   zPlease install virtualenv `z5 -m pip install virtualenv`to enable pip runtime env.z"Setting up pip for runtime_env: %s)
virtualenvImportErrorRuntimeErrorsys
executabledebug_target_dir_runtime_env_logger
pip_config_pip_configosenvironcopy_pip_envupdateenv_vars)selfr/   r   r1   r3   s        r   __init__zPipProcessor.__init__.   s    	 	 	 	.N. . .  	 	9;GGG%',7799
))T.7799:::::s    (/pathpip_versioncwdpip_envc                    K   |sdS t          j        |          }|ddddd| g}|                    d|           t          ||||           d{V  dS )z>Run the pip command to reinstall pip to the specified version.N-mr!   install--disable-pip-version-checkzInstalling pip with version %sr1   rH   env)r
   get_virtualenv_pythoninfor   )clsrF   rG   rH   rI   r1   pythonpip_reinstall_cmds           r   _ensure_pip_versionz PipProcessor._ensure_pip_versionE   s        	F!7== )+
 	4kBBB0SgVVVVVVVVVVVVr   	pip_checkc                    K   |s|                     d           dS t          j        |          }t          |ddddg|||           d{V  |                     d|           dS )	zRun the pip check command to check python dependency conflicts.
        If exists conflicts, the exit code of pip check command will be non-zero.
        zSkip pip check.NrK   r!   checkrM   rN   zPip check on %s successfully.)rQ   r
   rP   r   )rD   rF   rV   rH   rI   r1   rS   s          r   
_pip_checkzPipProcessor._pip_check`   s        	KK)***F!7==T5'+HI	
 
 
 	
 	
 	
 	
 	
 	
 	
 	3T:::::r   pip_packagesc                   K   t          j        |          }t          j        |          }t          j        ||          }t                      }	|	                    d t          j        ||           d {V  |dddd|g}
| j        	                    dddg          }|

                    |           |                    d|           t          |
|||	           d {V  d S )
NrK   r!   rL   z-rpip_install_optionsrM   z--no-cache-dirz$Installing python requirements to %srN   )r
   get_virtualenv_pathrP   r	   get_requirements_filer   run_in_executorgen_requirements_txtr=   r$   extendrQ   r   )rD   rF   rZ   rH   rI   r1   virtualenv_pathrS   pip_requirements_filelooppip_install_cmdpip_opt_lists               r   _install_pip_packagesz"PipProcessor._install_pip_packagesy   s-      +>tDD!7== 0 F,!
 !

  !!""1!	
 
 	
 	
 	
 	
 	
 	
 	
( !
 '++!$ACS#T
 
 	|,,,:OLLLv3GTTTTTTTTTTTTr   c                 l  K   | j         }| j        }| j        d         }t          j                            |d          }t          j        |d           	 t          j        |||           d {V  t          j	        |          }t          j        |||          4 d {V  |                     || j                            dd           || j        |           d {V  |                     |||| j        |           d {V  |                     || j                            dd          || j        |           d {V  d d d           d {V  d S # 1 d {V swxY w Y   d S # t"          $ rC |                    d|           t'          j        |d	           |                    d
            w xY w)Nr"   exec_cwdT)exist_okrG   rV   Fz Delete incomplete virtualenv: %s)ignore_errorszFailed to install pip packages.)r9   r;   r=   r>   rF   joinmakedirsr
   create_or_get_virtualenvrP   r	   	check_rayrU   r$   rA   rg   rY   	ExceptionrQ   shutilrmtree	exception)rD   rF   r1   rZ   ri   rS   s         r   _runzPipProcessor._run   s     '
3 7<<j11
Ht,,,, 	";D(FSSSSSSSSS%;DAAF'1&(FKK        ..$((==M         00 M         oo$((e<<M        %                             2  	 	 	KK:DAAAM$d3333>???		s3   AE& !BE E& 
EE&  E!E& &AF3c                 N    |                                                                  S N)rt   	__await__)rD   s    r   rw   zPipProcessor.__await__   s    yy{{$$&&&r   N)r*   
__module____qualname__default_loggerstrr   loggingLoggerrE   classmethodr   rU   boolrY   r   rg   rt   rw    r   r   r.   r.   -   sm       
 ,:	; ;; "; (	; ; ; ;. WW c]W 	W
 W W W W [W4;; ; 	;
 ; ; ; ; ;24U4U 3i4U 	4U
 4U 4U 4U 4U 4Ul) ) )V' ' ' ' 'r   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ddddde
ej                 def
dZe	fdee         dddddej        fdZdS )	PipPluginr!   resources_dirc                     t           j                            |d          | _        i | _        i | _        i | _        t          | j                   d S )Nr!   )r>   rF   rl   _pip_resources_dir_creating_task_create_locks_created_hash_bytesr   )rD   r   s     r   rE   zPipPlugin.__init__   sK    "$',,}e"D"D  7935  788888r   r   r   c                 L    t           j                            | j        |          S )zGenerate a path from the hash of a pip spec.

        Example output:
            /tmp/ray/session_2021-11-03_16-33-59_356303_41018/runtime_resources
                /pip/ray-9a7972c3a75f55e976e620484f58410c920db091
        )r>   rF   rl   r   )rD   r   s     r   _get_path_from_hashzPipPlugin._get_path_from_hash   s     w||D3X>>>r   r   r0   c                 8    |                                 }|r|gS g S )zDReturn the pip URI from the RuntimeEnv if it exists, else return [].)pip_uri)rD   r   r   s      r   get_uriszPipPlugin.get_uris   s(    %%'' 	9	r   r+   r1   c           	         |                     d|           t          |          \  }}|t          j        k    rt	          d| d|           | j                            |d          }||                                 | j        |= | 	                    |          }t          |          }| j        |= 	 t          j        |           n@# t          $ r3}|                    d| dt!          |                      Y d}~dS d}~ww xY w|S )z2Delete URI and return the number of bytes deleted.z Got request to delete pip URI %szDPipPlugin can only delete URIs with protocol pip. Received protocol z, URI NzError when deleting pip env z: r   )rQ   r   r   PIP
ValueErrorr   popcancelr   r   r   r   rq   rr   OSErrorwarningr{   )	rD   r+   r1   protocolr   taskpip_env_pathlocal_dir_sizees	            r   
delete_urizPipPlugin.delete_uri   sD    	6<<<&s^^(x|##@*2@ @:=@ @   "&&x66KKMMM$X.//991,??s#	M,'''' 	 	 	NNR,RR#a&&RRSSS11111	 s   4C	 	
D(DDcontextRuntimeEnvContextc                 >   	
K                                    sdS t          |          \  }	                     	          

fd}| j        vrt	          j                     j        |<    j        |         4 d {V  	 j        v r j        	         cd d d           d {V  S t           |                      x j        	<   }|	                    	 fd           | d {V }| j        	<   |cd d d           d {V  S # 1 d {V swxY w Y   d S )Nr   c                     K   t                     d {V  t                      } |                     d t                     d {V S rv   )r.   r   r_   r   )rd   r1   r   r/   s    r   _create_for_hashz*PipPlugin.create.<locals>._create_for_hash&  s               $%%D--.
        r   c                 :    j                             d           S rv   )r   r   )_r   rD   s    r   <lambda>z"PipPlugin.create.<locals>.<lambda>:  s    T-@-D-DXt-T-T r   )
has_pipr   r   r   asyncioLockr   r   r   add_done_callback)rD   r+   r   r   r1   r   r   r   pip_dir_bytesr   r/   s   ` ` `    @@r   createzPipPlugin.create  s"      ""$$ 	1&s^^(--h77

	 
	 
	 
	 
	 
	 
	 d(((&-lnnDs#%c* 	! 	! 	! 	! 	! 	! 	! 	!4333/9	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 4??O?O?Q?Q3R3RRD)D""#T#T#T#T#TUUU"&JJJJJJM1>D$X. 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s   D,AD
DDurisc                 t   |                                 sd S |d         }t          |          \  }}|                     |          }t          j        |          }	t
          j                            |	          st          d| d| d          |	|_	        |xj
        t          j        |          z  c_
        d S )Nr   zLocal directory z	 for URI zn does not exist on the cluster. Something may have gone wrong while installing the runtime_env `pip` packages.)r   r   r   r
   rP   r>   rF   existsr   py_executablecommand_prefixget_virtualenv_activate_command)
rD   r   r   r   r1   r+   r   r   r/   virtualenv_pythons
             r   modify_contextzPipPlugin.modify_context?  s     ""$$ 	F1g&s^^(--h77
,B:NNw~~/00 	=: = = = = =  
 !2"2"R#
 #
 	
r   N)r*   rx   ry   namer{   rE   r   r   r   rz   r   r|   r}   intr   r   r   r   r   r   r   r      s[       D9c 9 9 9 9?C ?C ? ? ? ?L T#Y     <J  ( 8	   F ,:$! $!$! "$! %	$!
 ($! 
$! $! $! $!V "0
 
3i
 "
 %	

 
 
 
 
 
 
r   r   )#r   r   r   r|   r>   rq   r6   r   r   typingr   r   r   ray._common.utilsr   ray._private.runtime_envr	   r
   "ray._private.runtime_env.packagingr   r   ray._private.runtime_env.pluginr   ray._private.runtime_env.utilsr   ray._private.utilsr   	getLoggerr*   rz   r{   r   r,   r.   r   r   r   r   <module>r      s       				  



 1 1 1 1 1 1 1 1 ' ' ' ' ' ' ' ' ' ' 5 5 5 5 5 5 G G G G G G G G B B B B B B B B < < < < < < ; ; ; ; ; ; 7 7 7 7 7 7""8,,D S     (3-    $n' n' n' n' n' n' n' n'bz
 z
 z
 z
 z
  z
 z
 z
 z
 z
r   