o
    biM;                     @   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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e0fddZ1dd Z2dd Z3e-fdeej4 fddZ5dd Z6dd Z7e-fdeej4 dee0 fddZ8	d,deeef de0deee0  deeef fd d!Z9dede0fd"d#Z:d$edee0 fd%d&Z;e-fd$d'deej4 dee0e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   R/home/ubuntu/.local/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_spec6   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_siteI   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_versionn   s   rK   c                   C   s   t jdko
t dkS )Ndarwinarm64)rI   platformmachiner   r   r   r    
_is_m1_macr   s   rP   c                 C   s   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 r:t r7td
t S t rCdtj 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devz-Nightly wheels are not available for M1 Macs.zray==)r   environgetr   r#   r   r   r   resolveparentsr   
__commit__r8   __version__rP   
ValueErrorr   r   r0   r   r   r    current_ray_pip_specifierv   s"   

r^   
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)rW   append
isinstancedictr,   )r_   r`   ra   r.   found_pip_dictdepr   r   r    inject_dependencies   s$   
"ri   c                 C   s&   t j| dd}t|d }|S )NT)	sort_keyszutf-8)jsondumpshashlibsha1encode	hexdigest)r_   serialized_conda_spechashr   r   r    _get_conda_env_hash   s   rs   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://)r_   zAconda field received by RuntimeEnvAgent must be str or dict, not rE   )rW   re   rH   rf   rs   	TypeErrortype__name__)rt   ru   urir   r   r    get_uri   s   



rz   
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.Nr]   zray[default]_inject_current_ray)rk   loadsconda_configr^   get_extensionr/   ri   rK   )rt   r0   r_   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 )CondaPluginru   resources_dirc                 C   s8   t j|d| _t| j t j| jd| _t | _d S )Nru   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__rr   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   rr   r   r   r    _get_path_from_hash  s   
zCondaPlugin._get_path_from_hashrt   r{   c                 C   s   |  }|r	|gS g S )zFReturn the conda URI from the RuntimeEnv if it exists, else return [].)	conda_uri)r   rt   r   r   r   r    get_uris(  s   zCondaPlugin.get_urisry   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   CONDAr\   r   r   r   r   r   r8   )r   ry   r0   protocolrr   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 )Nru   r   c                 3   s(    | ]} |d  kp |d kV  qdS )r      Nr   ).0envresultr   r    	<genexpr>`  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: r]   z"Setting up conda environment with zenvironment.ymlr2   r   r|   )r>   r0   z'Finished creating conda environment at )r   rW   re   rH   r   r   r   anyr\   adddebug	serializer   r   r   r   r   r   r   r   r#   r   r<   yamldumpr
   remover   rD   r   )
conda_infoenvsr   rr   conda_env_namer_   conda_yaml_filefiler0   rt   r   ry   r   r    _createP  sR   



z#CondaPlugin.create.<locals>._create)	has_condar   run_in_executor)r   ry   rt   r   r0   r   loopr   r   r    createF  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   rt   r   r0   r   r   rr   r   r   r    modify_context  s   

zCondaPlugin.modify_contextN)rx   
__module____qualname__namerH   r   r   r   r   default_loggerr   loggingLoggerintr   r   r   r   r   r   r   r    r     sF    

Hr   )N)>rm   rk   r   r   rN   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   	getLoggerrx   r   r   r3   rH   r!   r(   r/   r   rD   rK   rP   r^   ri   rs   rz   r   r   r   r   r   r    <module>   sv    





%
1



2

