o
    $i.                     @   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 mZmZ d dl	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ eeZd
e
defddZde
dee fddZ G dd dZ!G dd deZ"dS )    N)create_taskget_running_loop)DictListOptional)try_to_create_directory)dependency_utilsvirtualenv_utils)Protocol	parse_uri)RuntimeEnvPlugin)check_output_cmd)get_directory_size_bytespip_dictreturnc                 C   s&   t j| dd}t|d }|S )NT)	sort_keyszutf-8)jsondumpshashlibsha1encode	hexdigest)r   serialized_pip_spechash_val r   Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_private/runtime_env/pip.py_get_pip_hash   s   r   runtime_envc                 C   sn   |  d}|dur3t|trdt|d }|S t|tr(dtt|dd }|S tdt|j dd}|S )zBReturn `"pip://<hashed_dependencies>"`, or None if no GC required.pipNzpip://)r   )packagesz@pip field received by RuntimeEnvAgent must be list or dict, not .)get
isinstancedictr   list	TypeErrortype__name__)r   r   urir   r   r   get_uri   s   



r)   c                   @   s   e Zd Zefdedddeej fddZe	dedee d	ed
e
dejf
ddZdeded	ed
e
dejf
ddZdedee d	ed
e
dejf
ddZdd Zdd ZdS )PipProcessor
target_dirr   
RuntimeEnvloggerc                 C   sz   zdd l }W n ty   tdtj dw |d| || _|| _|| _| j	 | _
tj | _| j| j  d S )Nr   zPlease install virtualenv `z5 -m pip install virtualenv`to enable pip runtime env.z"Setting up pip for runtime_env: %s)
virtualenvImportErrorRuntimeErrorsys
executabledebug_target_dir_runtime_env_logger
pip_config_pip_configosenvironcopy_pip_envupdateenv_vars)selfr+   r   r-   r.   r   r   r   __init__.   s    zPipProcessor.__init__pathpip_versioncwdpip_envc                    sP   |sdS t |}|ddddd| g}|d| t||||dI dH  dS )z>Run the pip command to reinstall pip to the specified version.N-mr   install--disable-pip-version-checkzInstalling pip with version %sr-   rC   env)r	   get_virtualenv_pythoninfor   )clsrA   rB   rC   rD   r-   pythonpip_reinstall_cmdr   r   r   _ensure_pip_versionE   s   

z PipProcessor._ensure_pip_version	pip_checkc                    sN   |s
| d dS t|}t|ddddg|||dI dH  | d| dS )	zRun the pip check command to check python dependency conflicts.
        If exists conflicts, the exit code of pip check command will be non-zero.
        zSkip pip check.NrE   r   checkrG   rH   zPip check on %s successfully.)rK   r	   rJ   r   )r?   rA   rP   rC   rD   r-   rM   r   r   r   
_pip_check`   s   

zPipProcessor._pip_checkpip_packagesc                    s   t |}t |}t||}t }	|	d tj||I d H  |dddd|g}
| j	dddg}|

| |d| t|
|||d	I d H  d S )
NrE   r   rF   z-rpip_install_optionsrG   z--no-cache-dirz$Installing python requirements to %srH   )r	   get_virtualenv_pathrJ   r   get_requirements_filer   run_in_executorgen_requirements_txtr8   r!   extendrK   r   )r?   rA   rS   rC   rD   r-   virtualenv_pathrM   pip_requirements_filelooppip_install_cmdpip_opt_listr   r   r   _install_pip_packagesy   s4   


	
z"PipProcessor._install_pip_packagesc              	      s6  | j }| j}| jd }tj|d}tj|dd zft|||I d H  t	|}t
|||4 I d H > | || jdd || j|I d H  | |||| j|I d H  | || jdd|| j|I d H  W d   I d H  W d S 1 I d H syw   Y  W d S  ty   |d| tj|dd	 |d
  w )Nr   exec_cwdT)exist_okrB   rP   Fz Delete incomplete virtualenv: %s)ignore_errorszFailed to install pip packages.)r4   r6   r8   r9   rA   joinmakedirsr	   create_or_get_virtualenvrJ   r   	check_rayrO   r!   r<   r_   rR   	ExceptionrK   shutilrmtree	exception)r?   rA   r-   rS   r`   rM   r   r   r   _run   sL   



