
    U                         d Z ddlZddlZddlmZ  G d de      Z G d de      Z G d d	e      Z G d
 de      Z	 G d de      Z
 G d de      Zy)zAccess to hooks.    N)	HookErrorc                       e Zd ZdZd Zy)HookzGeneric hook object.c                 ,    t        | j                        )zExecute the hook with the given args

        Args:
          args: argument list to hook
        Raises:
          HookError: hook execution failure
        Returns:
          a hook may return a useful value
        )NotImplementedErrorexecute)selfargss      lib/third_party/dulwich/hooks.pyr   zHook.execute"   s     "$,,//    N)__name__
__module____qualname____doc__r    r   r   r   r      s
    
0r   r   c                   $    e Zd ZdZ	 	 	 ddZd Zy)	ShellHookzHook by executable file

    Implements standard githooks(5) [0]:

    [0] http://www.kernel.org/pub/software/scm/git/docs/githooks.html
    Nc                 X    || _         || _        || _        || _        || _        || _        y)a  Setup shell hook definition

        Args:
          name: name of hook for error messages
          path: absolute path to executable file
          numparam: number of requirements parameters
          pre_exec_callback: closure for setup before execution
            Defaults to None. Takes in the variable argument list from the
            execute functions and returns a modified argument list for the
            shell hook.
          post_exec_callback: closure for cleanup after execution
            Defaults to None. Takes in a boolean for hook success and the
            modified argument list and returns the final hook return value
            if applicable
          cwd: working directory to switch to when executing the hook
        N)namefilepathnumparampre_exec_callbackpost_exec_callbackcwd)r	   r   pathr   r   r   r   s          r   __init__zShellHook.__init__7   s0    2 	 !2"4r   c                 T   t        |      | j                  k7  r/t        d| j                  | j                  t        |      |fz        | j                   | j                  | }	 t        j                  | j                  gt        |      z   | j                        }|dk7  r9| j                   | j                  dg|  t        d| j                  |fz        | j                   | j                  dg| S y# t        $ r$ | j                   | j                  dg|  Y yY yw xY w)z Execute the hook with given argszeHook %s executed with wrong number of args.                             Expected %d. Saw %d. args: %sNr   r   z&Hook %s exited with non-zero status %d   )lenr   r   r   r   
subprocesscallr   listr   r   OSError)r	   r
   rets      r   r   zShellHook.executeY   s1    t9%;99dmmSY=>  !!-)4))40D	2//4==/DJ">DHHMCax**6+D++A55<		3?OO  &&2.t..q8488 3 	2&&2'''1D1 3	2s   $BC: :(D'&D')NNNr   r   r   r   r   r   r   r   r   r   r   /   s      D2r   r   c                       e Zd ZdZd Zy)PreCommitShellHookzpre-commit shell hookc                 z    t         j                  j                  |dd      }t        j	                  | d|d|       y )Nhooksz
pre-commitr   r   osr   joinr   r   r	   
controldirr   s      r   r   zPreCommitShellHook.__init__x   s1    77<<
G\B4x
Kr   Nr   r   r   r   r   r   r   r   r(   r(   u   s    Lr   r(   c                       e Zd ZdZd Zy)PostCommitShellHookzpost-commit shell hookc                 z    t         j                  j                  |dd      }t        j	                  | d|d|       y )Nr*   zpost-commitr   r   r+   r.   s      r   r   zPostCommitShellHook.__init__   s1    77<<
G]C4!Lr   Nr0   r   r   r   r2   r2   ~   s     Mr   r2   c                       e Zd ZdZd Zy)CommitMsgShellHookzqcommit-msg shell hook

    Args:
      args[0]: commit message
    Returns:
      new commit message or None
    c           	          t         j                  j                  |dd      }d }d }t        j	                  | d|d|||       y )Nr*   z
commit-msgc                      dd l }|j                         \  }}t        j                  |d      5 }|j	                  | d          d d d        |fS # 1 sw Y   |fS xY w)Nr   wb)tempfilemkstempr,   fdopenwrite)r
   r9   fdr   fs        r   prepare_msgz0CommitMsgShellHook.__init__.<locals>.prepare_msg   sT    !))+JR2t$Q  % 7N % 7Ns   AAc                     | rBt        |d   d      5 }|j                         }d d d        t        j                  |d          S t        j                  |d          y # 1 sw Y   <xY w)Nr   rb)openreadr,   unlink)successr
   r>   new_msgs       r   	clean_msgz.CommitMsgShellHook.__init__.<locals>.clean_msg   sS    $q'4(AffhG )		$q'"IId1g	 )(s   AA'r   r+   )r	   r/   r   r?   rG   s        r   r   zCommitMsgShellHook.__init__   sA    77<<
G\B		 	,![)Z	
r   Nr0   r   r   r   r5   r5      s    
r   r5   c                       e Zd ZdZd Zd Zy)PostReceiveShellHookzpost-receive shell hookc                     || _         t        j                  j                  |dd      }t        j                  | d|d       y )Nr*   zpost-receiver   )r/   r,   r   r-   r   r   r.   s      r   r   zPostReceiveShellHook.__init__   s3    $77<<
G^D41=r   c                    t         j                  j                  | j                        sy 	 t         j                  j                         }| j                  |d<   t        j                  | j                  t        j                  t        j                  t        j                  |      }dj                  |D cg c]  }dj                  |       c}      }|j                  |      \  }}|j                  dk7  s|r/d}||j                  ||fz  }	t        |	j                  dd            |S c c}w # t        $ r}
t        t!        |
            d }
~
ww xY w)	NGIT_DIR)stdinstdoutstderrenv
 r   z0post-receive exit code: %d
stdout:
%s
stderr:
%szutf-8backslashreplace)r,   r   existsr   environcopyr/   r!   PopenPIPEr-   communicate
returncoder   decoder$   repr)r	   client_refsrP   prefin_dataout_dataerr_dataerr_fmterr_msgerrs              r   r   zPostReceiveShellHook.execute   s   ww~~dmm,	'**//#C!__C	N   oo!!A ii+ F+3#+ FGG!"w!7Hh!hU!Q\\8X$FFw8J KLLO !G  	'DI&&	's+   B
D0 6D+AD0 +D0 0	E9EENr&   r   r   r   rI   rI      s    !>
'r   rI   )r   r,   r!   dulwich.errorsr   objectr   r   r(   r2   r5   rI   r   r   r   <module>rh      se   *  	 
06 0 C2 C2LL LM) M 
  
F$'9 $'r   