
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSK	J
r  SSKJr  SS	KJr   " S
 S\R                   5      r " S S\5      rg)zDManage and stream logs in-progress or completed PipelineRun/TaskRun.    )absolute_import)division)unicode_literalsN)logs)client_util)common)logc                   |    \ rS rSrSrSrSr\R                  4S jr	\
\R                  4S j5       rS rS rS	 rS
rg)GCLLogTailer   zBHelper class to tail logs from GCL, printing content as available.
cloudbuild_AllLogsc                 R   [         R                  " 5       U l        X0l        Xl        X l        SR                  U R                  U R                  S9U l        SR                  U R                  U R
                  U R                  U R                  S9U l
        X@l        XPl        SU l        g )NzDprojects/{project_id}/locations/global/buckets/_Default/views/{view})
project_idviewzHprojects/{project_id}/locations/{location}/buckets/{bucket}/views/{view})r   locationbucketr      )v1_logs_utilGetGCLLogTailertailer
log_filterprojectr   formatALL_LOGS_VIEWdefault_log_viewCLOUDBUILD_BUCKETworkerpool_log_viewhas_tipp_pooloutbuffer_window_seconds)selfr   r   r   r   r    s         0lib/googlecloudsdk/api_lib/cloudbuild/v2/logs.py__init__GCLLogTailer.__init__"   s     ..0DK OLMNf4+=+=f> 	  jpp<<%%	  q  !D
 'H!"D    c                     U " UUUUUS9$ )z'Build a GCLLogTailer from a log filter.)r   r   r   r   r     )clsr   r   r   r   r    s         r#   
FromFilterGCLLogTailer.FromFilter6   s     
 # r&   c                    U R                   (       d  gU R                  (       a  U R                  /nOU R                  /nU R                   R	                  UU R
                  U R                  S9nU R                  S5        U H/  nU R                  UR                  5      nU R                  U5        M1     U R                  S5        g)z9Tail the GCL logs and print any new bytes to the console.N)r!    REMOTE RUN OUTPUT z& RUN FINISHED; TRUNCATING OUTPUT LOGS )r   r   r   r   TailLogsr   r!   _PrintFirstLine_ValidateScreenReadertext_payload_PrintLogLine_PrintLastLiner"   resource_namesoutput_logsoutputtexts        r#   TailGCLLogTailer.TailC   s     ;;001n--.n++&&"88 ' K 	./''(;(;<d
  	@A
r&   c                     [         R                  " U R                  5        U R                  (       a  U R                  R	                  5         gg)zStop log tailing.N)timesleepr!   r   Stop)r"   s    r#   r>   GCLLogTailer.Stop^   s2     	JJt))*{{
kk r&   c                 H   U R                   (       a  U R                  /nOU R                  /n[        R                  " U R
                  SUS9nU R                  S5        U H/  nU R                  UR                  5      nU R                  U5        M1     U R                  5         g)zPrint GCL logs to the console.asc)r   order_byr5   r-   N)r   r   r   r   	FetchLogsr   r/   r0   textPayloadr2   r3   r4   s        r#   PrintGCLLogTailer.Printe   s    001n--.n""??%K 	./''(:(:;d
  	r&   )	r!   r   r   r   r   r    r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   r	   statusr$   classmethodr    r*   r9   r>   rE   __static_attributes__r(   r&   r#   r   r      sI    J"- ?Bjj#( =@WW
 
6r&   r   c                   `    \ rS rSrSrSS jrS rS rS rS r	\
R                  4S jrS	 rS
rg)CloudBuildLogClient{   zGClient for interacting with the Cloud Build API (and Cloud Build logs).c                 D    [         R                  " 5       U l        Xl        g N)v2_client_utilGetClientInstance	v2_client
sleep_time)r"   rW   s     r#   r$   CloudBuildLogClient.__init__~   s    #557DN Or&   c                 X    U(       a  U R                  XRX15      $ U R                  XRU5      $ rS   )_GetWorkerPoolLogFilter_GetNonWorkerPoolLogFilter)r"   regionrun_idrun_typer   create_times         r#   _GetLogFilter!CloudBuildLogClient._GetLogFilter   s+    ))+xPP,,[&IIr&   c                 "    SR                  XUS9$ )NzTtimestamp>="{timestamp}" AND labels.location="{region}" AND labels.run_name={run_id})	timestampr\   r]   r   )r"   r_   r]   r\   s       r#   r[   .CloudBuildLogClient._GetNonWorkerPoolLogFilter   s    	$f{&fABr&   c                 6    US:X  a  SOSnSR                  XRXS9$ )NtaskruntaskRunpipelineRunz(labels."k8s-pod/tekton.dev/{run_label}"="{run_id}" OR labels."k8s-pod/tekton_dev/{run_label}"="{run_id}") AND timestamp>="{timestamp}" AND resource.labels.location="{region}")	run_labelr]   rc   r\   rd   )r"   r_   r]   r^   r\   rj   s         r#   rZ   +CloudBuildLogClient._GetWorkerPoolLogFilter   s1    %2	I	K fk  	r&   c                    UR                   c<  [        R                  " X4XV5      n[        R                  " S5        UR                   c  M<  U(       a0  [        R                  " U R
                  5        UR                  5         U$ )z.Checks whether a log tailer should be stopped.   )completionTimerT   GetRunr<   r=   rW   r>   )r"   
log_tailerrunr   r\   r]   r^   s          r#   ShouldStopTailer$CloudBuildLogClient.ShouldStopTailer   sa     


$!!'6Dc
jjm 


$  jj!ooJr&   c                    [         R                  " XX45      n[        UR                  5      =(       a    SUR                  ;  nU R	                  X#XGUR
                  5      n[        R                  XXUS9n	Sn
U	(       a.  [        R                  " U	R                  S9n
U
R                  5         U R                  XXUU5      nU
(       a)  U
R                  5         U
R                  b  U
R                  eU$ )z(Streams the logs for a run if available.workerPoolSecondGen)r    N)target)rT   ro   bool
workerPoolr`   
createTimer   r*   r   ThreadInterceptorr9   startrr   join	exception)r"   r   r\   r]   r^   r    rq   r   r   rp   ts              r#   StreamCloudBuildLogClient.Stream   s    



BC 	S^^L!6cnn!L  ##J (( ) J 	A

(
(

@aggi



& (*Cffh	
	 kkJr&   c                    [         R                  " XX45      n[        UR                  5      =(       a    SUR                  ;  nU R	                  X#XFUR
                  5      n[        R                  XXv5      nU(       a  UR                  5         gg)zPrint the logs for a run.ru   N)	rT   ro   rw   rx   r`   ry   r   r*   rE   )	r"   r   r\   r]   r^   rq   r   r   rp   s	            r#   PrintLogCloudBuildLogClient.PrintLog   s     


BCS^^L!6cnn!L  ##J ((J  r&   )rW   rV   N)<   )rG   rH   rI   rJ   rK   r$   r`   r[   rZ   rr   r	   r    r   r   rN   r(   r&   r#   rP   rP   {   s3    O!JB ;>'' 6r&   rP   )rK   
__future__r   r   r   r<   !googlecloudsdk.api_lib.cloudbuildr   r   $googlecloudsdk.api_lib.cloudbuild.v2r   rT   googlecloudsdk.api_lib.loggingr   googlecloudsdk.corer	   
TailerBaser   objectrP   r(   r&   r#   <module>r      sF    K &  '  B N 1 #[<** [|[& [r&   