
    $                         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 ddlmZ ddlmZ dZdZd Zd Zd ZddZd Zd Zd Zy)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                     | s|r| r|rt        d      | rEt        j                  |       }t        j                  j                  dj                  |             |S |S )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                     ~| j                    S )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                     t        j                  dd      }t        j                  dd      }t        j                  |       }|j
                  j                  |j                  |            S )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   t        | |      }t        j                  d      5  t        j                         j                  t        |gt        t        j                  j                  j                  j                                ddd       y# 1 sw Y   yxY w)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   su     2(NK''13	MMO!!$%1&&CCJJL " 3 3 3s   A!BBc           	         t        j                  | j                        }d|v rt        |d   j	                  dd            }t        j                  |d      }t        |d   j	                  dd            }t        |d   j	                  dd            }|rt        t        ||z  d      d	z        }nd}d
j                  |t        j                  |d      t        j                  |d            }ndj                  t              }t        }d|v r)t        |d   D 	cg c]  }	t        |	d          c}	      }
nd}
t        j                         j                  }|j                  t        k(  rd}n||j                  t!        |      z     }t"        j$                  j'                  dj                  |d   |||
|             yc c}	w )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                     t        j                  dt              j                  t        | gt
        d      }t        |t        j                  t        dd             |S )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                    t        |       }t        j                  |j                        }t	        j
                  |j                        }t        j                  j                  d|z          t        j                  j                  dt	        j                  |d         z          d|v r%t        j                  j                  d|d   z          t        |j                        }t        j                  |j                        }d|v r%t        j                  j                  d|d   z          d|v r4d	|z   }t        j                  j                  d
j                  |             yy)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   