2
zPipProcessor._runc                 C   s   |    S N)rk   	__await__)r?   r   r   r   rm      s   zPipProcessor.__await__N)r'   
__module____qualname__default_loggerstrr   loggingLoggerr@   classmethodr   rO   boolrR   r   r_   rk   rm   r   r   r   r   r*   -   sZ    


6+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d	d
ddde
ej def
ddZe	fdee d	d
dddejfddZdS )	PipPluginr   resources_dirc                 C   s0   t j|d| _i | _i | _i | _t| j d S )Nr   )r9   rA   rc   _pip_resources_dir_creating_task_create_locks_created_hash_bytesr   )r?   rw   r   r   r   r@      s
   zPipPlugin.__init__r   r   c                 C   s   t j| j|S )zGenerate a path from the hash of a pip spec.

        Example output:
            /tmp/ray/session_2021-11-03_16-33-59_356303_41018/runtime_resources
                /pip/ray-9a7972c3a75f55e976e620484f58410c920db091
        )r9   rA   rc   rx   )r?   r   r   r   r   _get_path_from_hash   s   zPipPlugin._get_path_from_hashr   r,   c                 C   s   |  }|r	|gS g S )zDReturn the pip URI from the RuntimeEnv if it exists, else return [].)pip_uri)r?   r   r}   r   r   r   get_uris   s   zPipPlugin.get_urisr(   r-   c           	   
   C   s   | d| t|\}}|tjkrtd| d| | j|d}|dur*|  | j|= | 	|}t
|}| j|= zt| W |S  tyc } z|d| dt|  W Y d}~dS d}~ww )z2Delete URI and return the number of bytes deleted.z Got request to delete pip URI %szDPipPlugin can only delete URIs with protocol pip. Received protocol z, URI NzError when deleting pip env z: r   )rK   r   r
   PIP
ValueErrorry   popcancelr{   r|   r   rz   rh   ri   OSErrorwarningrq   )	r?   r(   r-   protocolr   taskpip_env_pathlocal_dir_sizeer   r   r   
delete_uri   s2   

zPipPlugin.delete_uricontextRuntimeEnvContextc           	   	      s     sdS t|\}  fdd}|jvr&t j|< j| 4 I d H ?  jv rDj  W  d   I d H  S t|  j < }|	 fdd |I d H }|j < |W  d   I d H  S 1 I d H stw   Y  d S )Nr   c                     s.   t  I d H  t } | d tI d H S rl   )r*   r   rW   r   )r\   )r-   r   r+   r   r   _create_for_hash&  s   

z*PipPlugin.create.<locals>._create_for_hashc                    s   j  d S rl   )ry   r   )_)r   r?   r   r   <lambda>:  s    z"PipPlugin.create.<locals>.<lambda>)
has_pipr   r|   rz   asyncioLockr{   r   ry   add_done_callback)	r?   r(   r   r   r-   r   r   r   pip_dir_bytesr   )r   r-   r   r?   r+   r   create  s$   




0zPipPlugin.createurisc           
      C   st   |  sd S |d }t|\}}| |}t|}	tj|	s+td| d| d|	|_	| j
t|7  _
d S )Nr   zLocal directory z	 for URI zn does not exist on the cluster. Something may have gone wrong while installing the runtime_env `pip` packages.)r   r   r|   r	   rJ   r9   rA   existsr   py_executablecommand_prefixget_virtualenv_activate_command)
r?   r   r   r   r-   r(   r   r   r+   virtualenv_pythonr   r   r   modify_context?  s   


zPipPlugin.modify_contextN)r'   rn   ro   namerq   r@   r|   r   r~   rp   r   rr   rs   intr   r   r   r   r   r   r   rv      sF    
	
#
+rv   )#r   r   r   rr   r9   rh   r1   r   r   typingr   r   r   ray._common.utilsr   ray._private.runtime_envr   r	   "ray._private.runtime_env.packagingr
   r   ray._private.runtime_env.pluginr   ray._private.runtime_env.utilsr   ray._private.utilsr   	getLoggerr'   rp   rq   r   r)   r*   rv   r   r   r   r   <module>   s*    
 2