
    &`i$                     @   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	m
Z
 d dlmZ d dl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  ej        e          Ze G d	 d
e                      Z G d d          Z G d d          Zefdddedededej         f
dZ!dS )    N)ABC)AnyDictListOptionalType)import_attr) RAY_RUNTIME_ENV_CLASS_FIELD_NAME'RAY_RUNTIME_ENV_PLUGIN_DEFAULT_PRIORITY#RAY_RUNTIME_ENV_PLUGIN_MAX_PRIORITY#RAY_RUNTIME_ENV_PLUGIN_MIN_PRIORITYRAY_RUNTIME_ENV_PLUGINS_ENV_VAR#RAY_RUNTIME_ENV_PRIORITY_FIELD_NAME)RuntimeEnvContext)URICache)DeveloperAPIc            
           e Zd ZU dZdZeed<   eZe	ed<   e
deddfd            Zdd	dee         fd
Zdee         dedej        defdZdee         dd	dedej        ddf
dZdedej        defdZdS )RuntimeEnvPluginz4Abstract base class for runtime environment plugins.Nnamepriorityruntime_env_dictreturnc                     dS )a	  Validate user entry for this plugin.

        The method is invoked upon installation of runtime env.

        Args:
            runtime_env_dict: The user-supplied runtime environment dict.

        Raises:
            ValueError: If the validation fails.
        N )r   s    s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/runtime_env/plugin.pyvalidatezRuntimeEnvPlugin.validate   s	     	    runtime_env
RuntimeEnvc                     g S Nr   )selfr   s     r   get_uriszRuntimeEnvPlugin.get_uris,   s    	r   uricontextloggerc                 
   K   dS )a{  Create and install the runtime environment.

        Gets called in the runtime env agent at install time. The URI can be
        used as a caching mechanism.

        Args:
            uri: A URI uniquely describing this resource.
            runtime_env: The RuntimeEnv object.
            context: Auxiliary information supplied by Ray.
            logger: A logger to log messages during the context modification.

        Returns:
            float: The disk space taken up by this plugin installation for this
                environment. e.g. for working_dir, this downloads the files to the
                local node.
        r   r   )r"   r$   r   r%   r&   s        r   createzRuntimeEnvPlugin.create/   s      . qr   urisc                     dS )a  Modify context to change worker startup behavior.

        For example, you can use this to prepend "cd <dir>" command to worker
        startup, or add new environment variables.

        Args:
            uris: The URIs used by this resource.
            runtime_env: The RuntimeEnv object.
            context: Auxiliary information supplied by Ray.
            logger: A logger to log messages during the context modification.
        Nr   )r"   r)   r   r%   r&   s        r   modify_contextzRuntimeEnvPlugin.modify_contextH   s	    $ 	r   c                     dS )a  Delete the runtime environment given uri.

        Args:
            uri: A URI uniquely describing this resource.
            logger: The logger used to log messages during the deletion.

        Returns:
            float: The amount of space reclaimed by the deletion.
        r   r   )r"   r$   r&   s      r   
delete_urizRuntimeEnvPlugin.delete_uri\   s	     qr   )__name__
__module____qualname____doc__r   str__annotations__r   r   intstaticmethoddictr   r   r#   r   r   loggingLoggerfloatr(   r+   r-   r   r   r   r   r      sC        >>D#;Hc;;;4 D    \L T#Y    c] #	
  
   23i " #	
  
   (
c 
7> 
e 
 
 
 
 
 
r   r   c                   &    e Zd ZdedededefdZdS )PluginSetupContextr   class_instancer   	uri_cachec                 >    || _         || _        || _        || _        d S r!   )r   r<   r   r=   )r"   r   r<   r   r=   s        r   __init__zPluginSetupContext.__init__j   s%     	, "r   N)r.   r/   r0   r2   r   r4   r   r?   r   r   r   r;   r;   i   sN        
#
# )
# 	
#
 
# 
# 
# 
# 
# 
#r   r;   c                       e Zd ZdZd Zdee         ddfdZdeddfdZ	d	e
e         ddfd
ZdeddfdZdedefdZde
e         fdZdS )RuntimeEnvPluginManagerz:This manager is used to load plugins in runtime env agent.c                     i | _         t          j                            t                    }|r+t          j        |          }|                     |           d S d S r!   )pluginsosenvirongetr   jsonloadsload_plugins)r"   plugin_config_strplugin_configss      r   r?   z RuntimeEnvPluginManager.__init__z   sZ    68JNN+JKK 	.!Z(9::Nn-----	. 	.r   plugin_classr   Nc                     t          |t                    st          d| d          |j        st          d| d          |j        | j        v r&t          d| d| j        |j                  d          d S )Nz!Invalid runtime env plugin class zQ. The plugin class must inherit ray._private.runtime_env.plugin.RuntimeEnvPlugin.z$No valid name in runtime env plugin .zThe name of runtime env plugin z conflicts with )
issubclassr   RuntimeErrorr   rC   )r"   rL   s     r   validate_plugin_classz-RuntimeEnvPluginManager.validate_plugin_class   s    ,(899 	DL D D D  
   	WUlUUUVVV,,;, ; ;\%67; ; ;   -,r   r   c           	          t          |t                    r|t          k     s|t          k    r#t	          d| dt           dt           d          d S )NzInvalid runtime env priority z", it should be an integer between z and rN   )
isinstancer4   r   r   rP   )r"   r   s     r   validate_priorityz)RuntimeEnvPluginManager.validate_priority   sx    8S))
	======> > >6> > ;> > >   >=r   rK   c           	         |D ]}t          |t                    r	t          |vrt          d| dt           d          t	          |t                             }|                     |           t          |v r|t                   }n|j        }|                     |            |            }t          |j
        |||                     |                    | j        |j
        <   dS )z8Load runtime env plugins and create URI caches for them.z"Invalid runtime env plugin config z+, it should be a object which contains the z field.N)rS   r6   r
   rP   r	   rQ   r   r   rT   r;   r   create_uri_cache_for_pluginrC   )r"   rK   plugin_configrL   r   r<   s         r   rI   z$RuntimeEnvPluginManager.load_plugins   s   + 	 	M}d333=HH"A A A7A A A  
 '}5U'VWWL&&|444
 3mCC()LM'0""8,,,)\^^N.@!00@@	/ /DL*++/	 	r   pluginc                     t          |          }|                     |           |                     |j                   t	          |j        ||j        |                     |                    | j        |j        <   dS )zAdd a plugin to the manager and create a URI cache for it.

        Args:
            plugin: The class instance of the plugin.
        N)typerQ   rT   r   r;   r   rV   rC   )r"   rX   rL   s      r   
add_pluginz"RuntimeEnvPluginManager.add_plugin   sy     F||""<000|4555*<!,,V44	+
 +
\&'''r   c           	          d|j          d                                }t          dt          t          j                            |d                    z            }t          |j        |          S )zCreate a URI cache for a plugin.

        Args:
            plugin_name: The name of the plugin.

        Returns:
            The created URI cache for the plugin.
        RAY_RUNTIME_ENV__CACHE_SIZE_GBi   @
   )	r   upperr4   r9   rD   rE   rF   r   r-   )r"   rX   cache_size_env_varcache_size_bytess       r   rV   z3RuntimeEnvPluginManager.create_uri_cache_for_plugin   sk     LKKKQQSSbjnn-?DDEEE
 
 )+;<<<r   c                 T    t          | j                                        d           S )zGet the sorted plugin setup contexts, sorted by increasing priority.

        Returns:
            The sorted plugin setup contexts.
        c                     | j         S r!   )r   )xs    r   <lambda>zFRuntimeEnvPluginManager.sorted_plugin_setup_contexts.<locals>.<lambda>   s    1: r   )key)sortedrC   values)r"   s    r   sorted_plugin_setup_contextsz4RuntimeEnvPluginManager.sorted_plugin_setup_contexts   s)     dl))++1E1EFFFFr   )r.   r/   r0   r1   r?   r   r   rQ   r   rT   r   r   rI   r[   r   rV   r;   rj   r   r   r   rA   rA   w   s       DD. . .$7G2H T    # $    4: $    @
!1 
d 
 
 
 
 =2B =x = = = = Gd3E.F G G G G G Gr   rA   r   r   rX   r=   r%   r&   c                 `  K   |j         | vs| |j                  dS |                    |            |                    |           }|s=|                    d|j          d           |                    d| ||           d{V  |D ]}||vrQ|                    d| d           |                    || ||           d{V }|                    |||           W|                    d|j          d| d	           |                    ||           |                    || ||           dS )
