o
    $i89                     @   s  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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Zd dlmZ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  d d	l!m"Z" d d
l#m$Z$ d dl%m&Z&m'Z'm(Z(m)Z) e*e+Z,ej-dkZ.de/fddZ0dd Z1dd Z2e,fdeej3 fddZ4dd Z5e,fdeej3 dee/ fddZ6	d*deeef de/deee/  deeef fddZ7dede/fd d!Z8d"edee/ fd#d$Z9e,fd"d%deej3 dee/ef fd&d'Z:G d(d) d)e"Z;dS )+    N)Path)AnyDictListOptional)FileLock)get_or_create_event_looptry_to_create_directory)create_conda_env_if_neededdelete_conda_envget_conda_activate_commandsget_conda_envsget_conda_info_json)RuntimeEnvContext)Protocol	parse_uri)RuntimeEnvPlugin)parse_and_validate_conda)get_directory_size_bytesget_master_wheel_urlget_release_wheel_urlget_wheel_filenamentreturnc                   C   s   t jt jtjd d S )Nr   )ospathsplitray__file__ r   r   [/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_private/runtime_env/conda.py_resolve_current_ray_path+   s   r!   c                  C   s"   t  } tj| d}t|d S )zFind the Ray setup_spec from the currently running Ray.

    This function works even when Ray is built from source with pip install -e.
    zsetup.py
setup_spec)r!   r   r   joinrunpyrun_path)ray_source_python_pathsetup_py_pathr   r   r    _get_ray_setup_spec5   s   r(   c                  C   s    t  jt  jd  } tt| S )z<Find the Ray dependencies when Ray is installed from source.default)r(   install_requiresextraslistset)depsr   r   r    -_resolve_install_from_source_ray_dependencies?   s   r/   loggerc                 C   s   t r
tj| d}ntj| d}t|ddg  }t }|	d| d| d tj|d}tj
|rF|	d	|  t| ttj|d
d}|| W d   dS 1 saw   Y  dS )z:Write the current Ray site package directory to a new sitepythonz
bin/pythonz-cz9import sysconfig; print(sysconfig.get_paths()['purelib'])z
Injecting z to environment site-packages z( because _inject_current_ray flag is on.r   z)Replacing existing ray installation with zray_shared.pthwN)_WIN32r   r   r#   
subprocesscheck_outputdecodestripr!   warningisdirshutilrmtreeopenwrite)
conda_pathr0   python_binarysite_packages_pathray_pathmaybe_ray_dirfr   r   r    _inject_ray_to_conda_siteH   s.   
"rD   c                   C   s   d tttjd d S )N.   )r#   mapstrsysversion_infor   r   r   r    _current_py_versionm   s   rK   c                 C   sl   t jdrt jttj j	d dt
 S tjdkr+t jddkr)| d dS d	tjv r3t S t S )
a  The pip requirement specifier for the running version of Ray.

    Returns:
        A string which can be passed to `pip install` to install the
        currently running Ray version, or None if running on a version
        built from source locally (likely if you are developing Ray).

    Examples:
        Returns "https://s3-us-west-2.amazonaws.com/ray-wheels/[..].whl"
            if running a stable release, a nightly or a specific commit
    RAY_CI_POST_WHEEL_TESTS   z.whlz{{RAY_COMMIT_SHA}}RAY_RUNTIME_ENV_LOCAL_DEV_MODE1zCurrent Ray version could not be detected, most likely because you have manually built Ray from source.  To use runtime_env in this case, set the environment variable RAY_RUNTIME_ENV_LOCAL_DEV_MODE=1.Ndev)r   environgetr   r#   r   r   r   resolveparentsr   
__commit__r8   __version__r   r   r0   r   r   r    current_ray_pip_specifierq   s   

rX   
conda_dict
py_versionpip_dependenciesc                 C   s   |du rg }|  ddu rg | d< | d }|d|  d|vr&|d d}|D ]}t|trI| drIt|d trI||d  |d< d} nq*|sS|d|i | S )a  Add Ray, Python and (optionally) extra pip dependencies to a conda dict.

    Args:
        conda_dict: A dict representing the JSON-serialized conda
            environment YAML file.  This dict will be modified and returned.
        py_version: A string representing a Python version to inject
            into the conda dependencies, e.g. "3.7.7"
        pip_dependencies (List[str]): A list of pip dependencies that
            will be prepended to the list of pip dependencies in
            the conda dict.  If the conda dict does not already have a "pip"
            field, one will be created.
    Returns:
        The modified dict.  (Note: the input argument conda_dict is modified
        and returned.)
    Ndependencieszpython=pipFT)rR   append
