
                         n    d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ  G d	 d
e      Zy)zIContext manager to help with Control-C handling during critical commands.    )absolute_import)division)unicode_literalsN	exit_code)
exceptions)logc                   (    e Zd ZdZd Zd Zd Zd Zy)CancellableTestSectionzCancel a test matrix if CTRL-C is typed during a section of code.

  While within this context manager, the CTRL-C signal is caught and a test
  matrix is cancelled. This should only be used with a section of code where
  the test matrix is running.
  c                 .    d | _         d | _        || _        y N)_old_sigint_handler_old_sigterm_handler_matrix_monitor)selfmatrix_monitors     :lib/googlecloudsdk/api_lib/firebase/test/ctrl_c_handler.py__init__zCancellableTestSection.__init__%   s    #D $D)D    c                 ^   t        j                  t         j                        | _        t        j                  t         j                        | _        t        j                   t         j                  | j                         t        j                   t         j                  | j                         | S r   )signal	getsignalSIGINTr   SIGTERMr   _Handler)r   s    r   	__enter__z CancellableTestSection.__enter__*   sa    %//>D & 0 0 @D
MM&--/
MM&..$--0Kr   c                     t        j                   t         j                  | j                         t        j                   t         j                  | j                         y)NF)r   r   r   r   r   )r   typvalue	tracebacks       r   __exit__zCancellableTestSection.__exit__1   s5    
MM&--!9!9:
MM&..$";";<r   c                 B   t         j                  j                  dj                  | j                  j
                               | j                  j                          t         j                  j                  d       t        j                  t        j                        )Nz

Cancelling test [{id}]...

)idz!
Test matrix has been cancelled.
r   )r	   statuswriteformatr   	matrix_idCancelTestMatrixr   ExitCodeNoErrorr   MATRIX_CANCELLED)r   unused_signalunused_frames      r   r   zCancellableTestSection._Handler6   sl    JJ8f 4 4 > >f?A))+JJ:;

$
$y/I/I
JJr   N)__name__
__module____qualname____doc__r   r   r!   r    r   r   r   r      s    *

Kr   r   )r0   
__future__r   r   r   r   $googlecloudsdk.api_lib.firebase.testr   googlecloudsdk.callioper   googlecloudsdk.corer	   objectr   r1   r   r   <module>r7      s.     P &  '  : . #KV Kr   