
                             S 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.    N)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.FromFilter3   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.Tail@   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_byr2   r*   N)r   r   r   r   	FetchLogsr   r,   r-   textPayloadr/   r0   r1   s        r    PrintGCLLogTailer.Printb   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'   r6   r;   rB   __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)CloudBuildLogClientx   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   rT   s     r    r!   CloudBuildLogClient.__init__{   s    #557DN Or#   c                 X    U(       a  U R                  XRX15      $ U R                  XRU5      $ rP   )_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})	timestamprY   rZ   r   )r   r\   rZ   rY   s       r    rX   .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_labelrZ   r`   rY   ra   )r   r\   rZ   r[   rY   rg   s         r    rW   +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.   )completionTimerQ   GetRunr9   r:   rT   r;   )r   
log_tailerrunr   rY   rZ   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)rQ   rl   bool
workerPoolr]   
createTimer   r'   r   ThreadInterceptorr6   startro   join	exception)r   r   rY   rZ   r[   r   rn   r   r   rm   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.rr   N)	rQ   rl   rt   ru   r]   rv   r   r'   rB   )	r   r   rY   rZ   r[   rn   r   r   rm   s	            r    PrintLogCloudBuildLogClient.PrintLog   s     


BCS^^L!6cnn!L  ##J ((J  r#   )rT   rS   N)<   )rD   rE   rF   rG   rH   r!   r]   rX   rW   ro   r   r   r|   r   rK   r%   r#   r    rM   rM   x   s3    O!JB ;>'' 6r#   rM   )rH   r9   !googlecloudsdk.api_lib.cloudbuildr   r   $googlecloudsdk.api_lib.cloudbuild.v2r   rQ   googlecloudsdk.api_lib.loggingr   googlecloudsdk.corer   
TailerBaser   objectrM   r%   r#   r    <module>r      s?    K  B N 1 #[<** [|[& [r#   