
    $                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SrSrS rS rS rSS jrS rS rS rg)z-Utils for common operations API interactions.    )absolute_import)division)unicode_literals)encoding)	jobs_util)apis)	name_util)log)
properties)console_attr)progress_tracker)retry)scaled_integerUNKNOWNc                     U (       d  U(       a  U (       a  U(       a  [        S5      eU (       aF  [        R                  " U 5      n[        R                  R                  SR                  U5      5        U$ U$ )z5Returns operation name or last operation of job name.z9job_name or operation_name must be provided but not both.zLatest Operation: {})
ValueErrorr   block_until_operation_createdr
   statusPrintformat)job_nameoperation_namelatest_operation_names      6lib/googlecloudsdk/api_lib/transfer/operations_util.py_get_operation_to_pollr   %   s^    
>xN
CE E %CCHMJJ+223HIJ  	    c                 &    AU R                   (       + $ )zETakes Operation Apitools object and returns if it is not marked done.)done)resultretryer_states     r   _is_operation_in_progressr"   2   s    [[r   c                     [         R                  " SS5      n[         R                  " SS5      n[        R                  " U 5      nUR
                  R                  UR                  US95      $ )z6Returns operation details from API as Apitools object.transferv1)name)r   GetClientInstanceGetMessagesModuler	   add_operation_prefixtransferOperationsGet+StoragetransferTransferOperationsGetRequest)r&   clientmessagesformatted_operation_names       r   api_getr0   8   sf    !!*d3&##J5(&;;DA		"	"	&	&::' ; )
* *r   Nc                 2   [        X5      n[        R                  " SS9   [        R                  " 5       R                  [        U/[        [        R                  R                  R                  R                  5       S9  SSS5        g! , (       d  f       g= f)a  Does not return until API responds that operation is done.

Args:
  job_name (str|None): If provided, poll job's last operation.
  operation_name (str|None): Poll this operation name.

Raises:
  ValueError: One of job_name or operation_name must be provided.
z!Waiting for operation to complete)messageargsshould_retry_ifsleep_msN)r   r   ProgressTrackerr   RetryerRetryOnResultr0   r"   r   VALUESr$   no_async_polling_interval_msGetInt)r   r   polling_operation_names      r   block_until_doner>   C   ss     2(K''13	MMO!!$%1&&CCJJL " 3 3 3s   AB
Bc           	         [         R                  " U R                  5      nSU;   a  [        US   R	                  SS5      5      n[
        R                  " USS9n[        US   R	                  SS5      5      n[        US   R	                  SS5      5      nU(       a  [        [        XV-  S5      S	-  5      nOSnS
R                  U[
        R                  " USS9[
        R                  " USS95      nOSR                  [        5      n[        nSU;   a,  [        US    V	s/ s H  n	[        U	S   5      PM     sn	5      n
OSn
[        R                  " 5       R                  nUR                  [        :X  a  SnOXR                  [!        U5      -     n["        R$                  R'                  SR                  US   XU
U5      5        gs  sn	f )zBGets operation from API and prints its progress updating in-place.countersbytesFromSourceSkippedBySyncr      )decimal_placesbytesCopiedToSinkbytesFoundFromSource   d   z{}% ({} of {})zProgress: {}errorBreakdowns
errorCount z&{} | {} | Skipped: {} | Errors: {} {}r   N)r   MessageToDictmetadataintgetr   FormatBinaryNumberroundr   _UNKNOWN_VALUEsumr   ProgressTrackerSymbolsAscii
spin_marksretrial_LAST_RETRIALlenr
   r   write)	operationr!   rL   skipped_bytesskipped_stringcopied_bytestotal_bytesprogress_percentprogress_stringerrorerror_countrT   	spin_marks                r   _print_progressrc   Y   s   ##I$6$67(8,001O124 5M#66a)N x
+//0CQGHLhz*../EqIJKU<#=qACGH&--)),qI))+aHJO %++N;O#N("/78I/JK/JeU< 	!/JKMK K779DD*m+I003z?BCI**=EEx/;  	Ls   !Gc           	          [         R                  " S[        S9R                  [        U /[
        SS9n[        U[         R                  " [        SSS95        U$ )zPrints progress of operation and blocks until transfer is complete.

Args:
  name (str|None): Poll this operation name.

Returns:
  Apitools Operation object containing the completed operation's metadata.
r   )	jitter_msstatus_update_funci  r3   N)rU   time_passed_mstime_to_wait_ms)r   r8   rc   r9   r0   r"   RetryerStaterV   )r&   complete_operations     r   _poll_progressrk      sd     }}o77D}
v3	 8E 8  dLM 
r   c                    [        U 5      n[        R                  " UR                  5      n[        R
                  " UR                  5      n[        R                  R                  SU-   5        [        R                  R                  S[        R                  " US   5      -   5        SU;   a%  [        R                  R                  SUS   -   5        [        UR                  5      n[        R                  " UR                  5      nSU;   a%  [        R                  R                  SUS   -   5        SU;   a4  S	U-   n[        R                  R                  S
R                  U5      5        gg)zFPrints and updates operation statistics, blocking until copy complete.zOperation name: zParent job: transferJobName	startTimezStart time: endTimez
End time: rH   z$gcloud transfer operations describe z!
To investigate errors, run: 
{}
N)r0   r   rK   rL   r	   remove_operation_prefixr&   r
   r   r   remove_job_prefixrk   r   )r&   initial_operationinitial_metadatar   final_operationfinal_metadatadescribe_commands          r   display_monitoring_viewrw      s1   dm++,=,F,FG445F5K5KL.**%67**!!"23D"EFGH $$JJ^&6{&CCD"#4#9#9:/))/*B*BC.. JJ^nY&??@.(>OJJ.556FGI )r   )NN)__doc__
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.transferr   googlecloudsdk.api_lib.utilr   #googlecloudsdk.command_lib.transferr	   googlecloudsdk.corer
   r   googlecloudsdk.core.consoler   r   googlecloudsdk.core.utilr   r   rV   rQ   r   r"   r0   r>   rc   rk   rw    r   r   <module>r      sb    4 &  ' % 5 , 9 # * 4 8 * 3 
*,'T,Ir   