
    &`i/#                        d dl Z d dlZd dlmZ d dlmZ d dlmZmZ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mZmZmZmZmZmZmZ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&  e j'        e(          Z)de*de+fdZ, ej-                    e)dfdee*ef         de+de
e*         de
e j.                 dee*ef         f
dZ/ G d de          Z0dS )    N)Path)
ModuleType)AnyDictListOptional)try_to_create_directory)RuntimeEnvContext)Protocoldelete_packagedownload_and_unpack_packageget_local_dir_from_uriget_uri_for_directoryget_uri_for_fileget_uri_for_packageinstall_wheel_package
is_whl_uripackage_exists	parse_uriupload_package_if_neededupload_package_to_gcs)RuntimeEnvPlugin)set_pythonpath_in_context)get_directory_size_bytes)	GcsClient)RuntimeEnvSetupErrorsreturnc                     	 t          |           \  }}n# t          $ r d\  }}Y nw xY w|t          j                    v r9|                    d          s$|                    d          st          d          |d uS )N)NNz.zip.whlz2Only .zip or .whl files supported for remote URIs.)r   
ValueErrorr   remote_protocolsendswith)r   protocolpaths      w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/py_modules.py_check_is_urir'   !   s    $"1$$ $ $ $#$$$$ 	H-////f%% 	0f%% 	0 MNNN4s    ''runtime_envinclude_gitignorescratch_dirloggerc           
         |                      d          }|| S t          |t                    s t          dt	          |           d          g }|D ]}t          |t
                    r|}nt          |t                    rt          |          }n~t          |t                    rIt          |d          s|j	        }nQt          |j                  dk    rt          d          |j        \  }n t          dt	          |           d          t          |          r|}	nt          |                                          st          |          j        d	k    rt          |                                          }
|                      d
d          }|
rt!          |||          }	nt#          |          }	|g	 t%          |	||||
||           n# t&          $ rA}ddlm}  |            rt-          d| d|           |t-          d| d|           |d}~ww xY w |||           nt          |          j        dk    rt/          t          |                    }	|it1          |	          sY	 t3          |	t          |                                                     nJ# t&          $ r}t-          d| d|           |d}~ww xY wn! ||dd           nt          d|           |                    |	           || d<   | S )zUploads the entries in py_modules and replaces them with a list of URIs.

    For each entry that is already a URI, this is a no-op.
    
py_modulesNzIpy_modules must be a List of local paths, imported modules, or URIs, got .__path__   zZpy_modules only supports modules whose __path__ has length 1 or those who are single-file.zHpy_modules must be a list of file paths, URIs, or imported modules, got z.pyexcludes)r)   r1   )r)   include_parent_dirr1   r+   r   )is_in_databricks_runtimezFailed to upload module z to the Ray cluster, please ensure there are only files under the module path, notebooks under the path are not allowed, original exception: z to the Ray cluster: )r1   r    zFailed to upload T)r1   is_filezFpy_modules entry must be a .py file, a directory, or a .whl file; got )get
isinstancelist	TypeErrortypestrr   r   hasattr__file__lenr/   r!   r'   is_dirsuffixr   r   r   	Exceptionray.util.spark.utilsr3   r   r   r   r   
read_bytesappend)r(   r)   r*   r+   	upload_fnr-   py_modules_urismodulemodule_path
module_urir>   r1   er3   s                 r&   upload_py_modules_if_neededrJ   1   sE    ..Jj$'' 
-j))- - -
 
 	

 O [+ [+fc"" 	 KK%% 	f++KK
++ 	6:.. 0$o v''!++$F   !'<,0LL< < <  
 %% >	$JJ K  '')) :T+->->-E-N-Nk**1133&??:t<< ?!6#*;!)" " "JJ "2+!>!>J$!0&''.?/5%-#)     % ! ! !QQQQQQ3355 %"6!H; !H !H EF!H !H# #
 $%% 3,{ , ,(), ,   !!! IkH=====k"")V330k1B1BCC
$)*55 	%%1 *D,=,=,H,H,J,J     ) % % %"6!0K !0 !0,-!0 !0# # $%%%	% IkD$GGGGG )&) )   	z****
 !0Ks0   ;G
H<HH5/J%%
K
/KK
c                       e Zd ZdZdedefdZdefdZefdede	e
j                 defd	Zdee         fd
Zefdedddede	e
j                 def
dZefdee         dedede	e
j                 fdZdS )PyModulesPluginr-   resources_dir
gcs_clientc                     t           j                            |d          | _        || _        t          | j                   d S )Npy_modules_files)osr%   join_resources_dir_gcs_clientr	   )selfrM   rN   s      r&   __init__zPyModulesPlugin.__init__   s:     gll=:LMM% 344444    uric                 ,    t          || j                  S N)r   rS   )rU   rX   s     r&   _get_local_dir_from_uriz'PyModulesPlugin._get_local_dir_from_uri   s    %c4+>???rW   r+   r   c                     |                     d|           t          || j                  }t          |          }t	          || j                  }|s|                    d| d           dS |S )z2Delete URI and return the number of bytes deleted.z%Got request to delete pymodule URI %sz!Tried to delete nonexistent URI: r.   r   )infor   rS   r   r   warning)rU   rX   r+   	local_dirlocal_dir_sizedeleteds         r&   
delete_urizPyModulesPlugin.delete_uri   s{     	;SAAA*30CDD	1)<< d&9:: 	NNEsEEEFFF1rW   c                 *    |                                 S rZ   )r-   )rU   r(   s     r&   get_uriszPyModulesPlugin.get_uris   s    %%'''rW   r(   
RuntimeEnvcontextc                    K   t          || j        | j        |           d {V }t          |          r/|}|                     |          }t          |||           d {V  t          |          S )N)r+   )	wheel_uri
target_dirr+   )r   rS   rT   r   r[   r   r   )rU   rX   r(   rf   r+   
module_dirrh   s          r&   createzPyModulesPlugin.create   s       7$d&6v
 
 
 
 
 
 
 
 

 c?? 	"I55c::J'#
6          (
333rW   urisruntime_env_dictc                 0   g }|D ]c}|                      |          }|                                st          d| d| d          |                    t	          |                     dt          t          j                            |          |           d S )NzLocal directory z	 for URI z~ does not exist on the cluster. Something may have gone wrong while downloading, unpacking or installing the py_modules files.)	r[   existsr!   rC   r:   r   rQ   pathseprR   )rU   rl   rm   rf   r+   module_dirsrX   rj   s           r&   modify_contextzPyModulesPlugin.modify_context   s      	0 	0C55c::J$$&&  Qz Q QC Q Q Q  
 s:////!"*//+">">HHHHHrW   N)__name__
__module____qualname__namer:   r   rV   r[   default_loggerr   loggingLoggerintrb   r   rd   r
   rk   r   rr    rW   r&   rL   rL      sg       D5c 5y 5 5 5 5
@3 @ @ @ @ <J  ( 8	   (tCy ( ( ( ( ,:4 44 "4 #	4
 (4 
4 4 4 44 ,:I I3iI I #	I
 (I I I I I IrW   rL   )1rx   rQ   pathlibr   typesr   typingr   r   r   r   ray._common.utilsr	    ray._private.runtime_env.contextr
   "ray._private.runtime_env.packagingr   r   r   r   r   r   r   r   r   r   r   r   r   ray._private.runtime_env.pluginr   $ray._private.runtime_env.working_dirr   ray._private.utilsr   ray._rayletr   ray.exceptionsr   	getLoggerrs   rw   r:   boolr'   getcwdry   rJ   rL   r{   rW   r&   <module>r      sr    				             , , , , , , , , , , , , 5 5 5 5 5 5 > > > > > >                              = < < < < < J J J J J J 7 7 7 7 7 7 ! ! ! ! ! ! / / / / / /""8,, S  T        & "+'5w wc3hww #w W^$	w 
#s(^w w w wtDI DI DI DI DI& DI DI DI DI DIrW   