ó
6qzRc           @   s;  d  Z  d d l Z d d l Z d d l Z d d l Z d d l j Z d e f d „  ƒ  YZ d e	 f d „  ƒ  YZ
 d e
 f d „  ƒ  YZ d	 e
 f d
 „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d S(   s`   
Simple class wrappers for the various external commands needed by
git-buildpackage and friends
iÿÿÿÿNt   CommandExecFailedc           B   s   e  Z d  Z RS(   s%   Exception raised by the Command class(   t   __name__t
   __module__t   __doc__(    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR       s   t   Commandc           B   sS   e  Z d  Z g  e d d e d „ Z d „  Z e d „ Z g  e d „ Z d „  Z	 RS(   s‡   
    Wraps a shell command, so we don't have to store any kind of command
    line options in one of the git-buildpackage commands
    c         C   s    | |  _  | |  _ d d j |  j  g |  j ƒ |  _ | |  _ d |  _ d |  _ | |  _ | |  _ | d  k	 r“ t
 j j ƒ  |  _ |  j j | ƒ n	 d  |  _ d  S(   Ns   Couldn't run '%s't    i   t    (   t   cmdt   argst   joint	   run_errort   shellt   retcodet   stderrt   capture_stderrt   cwdt   Nonet   ost   environt   copyt   envt   update(   t   selfR   R   R   t	   extra_envR   R   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyt   __init__'   s    							c         C   sÔ   d „  } t  j d |  j |  j | f ƒ d |  _ |  j rD t j n d
 } |  j g |  j | } |  j	 r| d j
 | ƒ } n  t j | d |  j d |  j	 d |  j d | d	 | ƒ} | j ƒ  \ } } | |  _ | j S(   sf   
        Wraps subprocess.call so we can be verbose and fix python's
        SIGPIPE handling
        c           S   s   t  j  t  j t  j ƒ d S(   sA   Restore default signal handler (http://bugs.python.org/issue1652)N(   t   signalt   SIGPIPEt   SIG_DFL(    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyt   default_sigpipe=   s    s   %s %s %sR   R   R   R   R   t
   preexec_fnR   N(   t   logt   debugR   R   R   R   t
   subprocesst   PIPER   R   R	   t   PopenR   R   t   communicatet
   returncode(   R   R   R   t
   stderr_argR   t   popent   dummyR   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyt   __call8   s     	 							c         C   sµ   yX |  j  | ƒ } | d k  r5 d |  j | f } n" | d k rW d |  j | f } n  Wn# t k
 r} } d | } d } n X| r¨ | r¨ t j d |  j | f ƒ n  | |  _ | S(   sÿ   
        run self.cmd adding args as additional arguments

        @param quiet: don't log errors to stderr Mostly useful during unit testing.

        Be verbose about errors and encode them in the return value, don't pass
        on exceptions.
        i    s   %s was terminated by signal %ds   %s returned %ds   Execution failed: %si   s   %s: %s(   t   _Command__callR   t   OSErrorR   t   errR
   R   (   R   R   t   quietR   t
   err_detailR+   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyt   __runR   s    		

	c         C   s   |  j  | | ƒ r t ‚ n  d S(   s.  
        Run the command and convert all errors into CommandExecFailed. Assumes
        that the lower levels printed an error message (the command itself and
        also via our logging api) - only useful if you only expect 0 as result.

        @param quiet: don't log failed execution to stderr. Mostly useful during
            unit testing
        @type quiet: C{bool}

        >>> Command("/bin/true")(["foo", "bar"])
        >>> Command("/foo/bar")(quiet=True)
        Traceback (most recent call last):
        ...
        CommandExecFailed
        N(   t   _Command__runR    (   R   R   R,   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyt   __call__j   s    c         C   s=   y |  j  | ƒ } Wn# t k
 r8 } t d | ƒ ‚ n X| S(   so  
        Like __call__ but let the caller handle the return status and don't
        use the logging api for errors.

        >>> Command("/bin/true").call(["foo", "bar"])
        0
        >>> Command("/foo/bar").call(["foo", "bar"]) # doctest:+ELLIPSIS
        Traceback (most recent call last):
        ...
        CommandExecFailed: Execution failed: ...
        s   Execution failed: %s(   R)   R*   R    (   R   R   t   retR+   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyt   call}   s
    N(
   R   R   R   t   FalseR   R   R)   R/   R0   R2   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   !   s   	t   RunAtCommandc           B   s   e  Z d  Z d d „ Z RS(   s%   Run a command in a specific directoryt   .c         G   st   t  j j t  j j ƒ } y4 t  j | ƒ t j |  t | Œ  ƒ t  j | ƒ Wn! t k
 ro t  j | ƒ ‚  n Xd  S(   N(	   R   t   patht   abspatht   curdirt   chdirR   R0   t   listt	   Exception(   R   t   dirR   R8   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR0   ’   s    (   R   R   R   R0   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR4      s   t   UnpackTarArchivec           B   s   e  Z d  Z g  d d „ Z RS(   s+   Wrap tar to unpack a compressed tar archivec      	   C   sx   | |  _  | |  _ g  | D] } d | ^ q } | s> d } n  t j |  d | d | | d | g ƒ d |  j  |  _ d  S(   Ns   --exclude=%ss   -at   tars   -Cs   -xfs   Couldn't unpack "%s"(   t   archiveR<   R   R   R
   (   R   R?   R<   t   filterst   compressiont   _filtert   exclude(    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   Ÿ   s    			N(   R   R   R   R   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR=      s   t   PackTarArchivec           B   s   e  Z d  Z g  d d „ Z RS(   s)   Wrap tar to pack a compressed tar archivec      
   C   s{   | |  _  | |  _ g  | D] } d | ^ q } | s> d } n  t j |  d | d | | d | | g ƒ d |  j  |  _ d  S(   Ns   --exclude=%ss   -aR>   s   -Cs   -cfs   Couldn't repack "%s"(   R?   R<   R   R   R
   (   R   R?   R<   t   destR@   RA   RB   RC   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   ®   s    			N(   R   R   R   R   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyRD   ¬   s   t   CatenateTarArchivec           B   s    e  Z d  Z d „  Z d „  Z RS(   s-   Wrap tar to catenate a tar file with the nextc         K   s,   | |  _  t j |  d d d | g |  d  S(   NR>   s   -As   -f(   R?   R   R   (   R   R?   t   kwargs(    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   ½   s    	c         C   s   t  j |  | g ƒ d  S(   N(   R   R0   (   R   t   target(    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR0   Á   s    (   R   R   R   R   R0   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyRF   »   s   	t
   RemoveTreec           B   s   e  Z d  Z d „  Z RS(   s(   Wrap rm to remove a whole directory treec         C   s6   | |  _  t j |  d d | g ƒ d |  j  |  _ d  S(   Nt   rms   -rfs   Couldn't remove "%s"(   t   treeR   R   R
   (   R   RK   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   Ç   s    	(   R   R   R   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyRI   Å   s   t   Dchc           B   s   e  Z d  Z d „  Z RS(   s#   Wrap dch and set a specific versionc         C   sB   d | g } | r" | j  | ƒ n  t j |  d | ƒ d |  _ d  S(   Ns   -vt   dchs   Dch failed.(   t   appendR   R   R
   (   R   t   versiont   msgR   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   Ï   s
    (   R   R   R   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyRL   Í   s   t   DpkgSourceExtractc           B   s    e  Z d  Z d „  Z d „  Z RS(   sf   
    Wrap dpkg-source to extract a Debian source package into a certain
    directory, this needs
    c         C   s   t  j |  d d g ƒ d  S(   Ns   dpkg-sources   -x(   R   R   (   R   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   Ü   s    c         C   s'   d | |  _  t j |  | | g ƒ d  S(   Ns   Couldn't extract "%s"(   R
   R   R0   (   R   t   dsct
   output_dir(    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR0   ß   s    (   R   R   R   R   R0   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyRQ   ×   s   	t   UnpackZipArchivec           B   s   e  Z d  Z d „  Z RS(   s   Wrap zip to Unpack a zip filec         C   sE   | |  _  | |  _ t j |  d d | d | g ƒ d |  j  |  _ d  S(   Nt   unzips   -qs   -ds   Couldn't unpack "%s"(   R?   R<   R   R   R
   (   R   R?   R<   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   æ   s    		(   R   R   R   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyRT   ä   s   t
   GitCommandc           B   s   e  Z d  Z g  d „ Z RS(   s!   Mother/Father of all git commandsc         K   s.   t  j |  d | g | |  d | |  _ d  S(   Nt   gits   Couldn't run git %s(   R   R   R
   (   R   R   R   RG   (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyR   ð   s    (   R   R   R   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyRV   î   s   (   R   R    R   t   os.pathR   t   gbp.logR   R;   R    t   objectR   R4   R=   RD   RF   RI   RL   RQ   RT   RV   (    (    (    s8   /usr/lib/python2.7/dist-packages/gbp/command_wrappers.pyt   <module>   s    o


