
                         D   d 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 ej&                   ej(                  ej*                  j,                        ej.                   G d dej0                                             Zy)%Copy an Artifact Registry repository.    )absolute_import)division)unicode_literals)waiter)base)flags)requests)log)	resources)progress_tracker)retryc                   >    e Zd ZdZddddZed        Zd Zd Zd Z	y	)
Copyr   z
          Copy all artifacts within an Artifact Registry repository to another repository.

          Note that this is pull-based, so default arguments will apply to the destination repository.
      a            To copy artifacts from `repo1` in project `proj1` and location `us` to `repo2` in project `proj2` and location `asia`:

            $ {command} repo2 --project=proj2 --location=asia --source-repo=projects/proj1/locations/us/repositories/repo1
      )BRIEFDESCRIPTIONEXAMPLESc                     t        j                         j                  |        t        j                  j                  |        | j                  dddd       y)z\Set up arguments for this command.

    Args:
      parser: An argparse.ArgumentParser.
    z--source-repoSOURCE_REPOSITORYTz2The source repository path to copy artifacts from.)metavarrequiredhelpN)r	   
GetRepoArgAddToParserr   
ASYNC_FLAGadd_argument)parsers    *lib/surface/artifacts/repositories/copy.pyArgsz	Copy.Args3   sL     
""6*OO'
#A	      c                    	
 t        j                         }|j                  j                  j	                         }t        j
                  |j                  |j                               }t        j                  j                  |j                  d      	t        j                  j                  dj                  |j                  |j                         	j                                      |j                   ryd}dddt#        j$                  | fdd	
      5  t        j                  j                  d       t'        j(                  |j*                        
t-        j.                  d      }	 |j1                  	
 fdd d      }|j2                  rQt        j                  j                  dj                  	j                         |j2                  j4                               	 ddd       y# t,        j6                  $ r  t        j2                  d       Y ddd       yt8        $ r6}t        j:                  dj                  |             Y d}~ddd       yd}~ww xY w# 1 sw Y   yxY w)z Run the repository copy command.z.artifactregistry.projects.locations.operations)
collectionz>Copy request issued from [{}] to [{}].
Created operation [{}].NzCopying artifactsr   )copiedtotalc                  &    j                         S N)_DetailMessage)progress_infoselfs   r   <lambda>zCopy.Run.<locals>.<lambda>\   s    (;(;M(Jr    T)detail_message_callbackautotickz.Press CTRL-C to stop waiting on the operation.)max_wait_msc                  *    j                         S r&   )_PollOperation)op_refpollerr(   r)   s   r   r*   zCopy.Run.<locals>.<lambda>i   s    D''Fr    c                 $    | xr | j                    S r&   )done)opstates     r   r*   zCopy.Run.<locals>.<lambda>j   s    b.@[.@r    i  )should_retry_ifsleep_msz
Operation [{}] failed: {}z2Error: Copy operation wait unexpectedly timed out.z An unexpected error occurred: {})r
   	GetClientCONCEPTS
repositoryParseCopyRepositorysource_repoRelativeNamer   REGISTRYParseRelativeNamenamer   statusPrintformatasync_r   ProgressTrackerr   CloudOperationPollerNoResourcesprojects_locations_operationsr   RetryerRetryOnResulterrormessageWaitException	Exceptionfatal)r)   argsclientrepo_refr4   spinner_messageretryer	operationer0   r1   r(   s   `        @@@r   RunzCopy.RunC   s    !F}}''--/H		 	 !1!183H3H3J	KB11
L 2 F
 JJI	h335v7J7J7L

 {{ *O 1-M		)	) J
 
jjGH 55

.
.f $/g))F@ * 
	
 ??
**

+22%%')@)@
 3
 
4    		FG;
 
<  		4;;A>?A
 
<=
 
s>   AI'2A8G44(I$I'&I$.$II'I$$I''I0c                 X    |d   }|d   }|dk(  ry||z  dz  }dj                  |||      S )z0Callback to update the progress tracker message.r#   r$   r   z% operation metadata not yet availabled   z# {:.1f}% copied ({} of {} versions))rD   )r)   r(   r#   r$   progresss        r   r'   zCopy._DetailMessage|   sC    8$F'"Ez4~#H077&%PPr    c                    	 |j                  |      }|r|j
                  rz|j
                  j                  rd|j
                  j                  D ci c]  }|j                  |j                   }}d|v r|d   j                  |d<   d|v r|d   j                  |d<   |S # t        j                  $ r }t        j                  d|       Y d}~yd}~ww xY wc c}w )z<Polls the operation and updates progress_info from metadata.zPolling error: %sNversionsCopiedCountr#   totalVersionsCountr$   )
Pollr   PollExceptionr   debugmetadataadditionalPropertieskeyvalueinteger_value)r)   r1   r0   r(   rU   rV   ppropss           r   r/   zCopy._PollOperation   s    ++f%i 	33'0'9'9'N'NO'N!quuagg~'NeO	%	'"'(=">"L"Lh		&!&';!<!J!Jg!  	ii#Q' Ps   B CC0CCN)
__name__
__module____qualname____doc__detailed_helpstaticmethodr   rW   r'   r/    r    r   r   r      sB     . 7


-  7r	Qr    r   N)rk   
__future__r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   $googlecloudsdk.command_lib.artifactsr	   r
   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   UniverseCompatibleReleaseTracksReleaseTrackGAHiddenCommandr   rn   r    r   <module>r|      s    , &  ' . ( 6 9 # ) 8 * D%%(()y4<< y  * yr    