o
    xi.!                     @  s   d Z ddlmZ ddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZ ddlmZ eeZed	d
dZeddde_edddej_G dd deZdS )z<Implementation of Google Artifact Registry for wandb launch.    )annotationsN)LaunchError)GCP_ARTIFACT_REGISTRY_URI_REGEXevent_loop_thread_exec)
get_module   )AbstractRegistrygooglezmThe google package is required to use launch with Google. Please install it with `pip install wandb[launch]`.)requiredzgoogle.authzrThe google-auth package is required to use launch with Google. Please install it with `pip install wandb[launch]`.zgoogle.cloud.artifactregistryzThe google-cloud-artifactregistry package is required to use launch with Google. Please install it with `pip install wandb[launch]`.c                   @  sR   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 )!GoogleArtifactRegistryaR  Google Artifact Registry helper for interacting with the registry.

    This helper should be constructed from either a uri or a repository,
    project, and optional image-name. If constructed from a uri, the uri
    must be of the form REGION-docker.pkg.dev/PROJECT/REPOSITORY/[IMAGE_NAME],
    with an optional https:// preceding.
    Nuri
str | None
repository
image_nameprojectregionreturnNonec                 C  sb  t d| d|  |durH|| _t||||grtdt| j}|s/td| j d|d| _|d| _	|d	| _
|d
| _n.tdd |||fD rXtd|| _|| _	|| _
|| _| j	 d| j d| j
 d| j | _d}| j	st|d| j
st|d	| jst|dtj \| _}| jp|| _| jtjjj  dS )a  Initialize the Google Artifact Registry.

        Either uri or repository and image_name must be provided. Project and
        region are optional, and will be inferred from the uri if provided, or
        from the default credentials if not.

        Arguments:
            uri (optional): The uri of the repository.
            repository (optional): The repository name.
            image_name (optional): The image name.
            project (optional): The GCP project name.
            region (optional): The GCP region name.

        Raises:
            LaunchError: If verify is True and the container registry or its
                environment have not been properly configured. Or if the environment
                is not an instance of GcpEnvironment.
        z6Initializing Google Artifact Registry with repository z and image name NzThe Google Artifact Registry must be specified with either the uri key or the repository, image-name, project and region keys, but not both.z!The Google Artifact Registry uri zb is invalid. Please provide a uri of the form REGION-docker.pkg.dev/PROJECT/REPOSITORY/IMAGE_NAME.r   r   r   r   c                 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/google_artifact_registry.py	<genexpr>a   s    z2GoogleArtifactRegistry.__init__.<locals>.<genexpr>z~The Google Artifact Registry must be specified with either the uri key or the repository, image-name, project and region keys.-docker.pkg.dev//zoThe Google Artifact Registry is missing the {} kwarg. Please specify it by name or as part of the uri argument.
image-name)_loggerinfor   anyr   r   matchgroupr   r   r   r   formatr	   authdefaultcredentialsrefresh	transportrequestsRequest)selfr   r   r   r   r   r   _missing_kwarg_msgr   r   r   __init__-   sP   $zGoogleArtifactRegistry.__init__configdictc                 C  sb   h d}t | | }|rtd| d| d| |d|d|d|d|d	d
S )at  Create a Google Artifact Registry from a config.

        Arguments:
            config: A dictionary containing the following keys:
                repository: The repository name.
                image-name: The image name.
            environment: A GcpEnvironment configured for access to this registry.

        Returns:
            A GoogleArtifactRegistry.
        >   r   typer   r   r   r   z@The Google Artifact Registry config contains unacceptable keys: z5. Please remove these keys. The acceptable keys are: .r   r   r   r   r   )r   r   r   r   r   )setkeysr   get)clsr,   acceptable_keysunacceptable_keysr   r   r   from_config|   s"   z"GoogleArtifactRegistry.from_configtuple[str, str]c                   s,   | j js| j tjjj  d| j jfS )z|Get the username and password for the registry.

        Returns:
            A tuple of the username and password.
        oauth2accesstoken)r$   tokenr%   r	   r"   r&   r'   r(   r)   r   r   r   get_username_password   s   z,GoogleArtifactRegistry.get_username_passwordstrc                   s$   | j  d| j d| j d| j S )zGet the URI for the given repository.

        Arguments:
            repo_name: The repository name.

        Returns:
            The repository URI.
        r   r   )r   r   r   r   r:   r   r   r   get_repo_uri   s   
z#GoogleArtifactRegistry.get_repo_uri	image_uriboolc              
     s   t d| d |d\}}|  I dH }||kr(td| d| j dd| j d	| j d
| j }t	t
jjj}|| jdI dH }t	|j}z|d|idI dH D ]}	||	jv ra W dS qVW dS  t
jjjy} }
 z
td| j d|
d}
~
ww )zCheck if the image exists.

        Arguments:
            image_uri: The image URI.

        Returns:
            True if the image exists, False otherwise.
        zChecking if image z exists:Nz
The image z, does not match to the image uri repository r/   z	projects/z/locations/z/repositories/)r$   parent)requestTz(The Google Artifact Registry repository zH does not exist. Please create it or modify your registry configuration.F)r   r   splitr=   r   r   r   r   r   r   r	   cloudartifactregistryArtifactRegistryClientr$   list_docker_imagestagsapi_core
exceptionsNotFound)r)   r>   repo_uritagself_repo_urirA   artifact_registry_clientclientlist_imagesimageer   r   r   check_image_exists   s>   	

z)GoogleArtifactRegistry.check_image_exists)NNNNN)r   r   r   r   r   r   r   r   r   r   r   r   )r,   r-   r   r   )r   r7   )r   r<   )r>   r<   r   r?   )
__name__
__module____qualname____doc__r+   classmethodr6   r;   r=   rT   r   r   r   r   r   $   s    
O
'

r   )rX   
__future__r   logginggoogle.authr	   google.cloud.artifactregistrywandb.sdk.launch.errorsr   wandb.sdk.launch.utilsr   r   
wandb.utilr   abstractr   	getLoggerrU   r   r"   rD   rE   r   r   r   r   r   <module>   s.    

