
    [0                        d Z ddlmZ ddlm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 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ZdZdZdZd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d  Z'd! Z(d" Z)y)#zAUtility for forming settings for Artifacts Registry repositories.    )absolute_import)division)unicode_literalsN)
exceptions)requests)util)apt)credentials)gradle)mvn)npm)python)yum)
propertiesz2.2.0zFailed to find attribute [project]. The attribute can be set in the following ways:
- provide the argument [--project] on the command line
- set the property [core/project]zFailed to find attribute [repository]. The attribute can be set in the following ways:
- provide the argument [--repository] on the command line
- set the property [artifacts/repository]zFailed to find attribute [location]. The attribute can be set in the following ways:
- provide the argument [--location] on the command line
- set the property [artifacts/location]c                     | j                   sKt        j                  j                  j                   j	                         st        j                  t              t        j                  |       S N)
projectr   VALUEScoreGetar_exceptionsInvalidInputValueError_PROJECT_NOT_FOUND_ERRORar_util
GetProjectargss    Hlib/googlecloudsdk/command_lib/artifacts/print_settings/settings_util.py_GetRequiredProjectValuer   8   sI    	j//44<<@@B

.
./G
HH			D	!!    c                     | j                   sKt        j                  j                  j                   j	                         st        j                  t              t        j                  |       S r   )

repositoryr   r   	artifactsr   r   r   _REPO_NOT_FOUND_ERRORr   GetRepor   s    r   _GetRequiredRepoValuer&   >   sG    	!2!2!<!<!G!G!K!K!M

.
./D
EE		r    c                     | j                   sKt        j                  j                  j                   j	                         st        j                  t              t        j                  |       S r   )
locationr   r   r#   r   r   r   _LOCATION_NOT_FOUND_ERRORr   GetLocationr   s    r   _GetRequiredLocationValuer+   D   sI    	z00::CCGGI

.
./H
II			T	""r    c                 $   t        |       }t        |       }t        |       }|dz   |z   }t        j                  dj                  |||            }|j
                  |k7  r/t        j                  dj                  |j
                  |            ||fS z,Get resource values and validate user input./(projects/{}/locations/{}/repositories/{}z-Invalid repository type {}. Valid type is {}.)r&   r   r+   ar_requestsGetRepositoryformatr   r   r   repo_formatrepor   r(   	repo_paths         r   _GetLocationAndRepoPathr7   J   s    	t	$$$T*'&t,(md")		"	"0778<>
?$ 
[[K

.
.7>>KK	&' ' 
9	r    c                 :   t        |       }t        |       }t        |       }|dz   |z   }t        j                  dj                  |||            }|j
                  |k7  r/t        j                  dj                  |j
                  |            |||j                  fS r-   )	r&   r   r+   r0   r1   r2   r   r   mavenConfigr3   s         r   "_GetLocationRepoPathAndMavenConfigr:   Z   s    	t	$$$T*'&t,(md")		"	"0778<>
?$ 
[[K

.
.7>>KK	&' ' 
9d..	..r    c                     t        j                  dj                  | ||            }t        |d      r0|j                  D ]!  }d|j
                  v sd|j                  v s! y y)zDetermine if a repository is public.

  Args:
    project: Project name.
    location: Repository location.
    repo: Repository name.

  Returns:
    bool, True if repository is public.
  r/   bindingsallUserszartifactregistry.readerTF)r0   GetIamPolicyr2   hasattrr<   membersrole)r   r(   r5   
iam_policybindings        r   IsPublicReporD   j   se     ''077
8T#$* Z$&&

''7<<7 '
 
r    c                 >   t        j                         }t        | |j                  j                  j
                        \  }}t        |       }t        |       }||||d}t        |||      rt        j                  }nt        j                  } |j                  di |S )zForms an apt settings snippet to add to the sources.list.d directory.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.

  Returns:
    An apt settings snippet.
  )r(   r   r5   r6    )r0   GetMessagesr7   
RepositoryFormatValueValuesEnumAPTr&   r   rD   r	   PUBLIC_TEMPLATEDEFAULT_TEMPLATEr2   )r   messagesr(   r6   r5   r   dataapt_setting_templates           r   GetAptSettingsSnippetrP      s     $$&(/
H5599;(I	t	$$$T*' 	
$ '8T*..//	$		$	$	,t	,,r    c                 <   t        j                         }t        | |j                  j                  j
                        \  }}t        |       }t        |       }|||d}t        |||      rt        j                  }nt        j                  } |j                  di |S )zForms a Yum settings snippet to add to the yum.repos.d directory.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.

  Returns:
    A yum settings snippet.
  )r(   r5   r6   rF   )r0   rG   r7   rH   rI   YUMr&   r   rD   r   rK   rL   r2   )r   rM   r(   r6   r5   r   rN   yum_setting_templates           r   GetYumSettingsSnippetrT      s     $$&(/
H5599;(I	t	$$$T*'9	E$'8T*..//	$		$	$	,t	,,r    c                    t        j                         }t        | |j                  j                  j
                        \  }} dj                  di ||d}d}| j                  rZ| j                  j                  d      rt        | j                        dk  rt        j                  d      | j                  dz   |z   }|||d}t        j                  | j                        }|rGt        j                   }t#        j$                  |j'                  d	            j)                  d	      |d
<   nt        j*                  } |j                  di |S )zForms an npm settings snippet to add to the .npmrc file.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.

  Returns:
    An npm settings snippet.
  z#{location}-npm.pkg.dev/{repo_path}/r(   r6   registry@   z>Scope name must start with "@" and be longer than 1 character.:)configured_registryregistry_pathr6   zutf-8passwordrF   )r0   rG   r7   rH   rI   NPMr2   scope