isinstancedictr,   )rY   rZ   r[   r.   found_pip_dictdepr   r   r    inject_dependencies   s$   
"rc   c                 C   s&   t j| dd}t|d }|S )NT)	sort_keyszutf-8)jsondumpshashlibsha1encode	hexdigest)rY   serialized_conda_spechashr   r   r    _get_conda_env_hash   s   rm   runtime_envc                 C   s`   |  d}|dur,t|trd}|S t|tr!dt|d }|S tdt|j dd}|S )zDReturn `"conda://<hashed_dependencies>"`, or None if no GC required.condaNzconda://)rY   zAconda field received by RuntimeEnvAgent must be str or dict, not rE   )rR   r_   rH   r`   rm   	TypeErrortype__name__)rn   ro   urir   r   r    get_uri   s   



rt   
RuntimeEnvc                 C   sZ   t |  }|dusJ t|d}|r|dg}n| dr"t }ng }t|t |}|S )zEReturns the conda spec with the Ray and `python` dependency inserted.NrW   zray[default]_inject_current_ray)re   loadsconda_configrX   get_extensionr/   rc   rK   )rn   r0   rY   ray_pipextra_pip_dependenciesr   r   r    !_get_conda_dict_with_ray_inserted   s   


r|   c                   @   s   e Zd ZdZdefddZdedefddZd	d
dee fddZe	fdede
ej defddZe	fde
e d	d
dedejdef
ddZe	fdee d	d
dede
ej fddZdS )CondaPluginro   resources_dirc                 C   s8   t j|d| _t| j t j| jd| _t | _d S )Nro   z%ray-conda-installs-and-deletions.lock)r   r   r#   _resources_dirr	   !_installs_and_deletions_file_lockr-   _validated_named_conda_env)selfr~   r   r   r    __init__   s   
zCondaPlugin.__init__rl   r   c                 C   s   t j| j|S )a  Generate a path from the hash of a conda or pip spec.

        The output path also functions as the name of the conda environment
        when using the `--prefix` option to `conda create` and `conda remove`.

        Example output:
            /tmp/ray/session_2021-11-03_16-33-59_356303_41018/runtime_resources
                /conda/ray-9a7972c3a75f55e976e620484f58410c920db091
        )r   r   r#   r   )r   rl   r   r   r    _get_path_from_hash  s   
zCondaPlugin._get_path_from_hashrn   ru   c                 C   s   |  }|r	|gS g S )zFReturn the conda URI from the RuntimeEnv if it exists, else return [].)	conda_uri)r   rn   r   r   r   r    get_uris  s   zCondaPlugin.get_urisrs   r0   c                 C   s   | d|  t|\}}|tjkrtd| d| | |}t|}t| j t	||d}W d   n1 s<w   Y  |sN|
d| d dS |S )	z2Delete URI and return the number of bytes deleted.zGot request to delete URI zICondaPlugin can only delete URIs with protocol conda.  Received protocol z, URI prefixr0   NzError when deleting conda env z. r   )infor   r   CONDA
ValueErrorr   r   r   r   r   r8   )r   rs   r0   protocolrl   conda_env_pathlocal_dir_size
successfulr   r   r    
delete_uri"  s&   

zCondaPlugin.delete_uricontextc                    s8     sdS  fdd}t }|d |I d H S )Nr   c               
      s  t d t tr; jv rdS t } t| }t fdd|D s3td  d d  dj	  dS 
d	   t\}}|}td
}d  tjK z1tjjd}t|d}t|| W d    n1 sw   Y  t||d W t| nt| w drt|d W d    n1 sw   Y  d|  t|S )Nro   r   c                 3   s(    | ]} |d  kp |d kV  qdS )r      Nr   ).0envresultr   r    	<genexpr>S  s   & z6CondaPlugin.create.<locals>._create.<locals>.<genexpr>zThe given conda environment 'z' from the runtime env z doesn't exist from the output of `conda info --json`. You can only specify an env that already exists. Please make sure to create an env  z"Setting up conda for runtime_env: rW   z"Setting up conda environment with zenvironment.ymlr2   r   rv   )r>   r0   z'Finished creating conda environment at )r   rR   r_   rH   r   r   r   anyr   adddebug	serializer   r   r|   r   r   r   r   r   r#   r   r<   yamldumpr
   removery   rD   r   )
conda_infoenvsr   rl   conda_env_namerY   conda_yaml_filefiler0   rn   r   rs   r   r    _createC  sR   



z#CondaPlugin.create.<locals>._create)	has_condar   run_in_executor)r   rs   rn   r   r0   r   loopr   r   r    create9  s   6zCondaPlugin.createurisc                 C   sT   |  sd S | r| }nt| \}}| |}d|_| jt|7  _d S )Nr1   )r   r   r   r   r   py_executablecommand_prefixr   )r   r   rn   r   r0   r   r   rl   r   r   r    modify_context|  s   

zCondaPlugin.modify_contextN)rr   
__module____qualname__namerH   r   r   r   r   default_loggerr   loggingLoggerintr   r   r   r   r   r   r   r    r}      sF    

Hr}   )N)<rg   re   r   r   r$   r:   r4   rI   pathlibr   typingr   r   r   r   r   filelockr   r   ray._common.utilsr   r	   $ray._private.runtime_env.conda_utilsr
   r   r   r   r    ray._private.runtime_env.contextr   "ray._private.runtime_env.packagingr   r   ray._private.runtime_env.pluginr   #ray._private.runtime_env.validationr   ray._private.utilsr   r   r   r   	getLoggerrr   r   r   r3   rH   r!   r(   r/   r   rD   rK   rX   rc   rm   rt   r|   r}   r   r   r   r    <module>   sr    





%
)



2

