
    o                          S r SSK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KJr  SSKJr  SS	KJr  SS
KJr  SrSS jrS r " S S\R(                  5      r " S S\R(                  5      r " S S\R(                  5      r " S S\5      rg);High-level client for interacting with the Cloud Build API.    N)encoding)cloudbuild_util)logs)requests)
exceptions)log)
properties)rangezZError Response:{status_code? [{?}]}{status_message? {?}}{url?
{?}}{details?

Details:
{?}}c                    U R                   b  U R                   R                   H{  nUR                  S:X  d  M  UR                  R                  R
                   HB  nUR                  U:X  d  M  UR                  R                  nU=(       d    UR                  s  s  $    M}     U(       a  [        SR                  U5      5      eg)ap  Extract the value of a build's prop_key from a build operation.

Args:
  build_op: A Google Cloud Builder build operation.
  prop_key: str, The property name.
  required: If True, raise an OperationError if prop_key isn't present.

Returns:
  The corresponding build operation value indexed by prop_key.

Raises:
  OperationError: The required prop_key was not found.
Nbuildz7Build operation does not contain required property [{}])	metadataadditionalPropertieskeyvalueobject_valuer
   string_valueOperationErrorformat)build_opprop_keyrequiredprop
build_propr   s         .lib/googlecloudsdk/api_lib/cloudbuild/build.pyGetBuildPropr   $   s     "!!66	W	**11<<J^^x'%++88L3:#3#33 = 7 
 ))/)9; ;     c                     U R                   (       a`  U R                   R                  (       aE  U R                   R                   H+  nUR                  S:X  d  M  UR                  R                  s  $    g)a  Get the Cloud Build Status from an Operation object.

The op.response field is supposed to have a copy of the build object; however,
the wire JSON from the server doesn't get deserialized into an actual build
object. Instead, it is stored as a generic ResponseValue object, so we have
to root around a bit.

Args:
  op: the Operation object from a CloudBuild build request.

Returns:
  string status, likely "SUCCESS" or "ERROR".
statusUNKNOWN)responser   r   r   r   )opr   s     r   _GetStatusFromOpr#   >   sL     [[R[[5500	X	zz&&& 1 
r   c                       \ rS rSrSrSrg)BuildFailedErrorS   z/Raised when a Google Cloud Builder build fails. N__name__
__module____qualname____firstlineno____doc____static_attributes__r'   r   r   r%   r%   S   s    7r   r%   c                       \ rS rSrSrSrg)OperationTimeoutErrorW   z#Raised when an operation times out.r'   Nr(   r'   r   r   r0   r0   W   s    +r   r0   c                       \ rS rSrSrSrg)r   \   z+Raised when an operation contains an error.r'   Nr(   r'   r   r   r   r   \   s    3r   r   c                   \    \ rS rSrSrSrSrSrSrSS jr	SS	 jr
SS
 jrS rSS jrS rSrg)CloudBuildClienta   r      i  SUCCESSzlog-{build_id}.txtNc                     U=(       d    [         R                  " 5       U l        U=(       d    [         R                  " 5       U l        g N)r   GetClientInstanceclientGetMessagesModulemessages)selfr<   r>   s      r   __init__CloudBuildClient.__init__i   s,    ?O==?DKC A A CDMr   c                     Uc1  [         R                  R                  R                  R	                  SS9nU R
                  R                  R                  U R                  R                  UUS95      nU$ )a9  Execute a call to CloudBuild service and return the build operation.


Args:
  build: Build object. The Build to execute.
  project: The project to execute, or None to use the current project
      property.

Raises:
  BuildFailedError: when the build fails.

Returns:
  build_op, an in-progress build operation.
Tr   )	projectIdr   )
r
   VALUEScoreprojectGetr<   projects_buildsCreater>   %CloudbuildProjectsBuildsCreateRequestr?   r   rG   r   s       r   ExecuteCloudBuildAsync'CloudBuildClient.ExecuteCloudBuildAsyncm   sl     !!&&..22D2Ag{{**11;; 	< 	H Or   c                 H    U R                  X5      nU R                  U5        g)zExecute a call to CloudBuild service and wait for it to finish.


Args:
  build: Build object. The Build to execute.
  project: The project to execute, or None to use the current project
      property.

Raises:
  BuildFailedError: when the build fails.
N)rM   WaitAndStreamLogsrL   s       r   ExecuteCloudBuild"CloudBuildClient.ExecuteCloudBuild   s"     **5:H8$r   c                 X   [        USSS9n[        US5      n[        US5      n[        R                  R                  SR	                  US95        SnS	nU(       a|  U R
                  R	                  US9n[        R                  " UUS
9nU(       a(  [        R                  R                  SU-   5        SU-   nO[        R                  R                  S5        S	nU(       a  UR                  n U R                  XS9n	U(       a  UR                  SS9  [        U	5      n
XR                  :w  aN  [        R                   " ["        R$                  " U	R&                  5      5      n[        SU-   S-   U
-   S-   U-   5      eg	! [         a$    [        R                  " SSS9  [        SU-   5      ef = f)z=Wait for a Cloud Build to finish, streaming logs if possible.idTrC   logUrl
logsBucketz!Started cloud build [{build_id}].)build_idzin the Cloud Console.N)bucketobjz"To see logs in the Cloud Console: zat z'Logs can be found in the Cloud Console.)	operationretry_callback )exc_infoz"Cloud build timed out. Check logs )is_lastzCloud build failed. Check logs z Failure status: z: )r   r	   r   Printr   CLOUDBUILD_LOGFILE_FMT_STRINGcloudbuild_logsGCSLogTailerPollWaitForOperationr0   debugr%   r#   CLOUDBUILD_SUCCESSr   ExtractErrorMessager   MessageToPyValueerror)r?   r   rW   logs_urilogs_bucketlog_loc
log_tailer
log_objectcallbackr"   final_statusmessages               r   rP   "CloudBuildClient.WaitAndStreamLogs   s   HdT:HHh/Hx6KJJ+22H2EG%GJ55<<h<Oj"//j 


=HI("

BCHhM  8 Mb oodo##B'L...,,

#
#BHH
-/g>H235ABDHI&' ( ( / ! M	iiT"AGKLLMs   -E; ;.F)c                 ~    U R                  X5      nU(       d$  [        SR                  UR                  5      5      eU$ )a  Wait until the operation is complete or times out.

This does not use the core api_lib.util.waiter because the cloud build logs
serve as a progress tracker.

Args:
  operation: The operation resource to wait on
  retry_callback: A callback to be executed before each retry, if desired.
Returns:
  The operation resource when it has completed
Raises:
  OperationTimeoutError: when the operation polling times out
z@Operation [{0}] timed out. This operation may still be underway.)_PollUntilDoner0   r   name)r?   rZ   r[   completed_operations       r   rd   !CloudBuildClient.WaitForOperation   sC     --iH! $<=CV'0~~>78 8 r   c                    UR                   (       a  U$ U R                  R                  R                  S5      nU" UR                  S9n[        U R                  5       H  nU R                  R                  R                  U5      nUR                   (       a[  [        R                  " SR                  UR                  [        R                  " [        R                  " U5      SS95      5        Us  $ [        R                  " SR                  UR                  U R                  5      5        [         R"                  " U R                  5        Uc  M  U" 5         M     g)z?Polls the operation resource until it is complete or times out.rH   )ru   z%Operation [{0}] complete. Result: {1}   )indentz+Operation [{0}] not complete. Waiting {1}s.N)doner<   
operationsGetRequestTyperu   r   _MAX_RETRIESrH   r	   re   r   jsondumpsr   MessageToDict_RETRY_INTERVALtimesleep)r?   rZ   r[   request_typerequest_s         r   rt   CloudBuildClient._PollUntilDone   s    ~~;;))88?L	/G4$$%++((,,W5i			9@@NNJJx--i8CE 	F 	ii=DD
..$..0 1
jj%%&		# & r   )r<   r>   )NNr:   )r)   r*   r+   r,   r-   r   r~   rf   r`   r@   rM   rQ   rP   rd   rt   r.   r'   r   r   r5   r5   a   s;    C/, "6D0% +(Z.r   r5   )F)r-   r   r   apitools.base.pyr   !googlecloudsdk.api_lib.cloudbuildr   r   ra   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r	   r
   	six.movesr   _ERROR_FORMAT_STRINGr   r#   Errorr%   r0   r   objectr5   r'   r   r   <module>r      s    B   % = E 0 * # * 6 
;4*8z'' 8J,, 
Z%% 
Mv Mr   