o
    `۷i                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZmZ dZedd	 Zd ddZded
dfddZded
dfddZdeded
dfddZded
efddZd!ded
dfddZeG dd dZedd ZdS )"    N)contextmanager)	dataclass)Path)DictOptional)AuthenticationTokenLoaderConfig)RAY_AUTH_MODERAY_AUTH_TOKENRAY_AUTH_TOKEN_PATHz.ray
auth_tokenreturnc                   C   s   t    td dS )z4Reset authentication token and AUTH_MODE ray config. N)r   instancereset_cacher   
initialize r   r   \/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/_private/authentication_test_utils.pyreset_auth_token_state   s   r   modec                 C   s   | t jd< dS )z1Set the authentication mode environment variable.r	   N)osenviron)r   r   r   r   set_auth_mode   s   r   tokenc                 C   s   | t jd< t jdd dS )z<Configure the authentication token via environment variable.r
   r   N)r   r   pop)r   r   r   r   set_env_auth_token   s   
r   pathc                 C   sJ   t |}| dur|jjddd ||  t|tjd< tjdd dS )zMWrite the authentication token to a specific path and point the loader to it.NTparentsexist_okr   r
   )r   parentmkdir
write_textstrr   r   r   )r   r   
token_pathr   r   r   set_auth_token_path#   s   
r%   c                 C   s*   t  t }|jjddd ||  |S )zIWrite the authentication token to the default ~/.ray/auth_token location.Tr   )r   home!_DEFAULT_AUTH_TOKEN_RELATIVE_PATHr    r!   r"   )r   default_pathr   r   r   set_default_auth_token.   s   
r)   Fremove_defaultc                 C   s<   dD ]	}t j|d q| rt t }|jdd dS dS )zSClear authentication-related environment variables and optional default token file.)r
   r   NT
missing_ok)r   r   r   r   r&   r'   unlink)r*   varr(   r   r   r   clear_auth_token_sources7   s   r/   c                   @   s   e Zd ZU eeee f ed< ee ed< eed< ee ed< eed< eed< ee ed< e	dd	d
Z
dddZdddZdS )AuthenticationEnvSnapshotoriginal_envoriginal_homehome_was_set	temp_homedefault_token_pathdefault_token_existsdefault_token_contentsr   c           
   	   C   s   dd t D }dtjv }tjd}d}|s:tjd}|r"t|ntt }|d }|jddd t|tjd< t	 t
 }| }|rJ| nd}	| |||||||	d	S )
z9Capture current authentication-related environment state.c                 S   s   i | ]	}|t j|qS r   )r   r   get).0r.   r   r   r   
<dictcomp>P   s    z5AuthenticationEnvSnapshot.capture.<locals>.<dictcomp>HOMENTEST_TMPDIRray_test_homeTr   )r1   r2   r3   r4   r5   r6   r7   )_AUTH_ENV_VARSr   r   r8   r   tempfile
gettempdirr!   r#   r&   r'   exists	read_text)
clsr1   r3   r2   r4   test_tmpdirbase_dirr5   r6   r7   r   r   r   captureL   s.   
z!AuthenticationEnvSnapshot.captureNc                 C   s   | j jdd dS )z3Remove the default token file for the current HOME.Tr+   N)r5   r-   )selfr   r   r   clear_default_tokenm   s   z-AuthenticationEnvSnapshot.clear_default_tokenc                 C   s8  t jd}|durt|}| r|jdd | j D ]\}}|du r-t j|d q|t j|< q| j	rI| j
du rCt jdd n| j
t jd< | jr_| jjjddd | j| jp\d n| jjdd | j	st jd}| jdur|t| jkrt jdd | jdur| j rtj| jdd dS dS dS dS )	zERestore the captured environment, HOME, and default token file state.r   NTr+   r;   r   r   )ignore_errors)r   r   r8   r   rA   r-   r1   itemsr   r3   r2   r6   r5   r    r!   r"   r7   r4   r#   shutilrmtree)rG   custom_token_pathr.   valuecurrent_homer   r   r   restorer   s2   
z!AuthenticationEnvSnapshot.restore)r   r0   r   N)__name__
__module____qualname__r   r#   r   __annotations__boolr   classmethodrF   rH   rP   r   r   r   r   r0   B   s   
 
 r0   c                  c   s*    t  } z
| V  W |   dS |   w )zGContext manager that restores authentication environment state on exit.N)r0   rF   rP   )snapshotr   r   r   authentication_env_guard   s
   rY   rQ   )F)r   rK   r?   
contextlibr   dataclassesr   pathlibr   typingr   r   ray._rayletr   r   r>   r'   r   r#   r   r   r%   r)   rV   r/   r0   rY   r   r   r   r   <module>   s(    
	R