zISet up the environment using the plugin if not already set up and cached.NzNo URIs for runtime env plugin z+; create always without checking the cache.)r&   zCache miss for URI rN   zRuntime env  zq is already installed and will be reused. Search all runtime_env_setup-*.log to find the corresponding setup log.)	r   r   r#   debugr(   addinfo	mark_usedr+   )r   rX   r=   r%   r&   r)   r$   
size_bytess           r   create_for_plugin_if_neededrr      s      {+%%V[)A)I
OOK   ??;''D G8fk 8 8 8	
 	
 	
 mmD+wvmFFFFFFFFF 4 4iLL5s555666%}}S+wv}VVVVVVVVJMM#z&M9999KKSv{ S SS S S S  
 F3333
$Wf=====r   )"rG   r7   rD   abcr   typingr   r   r   r   r   ray._common.utilsr	   "ray._private.runtime_env.constantsr
   r   r   r   r   r    ray._private.runtime_env.contextr   "ray._private.runtime_env.uri_cacher   ray.util.annotationsr   	getLoggerr.   default_loggerr   r;   rA   r8   rr   r   r   r   <module>r|      s      				       2 2 2 2 2 2 2 2 2 2 2 2 2 2 ) ) ) ) ) )                ? > > > > > 7 7 7 7 7 7 - - - - - -""8,, N N N N Ns N N Nb# # # # # # # #lG lG lG lG lG lG lG lGh ,#> #>#>#> #> 	#>
 N#> #> #> #> #> #>r   