o
    xi(                     @   sp   d Z ddl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 d	d
lmZ G dd deZdS )z)Implementation of AzureEnvironment class.    )HttpResponseError)DefaultAzureCredential)
BlobClientBlobServiceClient)Self   )LaunchError)AZURE_BLOB_REGEX   )AbstractEnvironmentc                   @   s   e Zd ZdZ		dddZeddededefd	d
Z	ede
fddZdededdfddZdededdfddZdeddfddZdddZededeeeef fddZdS )AzureEnvironmentz;AzureEnvironment is a helper for accessing Azure resources.returnNc                 C   s   dS )zInitialize an AzureEnvironment.N selfr   r   b/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/environment/azure_environment.py__init__   s    zAzureEnvironment.__init__Tconfigverifyc                 C   s   |  S )z.Create an AzureEnvironment from a config dict.r   )clsr   r   r   r   r   from_config   s   zAzureEnvironment.from_configc              
   C   s2   zt  W S  ty } ztd| |d}~ww )zGet Azure credentials.z`Could not get Azure credentials. Please make sure you have configured your Azure CLI correctly.
N)r   	Exceptionr   )r   er   r   r   get_credentials   s   z AzureEnvironment.get_credentialssourcedestinationc              
      s   |  |\}}}d| d| }|  }z-td| d|||d}t|d}	|j|	dd W d	   W d	S 1 s;w   Y  W d	S  tyY }
 zt| d
|
j |
d	}
~
w tys }
 zt| d
|
j	j
 d
|
 |
d	}
~
ww )ap  Upload a file to Azure blob storage.

        Arguments:
            source (str): The path to the file to upload.
            destination (str): The destination path in Azure blob storage. Ex:
                https://<storage_account>.blob.core.windows.net/<storage_container>/<path>
        Raise:
            LaunchError: If the file could not be uploaded.
        zCould not upload file z to Azure blob https://.blob.core.windows.net
credentialrbT)	overwriteNz: )	parse_urir   r   openupload_blobr   r   messager   	__class____name__)r   r   r   storage_accountstorage_containerpath_err_prefixcredsclientfr   r   r   r   upload_file%   s*   

&zAzureEnvironment.upload_filec                    s   t  )z)Upload a directory to Azure blob storage.)NotImplementedError)r   r   r   r   r   r   
upload_dir@   s   zAzureEnvironment.upload_diruric              
      sp   |   }| |\}}}ztd| d|d}|| W dS  ty7 } ztd| d| d|d}~ww )zsVerify that the given blob storage prefix exists.

        Args:
            uri (str): The URI to verify.
        r   r   r   zCould not verify storage URI z in container .N)r   r"   r   get_container_clientr   r   )r   r2   r,   r(   r)   _r-   r   r   r   r   verify_storage_uriD   s"   
z#AzureEnvironment.verify_storage_uric                    s   |    dS )z*Verify that the AzureEnvironment is valid.N)r   r   r   r   r   r   W   s   zAzureEnvironment.verifyc                 C   s>   t | }|du rtd|  d|d|d|dfS )zParse an Azure blob storage URI into a storage account and container.

        Args:
            uri (str): The URI to parse.

        Returns:
            Tuple[str, str, prefix]: The storage account, container, and path.
        NzCould not parse Azure blob URI r3   r
   r      )r	   matchr   group)r2   r8   r   r   r   r"   [   s   

zAzureEnvironment.parse_uri)r   N)T)r'   
__module____qualname____doc__r   classmethoddictboolr   r   r   r   strr/   r1   r6   r   staticmethodtupler"   r   r   r   r   r      s    


"r   N)r<   azure.core.exceptionsr   azure.identityr   azure.storage.blobr   r   typing_extensionsr   errorsr   utilsr	   abstractr   r   r   r   r   r   <module>   s    