o
    `۷i*                     @   st   d dl Z d dlZd dlmZ dZdZdZG dd dZe dd	d
 e	 D Z
edd Zee
_dd Zee
_dS )    N)urlparseRAY_RUNTIME_ENV_HTTP_USER_AGENTRAY_RUNTIME_ENV_BEARER_TOKENzray-runtime-env-curl/1.0c                   @   s   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
edefddZedd ZedededefddZdS )ProtocolsProviderz~Note that these must be preinstalled on all nodes in the Ray cluster; it is not sufficient to install them in the runtime_env.c                 C      h dS )N>
   gss3uvgcspipfileabfssazurecondahttps clsr   r   W/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/_private/runtime_env/protocol.pyget_protocols      zProtocolsProvider.get_protocolsc                 C   r   )N>   r   r   r   r   r   r   r   r   r   r   r   get_remote_protocols*   r   z&ProtocolsProvider.get_remote_protocolsc                 C   s   zddl }ddlm} W n ty   td| j w | }| r)|d}nddlm	} ddl
m} |jd||dd	}d
|i}||fS )zSet up S3 protocol handling.

        Returns:
            tuple: (open_file function, transport_params)

        Raises:
            ImportError: If required dependencies are not installed.
        r   NopenzBYou must `pip install smart_open[s3]` to fetch URIs in s3 bucket. r   )UNSIGNED)Config)signature_version)configclient)boto3
smart_openr   ImportError_MISSING_DEPENDENCIES_WARNINGSessionget_credentialsr   botocorer   botocore.configr   )r   r   	open_filesession	s3_clientr   r   transport_paramsr   r   r   _handle_s3_protocol.   s$   
z%ProtocolsProvider._handle_s3_protocolc                 C   s@   zddl m} ddlm} W |dfS  ty   td| j w )zSet up Google Cloud Storage protocol handling.

        Returns:
            tuple: (open_file function, transport_params)

        Raises:
            ImportError: If required dependencies are not installed.
        r   )storager   zTYou must `pip install smart_open[gcs]` to fetch URIs in Google Cloud Storage bucket.N)google.cloudr,   r    r   r!   r"   )r   r,   r'   r   r   r   _handle_gs_protocolQ   s   
z%ProtocolsProvider._handle_gs_protocolc                 C   s   zddl m} ddlm} ddlm} W n ty"   td| j w t	d}|s.t
dd| d	}d
||| di}||fS )a  Set up Azure blob storage protocol handling.

        Returns:
            tuple: (open_file function, transport_params)

        Raises:
            ImportError: If required dependencies are not installed.
            ValueError: If required environment variables are not set.
        r   DefaultAzureCredential)BlobServiceClientr   zpYou must `pip install azure-storage-blob azure-identity smart_open[azure]` to fetch URIs in Azure Blob Storage. AZURE_STORAGE_ACCOUNTz`Azure Blob Storage authentication requires AZURE_STORAGE_ACCOUNT environment variable to be set.zhttps://z.blob.core.windows.net/r   )account_url
