o
    xi                     @  s   d Z ddlm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 ddlmZ d	d
lmZ er>ddlmZ ddlmZ edddjZedddjZG dd deZdS )z/Implementation of AzureContainerRegistry class.    )annotationsN)TYPE_CHECKING)AzureEnvironment)LaunchError)"AZURE_CONTAINER_REGISTRY_URI_REGEX)
get_module   )AbstractRegistry)ContainerRegistryClient)ResourceNotFoundErrorzazure.containerregistryzThe azure-containerregistry package is required to use launch with Azure. Please install it with `pip install azure-containerregistry`.)requiredzazure.core.exceptionszmThe azure-core package is required to use launch with Azure. Please install it with `pip install azure-core`.c                   @  sN   e Zd ZdZ			ddddZedddZdddZdddZdddZ	dS )AzureContainerRegistryz8Helper for accessing Azure Container Registry resources.Nuri
str | Noneregistry_name	repo_namec                 C  s   |durF|| _ tdd ||fD rtd| j dr&| j tdd | _ t| j }|du r8td| j  |d| _|d| _	dS td	d ||fD rUtd
|| _|| _	| j d| j	 | _ dS )z%Initialize an AzureContainerRegistry.Nc                 s  s    | ]}|d uV  qd S N .0xr   r   f/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/registry/azure_container_registry.py	<genexpr>+       z2AzureContainerRegistry.__init__.<locals>.<genexpr>zFPlease specify either a registry name and repo name or a registry URI.https://.Unable to parse Azure Container Registry URI: r      c                 s  s    | ]}|d u V  qd S r   r   r   r   r   r   r   9   r   zDPlease specify both a registry name and repo name or a registry URI..azurecr.io/)
r   anyr   
startswithlenr   matchgroupr   r   )selfr   r   r   r!   r   r   r   __init__"   s,   
zAzureContainerRegistry.__init__configdictreturnc                 C  s$   | d}|du rtd| |dS )a  Create an AzureContainerRegistry from a config dict.

        Args:
            config (dict): The config dict.
            environment (AbstractEnvironment): The environment to use.
            verify (bool, optional): Whether to verify the registry. Defaults to True.

        Returns:
            AzureContainerRegistry: The registry.

        Raises:
            LaunchError: If the config is invalid.
        r   Nz=Please specify a registry name to use under the registry.uri.)r   )getr   )clsr%   r   r   r   r   from_configA   s   
z"AzureContainerRegistry.from_configtuple[str, str]c                   s   t )z1Get username and password for container registry.)NotImplementedErrorr#   r   r   r   get_username_password\   s   z,AzureContainerRegistry.get_username_password	image_uristrboolc           	   
     s   t t|}|du rtd| |d}|d}|d}t }td| d|}z	||| W dS  t	yA   Y d	S  t
yT } ztd
| |d}~ww )zCheck if image exists in container registry.

        Args:
            image_uri (str): Image URI to check.

        Returns:
            bool: True if image exists, False otherwise.
        Nr   r   r      r   z.azurecr.ioTFz=Unable to check if image exists in Azure Container Registry: )rer!   r   r   r"   r   get_credentialsr
   get_manifest_propertiesr   	Exception)	r#   r/   r!   registry
repositorytag
credentialclienter   r   r   check_image_exists`   s0   	


z)AzureContainerRegistry.check_image_existsc                   s   | j  d| j S )Nr   )r   r   r-   r   r   r   get_repo_uri}   s   z#AzureContainerRegistry.get_repo_uri)NNN)r   r   r   r   r   r   )r%   r&   r'   r   )r'   r+   )r/   r0   r'   r1   )r'   r0   )
__name__
__module____qualname____doc__r$   classmethodr*   r.   r=   r>   r   r   r   r   r      s    

r   )rB   
__future__r   r3   typingr   .wandb.sdk.launch.environment.azure_environmentr   wandb.sdk.launch.errorsr   wandb.sdk.launch.utilsr   
wandb.utilr   abstractr	   azure.containerregistryr
   azure.core.exceptionsr   r   r   r   r   r   <module>   s2    