startswithlenr   r   r
   GetServiceAccountCredsjson_keyr   SERVICE_ACCOUNT_TEMPLATEbase64	b64encodeencodedecodeNO_SERVICE_ACCOUNT_TEMPLATE)	r   rM   r(   r6   r\   r[   rN   sa_credsnpm_setting_templates	            r   GetNpmSettingsSnippetrl      sB    $$&(/
H5599;(I>7>> B - #	ZZ::  %TZZA)=00
LN N**s*-@@ 1$
$ //>(77'' ""(&/ 	 ::	$		$	$	,t	,,r    c                 F   t        j                         }t        | |j                  j                  j
                        \  }}}d|d|d}t        j                  | j                        }t        |||      }|rd|d<   d|d<   ||d<   n	t        |d	<    |j                  d
i |S )zForms a maven snippet to add to the pom.xml file.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.

  Returns:
    str, a maven snippet to add to the pom.xml file.
  artifactregistryzartifact-registry)schemer(   	server_idr6   httpsro   _json_key_base64usernamer]   extension_versionrF   )r0   rG   r:   rH   rI   MAVENr
   rb   rc   GetMavenTemplate_EXT_VERSIONr2   )r   rM   r(   r6   	maven_cfgrN   rj   mvn_templates           r   GetMavenSnippetrz      s     $$&(#E
H55;;$= (Iy #&	
$ //>(!(Ix@,DN)DD ,D					$t	$$r    c                    t         j                  }|rQ|j                  | j                  j                  j
                  k(  r$t         j                  }|rt         j                  }|S |rQ|j                  | j                  j                  j                  k(  r$t         j                  }|rt         j                  }|S |rt         j                  }|S )aI  Forms a maven snippet to add to the pom.xml file.

  Args:
    messages: Module, the messages module for the API.
    maven_cfg: MavenRepositoryConfig, the maven configuration proto that
      contains the version policy.
    sa_creds: str, service account credentials.

  Returns:
    str, a maven template to add to pom.xml.
  )r   ri   versionPolicyMavenRepositoryConfigVersionPolicyValueValuesEnumSNAPSHOT$NO_SERVICE_ACCOUNT_SNAPSHOT_TEMPLATE!SERVICE_ACCOUNT_SNAPSHOT_TEMPLATERELEASE#NO_SERVICE_ACCOUNT_RELEASE_TEMPLATE SERVICE_ACCOUNT_RELEASE_TEMPLATErd   )rM   rx   rj   ry   s       r   rv   rv     s     00,9**h.L.L.i.i.r.rr;;L::l 
 Y,,0N0N0k0k0s0ss::L99l 
 //L	r    c                 8   t        j                         }t        | |j                  j                  j
                        \  }}}t        j                  | j                        }t        |||      }||d}|rd|d<   ||d<   n	t        |d<    |j                  di |S )zForms a gradle snippet to add to the build.gradle file.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.

  Returns:
    str, a gradle snippet to add to build.gradle.
  rV   rr   rs   r]   rt   rF   )r0   rG   r:   rH   rI   ru   r
   rb   rc   GetGradleTemplaterw   r2   )r   rM   r(   r6   rx   rj   gradle_templaterN   s           r   GetGradleSnippetr     s     $$&(#E
H55;;$= (Iy//>(%h	8D/Y	7$)DD !-D					'$	''r    c                    t         j                  }| j                  j                  j                  }| j                  j                  j
                  }|r3|j                  |k(  r$t         j                  }|rt         j                  }|S |r3|j                  |k(  r$t         j                  }|rt         j                  }|S |rt         j                  }|S )aV  Forms a gradle snippet to add to the build.gradle file.

  Args:
    messages: Module, the messages module for the API.
    maven_cfg: MavenRepositoryConfig, the maven configuration proto that
      contains the version policy..
    sa_creds: str, service account credentials.

  Returns:
    str, a gradle template to add to build.gradle.
  )r   ri   r}   r~   r   r   r|   r   r   r   r   rd   )rM   rx   rj   r   snapshotreleases         r   r   r   7  s     66/$$AAJJ  **GGOO'9**h6AAO@@o 
 Y,,7@@O??o 
 55O	r    c                 n   t        j                         }t        | |j                  j                  j
                        \  }}t        |       }|||d}t        j                  | j                        }|r$||d<   t        j                  j                  di |S t        j                  j                  di |S )zForms a Python snippet for .pypirc file (twine) and pip.conf file.

  Args:
    args: an argparse namespace. All the arguments that were provided to this
      command invocation.

  Returns:
    A python snippet.
  )r(   r6   r5   r]   rF   )r0   rG   r7   rH   rI   PYTHONr&   r
   rb   rc   r    SERVICE_ACCOUNT_SETTING_TEMPLATEr2   #NO_SERVICE_ACCOUNT_SETTING_TEMPLATE)r   rM   r(   r6   r5   rN   rj   s          r   GetPythonSettingsSnippetr   V  s     $$&(/
H55<<>(I	t	$$Y	E$//>(D2299ADAA55<<DtDDr    )*__doc__
__future__r   r   r   re    googlecloudsdk.api_lib.artifactsr   r   $googlecloudsdk.command_lib.artifactsr   r0   r   r   3googlecloudsdk.command_lib.artifacts.print_settingsr	   r
   r   r   r   r   r   googlecloudsdk.corer   rw   r   r$   r)   r   r&   r+   r7   r:   rD   rP   rT   rl   rz   rv   r   r   r   rF   r    r   <module>r      s    H &  '  H H @ C K F C C F C *% - + "# / 0-<-4%-P%@6(4>Er    