credential)azure.identityr0   azure.storage.blobr1   r    r   r!   r"   osgetenv
ValueError)r   r0   r1   r'   azure_storage_account_namer3   r*   r   r   r   _handle_azure_protocolg   s,   
z(ProtocolsProvider._handle_azure_protocolc                    sR   zddl ddlm  W n ty   td| j w dd fdd
}|dfS )a&  Set up Azure Blob File System Secure (ABFSS) protocol handling.

        Returns:
            tuple: (open_file function, transport_params)

        Raises:
            ImportError: If required dependencies are not installed.
            ValueError: If the ABFSS URI format is invalid.
        r   Nr/   z\You must `pip install adlfs azure-identity` to fetch URIs in Azure Blob File System Secure. r*   c                   s   t | }|jrd|jvrtd|  |jdd\}}|s%td|  |r,|ds3td|  |dd }|sCtd	|  j|  d
}|| |S )N@z6Invalid ABFSS URI format - missing container@account:    z1Invalid ABFSS URI format - empty container name: z.dfs.core.windows.netzSInvalid ABFSS URI format - invalid hostname (must end with .dfs.core.windows.net): .r   z/Invalid ABFSS URI format - empty account name: )account_namer4   )r   netlocr9   splitendswithAzureBlobFileSystemr   )urimoder*   parsedcontainer_parthostname_partr:   
filesystemr0   adlfsr   r   r'      s0   z;ProtocolsProvider._handle_abfss_protocol.<locals>.open_file)rL   r5   r0   r!   r"   r   r'   r   rK   r   _handle_abfss_protocol   s   &z(ProtocolsProvider._handle_abfss_protocolreturnc                 C   s6   t jttdd}t jt}|rd| |d< |S )Nz*/*)z
User-AgentAcceptzBearer Authorization)r7   environget'RAY_RUNTIME_ENV_HTTP_USER_AGENT_ENV_VAR_DEFAULT_HTTP_USER_AGENT$RAY_RUNTIME_ENV_BEARER_TOKEN_ENV_VAR)r   headersbearer_tokenr   r   r   _http_headers   s   zProtocolsProvider._http_headersc                    sJ   zddl m W n ty   td j w dd fdd
}|dfS )z6Set up HTTPS protocol handling with curl-like headers.r   r   z7You must `pip install smart_open` to fetch HTTPS URIs. Nr<   c                   s*      dd}|r|| | ||dS )N<   )rW   timeoutr<   )rY   update)rE   rF   r*   paramsr   smart_open_openr   r   r'      s   
z;ProtocolsProvider._handle_https_protocol.<locals>.open_file)r    r   r!   r"   rM   r   r^   r   _handle_https_protocol   s   	z(ProtocolsProvider._handle_https_protocolprotocol
source_uri	dest_filec              	   C   sd  ||   v sJ d}d}|dkr |tdd }dddd}nT|dkr+|  \}}nI|dkr6|  \}}n>|d	krA|  \}}n3|d
krL|  \}}n(|dkrW|  \}}nzddlm} W n t	ys   t	d|
  d| j w ||d|d-}t|d}||  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )ax  Download file from remote URI to destination file.

        Args:
            protocol: The protocol to use for downloading (e.g., 's3', 'https').
            source_uri: The source URI to download from.
            dest_file: The destination file path to save to.

        Raises:
            ImportError: If required dependencies for the protocol are not installed.
        Nr   zfile://r<   c                S   s
   t | |S Nr   )rE   rF   r*   r   r   r   r'     s   
z8ProtocolsProvider.download_remote_uri.<locals>.open_filer   r   r   r   r   r   r   z+You must `pip install smart_open` to fetch z URIs. rbwb)r   lenr`   r+   r.   r;   rN   r    r   r!   upperr"   writeread)r   ra   rb   rc   tpr'   finfoutr   r   r   download_remote_uri   sD   "z%ProtocolsProvider.download_remote_uriN)__name__
__module____qualname__r"   classmethodr   r   r+   r.   r;   rN   dictrY   r`   strrn   r   r   r   r   r   
   s*    


"

'
<
r   Protocolc                 C   s   i | ]}|  |qS r   rh   .0ra   r   r   r   
<dictcomp>$  s    ry   c                    s    fddt  D S )Nc                    s   g | ]} |   qS r   rv   rw   r   r   r   
<listcomp>,  s    z%_remote_protocols.<locals>.<listcomp>)r   r   r   r   r   r   _remote_protocols(  s   
r{   c                 C   s   t | j||S rd   )r   rn   value)selfrb   rc   r   r   r   _download_remote_uri4  s   r~   )enumr7   urllib.parser   rT   rV   rU   r   Enumr   ru   rr   r{   remote_protocolsr~   rn   r   r   r   r   <module>   s$      

