o
    bi$                     @   s   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eZeG d	d
 d
eZG dd dZG dd dZefdddedededej f
d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                
   @   s   e Zd ZU dZdZeed< eZe	ed< e
deddfddZd	d
dee fddZdee dedejdefddZdee d	d
dedejddf
ddZdedejdefddZdS )RuntimeEnvPluginz4Abstract base class for runtime environment plugins.Nnamepriorityruntime_env_dictreturnc                 C      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   r   r   S/home/ubuntu/.local/lib/python3.10/site-packages/ray/_private/runtime_env/plugin.pyvalidate   s   zRuntimeEnvPlugin.validateruntime_env
RuntimeEnvc                 C   s   g S Nr   )selfr   r   r   r   get_uris,   s   zRuntimeEnvPlugin.get_urisuricontextloggerc                    s   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"   r   r   r   create/   s   zRuntimeEnvPlugin.createurisc                 C   r   )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"   r   r   r   modify_contextH   s   zRuntimeEnvPlugin.modify_contextc                 C   r   )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"   r   r   r   
delete_uri\   s   
zRuntimeEnvPlugin.delete_uri)__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      s8   
 

r   c                   @   s&   e Zd ZdedededefddZdS )PluginSetupContextr   class_instancer   	uri_cachec                 C   s   || _ || _|| _|| _d S r   )r   r4   r   r5   )r   r   r4   r   r5   r   r   r   __init__j   s   
zPluginSetupContext.__init__N)r'   r(   r)   r+   r   r-   r   r6   r   r   r   r   r3   i   s    r3   c                   @   s   e Zd ZdZdd Zdee ddfddZd	eddfd
dZ	de
e ddfddZdeddfddZdedefddZde
e fddZdS )RuntimeEnvPluginManagerz:This manager is used to load plugins in runtime env agent.c                 C   s2   i | _ tjt}|rt|}| | d S d S r   )pluginsosenvirongetr   jsonloadsload_plugins)r   plugin_config_strplugin_configsr   r   r   r6   z   s   
z RuntimeEnvPluginManager.__init__plugin_classr   Nc                 C   s^   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   r8   )r   rA   r   r   r   validate_plugin_class   s   


z-RuntimeEnvPluginManager.validate_plugin_classr   c                 C   s:   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 rB   )
isinstancer-   r   r   rD   )r   r   r   r   r   validate_priority   s   z)RuntimeEnvPluginManager.validate_priorityr@   c                 C   s   |D ]D}t |trt|vrtd| dt dt|t }| | t|v r,|t }n|j}| | | }t	|j
||| || j|j
< q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)rF   r/   r	   rD   r   rE   r   r   rG   r3   r   create_uri_cache_for_pluginr8   )r   r@   plugin_configrA   r   r4   r   r   r   r>      s.   


z$RuntimeEnvPluginManager.load_pluginspluginc                 C   sB   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)typerE   rG   r   r3   r   rH   r8   )r   rJ   rA   r   r   r   
add_plugin   s   
z"RuntimeEnvPluginManager.add_pluginc                 C   s8   d|j  d }tdttj|d }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   upperr-   r2   r9   r:   r;   r   r&   )r   rJ   cache_size_env_varcache_size_bytesr   r   r   rH      s
   
z3RuntimeEnvPluginManager.create_uri_cache_for_pluginc                 C   s   t | j dd dS )zGet the sorted plugin setup contexts, sorted by increasing priority.

        Returns:
            The sorted plugin setup contexts.
        c                 S   s   | j S r   )r   )xr   r   r   <lambda>   s    zFRuntimeEnvPluginManager.sorted_plugin_setup_contexts.<locals>.<lambda>)key)sortedr8   values)r   r   r   r   sorted_plugin_setup_contexts   s   z4RuntimeEnvPluginManager.sorted_plugin_setup_contexts)r'   r(   r)   r*   r6   r   r   rE   r   rG   r   r   r>   rL   r   rH   r3   rX   r   r   r   r   r7   w   s     r7   r   r   rJ   r5   r!   r"   c                    s   |j | vs| |j  du rdS ||  || }|s1|d|j  d |jd| ||dI dH  |D ]8}||vrW|d| d |j|| ||dI dH }|j|||d q3|d|j  d| d	 |j||d q3||| || 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 rB   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   rJ   r5   r!   r"   r$   r    
size_bytesr   r   r   create_for_plugin_if_needed   s(   

r_   )"r<   r0   r9   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   r3   r7   r1   r_   r   r   r   r   <module>   s6     
Qt