o
    $iD                  	   @   s  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Zd dlmZ d dlmZ e eZdeddfd	d
ZdefddZdefddZdee dee fddZdee dee fddZdedefddZdedefddZde
eef de
eef fddZd e
eee ef de	e fd!d"Zd#e
eee ef de	e fd$d%Zd&ee dee fd'd(Zd)ee dee fd*d+Z d,eeef de	eeef  fd-d.Z!eee eeee!ed/Z"eed0Z#dS )1    N)OrderedDict)Path)DictListOptionalUnion)is_path
parse_pathpathreturnc                 C   s   t |  dS )z6Parse the path to ensure it is well-formed and exists.Nr	   )r    r   `/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_private/runtime_env/validation.pyvalidate_path   s   r   uric                 C   sr   zddl m}m} || \}}W n ty   t|  dw || v r3|ds5|ds7tdd S d S d S )Nr   )Protocol	parse_uriz is not a valid URI. Passing directories or modules to be dynamically uploaded is only supported at the job level (i.e., passed to `ray.init`).z.zipz.whlz2Only .zip or .whl files supported for remote URIs.)"ray._private.runtime_env.packagingr   r   
ValueErrorremote_protocolsendswith)r   r   r   protocolr   r   r   r   validate_uri   s   r   requirements_filec                 C   s0   t | }| st| d|  dS )zIRead the given [requirements_file], and return all required dependencies.z is not a valid file
)r   is_filer   	read_textstripsplit)r   requirements_pathr   r   r   #_handle_local_deps_requirement_file(   s   r    py_modules_urisc                 C   sR   t | tstdt|  d| D ]}t |ts"tdt| dt| qdS )z^Parses and validates a 'py_modules' option.

    Expects py_modules to be a list of URIs.
    ,`py_modules` must be a list of strings, got ."`py_module` must be a string, got N)
isinstancelist	TypeErrortypestrr   )r!   moduler   r   r   validate_py_modules_uris0   s   


r+   
py_modulesc                 C   sd   t | tstdt|  d| D ]}t |ts"tdt| dt|r+t| qt| q| S )zmParses and validates a 'py_modules' option.

    Expects py_modules to be a list of local paths or URIs.
    r"   r#   r$   )r%   r&   r'   r(   r)   r   r   r   )r,   r*   r   r   r   parse_and_validate_py_modulesB   s   



r-   working_dir_uric                 C   s*   t | tstdt|  dt|  dS )z,Parses and validates a 'working_dir' option.$`working_dir` must be a string, got r#   N)r%   r)   r'   r(   r   )r.   r   r   r   validate_working_dir_uriY   s
   
r0   working_dirc                 C   sJ   | dusJ t | tstdt|  dt| rt|  | S t|  | S )zSParses and validates a 'working_dir' option.

    This can be a URI or a path.
    Nr/   r#   )r%   r)   r'   r(   r   r   r   )r1   r   r   r   parse_and_validate_working_dirc   s   
r2   condac              
   C   s   | dusJ t jdkrtd | }t| trdt| }|jdv rN| s,t	d| dz
t
| }W |S  tyM } zt	d| d| dd}~ww | rb| s^t	d	| dt|}|S t| trm| }|S td
t|  d)a  Parses and validates a user-provided 'conda' option.

    Conda can be one of three cases:
        1) A dictionary describing the env. This is passed through directly.
        2) A string referring to the name of a preinstalled conda env.
        3) A string pointing to a local conda YAML file. This is detected
           by looking for a '.yaml' or '.yml' suffix. In this case, the file
           will be read as YAML and passed through as a dictionary.
    Nwin32runtime environment support is experimental on Windows. If you run into issues please file a report at https://github.com/ray-project/ray/issues.)z.yamlz.ymlzCan't find conda YAML file r#   zFailed to read conda file z: zCan't find conda env directory z6runtime_env['conda'] must be of type str or dict, got )sysplatformloggerwarningr%   r)   r   suffixr   r   yaml	safe_loadr   	Exceptionis_absoluteis_dirdictr'   r(   )r3   result	file_pather   r   r   parse_and_validate_condau   s:   




rD   uvc                 C   s   | dusJ t jdkrtd d}t| tr"t| }t|dd}nt| tr7t	dd | D r7t| dd}nt| trt
|  h d	 rQtd
t|   d| vr\td|  d| v rrt| d tsrtdt| d  d| v rt| d tstdt| d  d| v rt| d tstdt| d  t| d D ]\}}t|tstdt| d| dq|  }| dd|d< | ddg|d< t| d tstdt| d  n	tdt|  tt|d |d< t|d dkrd}td|  d| d |S )uH  Parses and validates a user-provided 'uv' option.

    The value of the input 'uv' field can be one of two cases:
        1) A List[str] describing the requirements. This is passed through.
           Example usage: ["tensorflow", "requests"]
        2) a string containing the path to a local pip “requirements.txt” file.
        3) A python dictionary that has one field:
            a) packages (required, List[str]): a list of uv packages, it same as 1).
            b) uv_check (optional, bool): whether to enable pip check at the end of uv
               install, default to False.
            c) uv_version (optional, str): user provides a specific uv to use; if
               unspecified, default version of uv will be used.
            d) uv_pip_install_options (optional, List[str]): user-provided options for
              `uv pip install` command, default to ["--no-cache"].

    The returned parsed value will be a list of packages. If a Ray library
    (e.g. "ray[serve]") is specified, it will be deleted and replaced by its
    dependencies (e.g. "uvicorn", "requests").
    Nr4   r5    F)packagesuv_checkc                 s       | ]}t |tV  qd S Nr%   r)   .0depr   r   r   	<genexpr>       z(parse_and_validate_uv.<locals>.<genexpr>>   rG   rH   
uv_versionuv_pip_install_optionszrruntime_env['uv'] can only have these fields: packages, uv_check, uv_version and uv_pip_install_options, but got: rG   z9runtime_env['uv'] must include field 'packages', but got rH   z8runtime_env['uv']['uv_check'] must be of type bool, got rQ   z9runtime_env['uv']['uv_version'] must be of type str, got rR   zJruntime_env['uv']['uv_pip_install_options'] must be of type list[str] got  for 	-th item.z
--no-cachez9runtime_env['uv']['packages'] must be of type list, got: z:runtime_env['uv'] must be of type List[str], or dict, got r   z$Rewrote runtime_env `uv` field from  to r#   r6   r7   r8   r9   r%   r)   r    r@   r&   allsetkeysr   boolr'   r(   	enumeratecopygetr   fromkeyslendebug)rE   rA   uv_listidxcur_optr   r   r   parse_and_validate_uv   s   








rd   pipc                 C   s.  | dusJ d}t jdkrtd t| tr"t| }t|dd}nt| tr7t	dd | D r7t| dd}nt| trt
|  h d rQtd	t|   d
| v rgt| d
 tsgtdt| d
  d| v r}t| d ts}tdt| d  d| v rt| d tstdt| d  t| d D ]\}}t|tstdt| d| dq|  }| d
d|d
< d| vrtd|  t| d trt| d |d< nt| d tstdt| d  n	tdt|  tt|d |d< t|d dkr	d}td|  d| d |S )a  Parses and validates a user-provided 'pip' option.

    The value of the input 'pip' field can be one of two cases:
        1) A List[str] describing the requirements. This is passed through.
        2) A string pointing to a local requirements file. In this case, the
           file contents will be read split into a list.
        3) A python dictionary that has three fields:
            a) packages (required, List[str]): a list of pip packages, it same as 1).
            b) pip_check (optional, bool): whether to enable pip check at the end of pip
               install, default to False.
            c) pip_version (optional, str): the version of pip, ray will spell
               the package name 'pip' in front of the `pip_version` to form the final
               requirement string, the syntax of a requirement specifier is defined in
               full in PEP 508.
            d) pip_install_options (optional, List[str]): user-provided options for
              `pip install` command, defaults to ["--disable-pip-version-check", "--no-cache-dir"].

    The returned parsed value will be a list of pip packages. If a Ray library
    (e.g. "ray[serve]") is specified, it will be deleted and replaced by its
    dependencies (e.g. "uvicorn", "requests").
    Nr4   r5   F)rG   	pip_checkc                 s   rI   rJ   rK   rL   r   r   r   rO   (  rP   z)parse_and_validate_pip.<locals>.<genexpr>>   rG   rf   pip_versionpip_install_optionszrruntime_env['pip'] can only have these fields: packages, pip_check, pip_install_options and pip_version, but got: rf   z:runtime_env['pip']['pip_check'] must be of type bool, got rg   z;runtime_env['pip']['pip_version'] must be of type str, got rh   zHruntime_env['pip']['pip_install_options'] must be of type list[str] got rS   rT   rG   z:runtime_env['pip'] must include field 'packages', but got zAruntime_env['pip']['packages'] must be of type str of list, got: z9runtime_env['pip'] must be of type str or List[str], got r   z%Rewrote runtime_env `pip` field from rU   r#   rV   )re   rA   pip_listrb   rc   r   r   r   parse_and_validate_pip  s   








rj   	containerc                 C   s   | dusJ | S )zwParses and validates a user-provided 'container' option.

    This is passed through without validation (for now).
    Nr   )rk   r   r   r   parse_and_validate_containers  s   rl   excludesc                 C   sX   | dusJ t | trt| dkrdS t | tr#tdd | D r#| S tdt|  )zParses and validates a user-provided 'excludes' option.

    This is validated to verify that it is of type List[str].

    If an empty list is passed, we return `None` for consistency.
    Nr   c                 s   rI   rJ   rK   )rM   r   r   r   r   rO     rP   z.parse_and_validate_excludes.<locals>.<genexpr>z7runtime_env['excludes'] must be of type List[str], got )r%   r&   r_   rW   r'   r(   )rm   r   r   r   parse_and_validate_excludes|  s   rn   env_varsc                 C   s   | dusJ t | dkrdS t| tstdt|  |  D ]&\}}t|ts5td| dt| t|tsFtd| dt| q | S )a(  Parses and validates a user-provided 'env_vars' option.

    This is validated to verify that all keys and vals are strings.

    If an empty dictionary is passed, we return `None` for consistency.

    Args:
        env_vars: A dictionary of environment variables to set in the
            runtime environment.

    Returns:
        The validated env_vars dictionary, or None if it was empty.

    Raises:
        TypeError: If the env_vars is not a dictionary of strings. The error message
            will include the type of the invalid value.
    Nr   z<runtime_env['env_vars'] must be of type Dict[str, str], got zDruntime_env['env_vars'] must be of type Dict[str, str], but the key z is of type zFruntime_env['env_vars'] must be of type Dict[str, str], but the value )r_   r%   r@   r'   r(   itemsr)   )ro   keyvalr   r   r   parse_and_validate_env_vars  s8   


rs   )r,   r1   rm   r3   re   rE   ro   rk   )r1   r,   )$loggingr6   collectionsr   pathlibr   typingr   r   r   r   r;   ray._private.path_utilsr   r   r
   	getLogger__name__r8   r)   r   r   r    r+   r-   r0   r2   r@   rD   rd   rj   rl   rn   rs   OPTION_TO_VALIDATION_FNOPTION_TO_NO_PATH_VALIDATION_FNr   r   r   r   <module>   sD    

"$+$bq	&.
