ó
AqzRc           @   sQ  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z m Z d d l m Z d d l m Z m Z d d l m Z d d l Z d	 Z d
 „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z e d d d d „ Z d d „ Z  d „  Z! d „  Z" d d „ Z# d d „ Z$ d „  Z% d S(   s=   Common functionality for Debian and RPM patchqueue managementiÿÿÿÿN(   t   Message(   t   Header(   t   Charsett   QP(   t   GitRepositoryError(   t   GitModifiert   GitTz(   t   GbpErrors   patch-queue/c         C   s   t  t g |  j t ƒ S(   s   
    is branch a patch-queue branch?

    >>> is_pq_branch("foo")
    False
    >>> is_pq_branch("patch-queue/foo")
    True
    (   t   Falset   Truet
   startswitht   PQ_BRANCH_PREFIX(   t   branch(    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   is_pq_branch&   s    	c         C   s   t  |  ƒ s t |  Sd S(   sž   
    get the patch queue branch corresponding to branch

    >>> pq_branch_name("patch-queue/master")
    >>> pq_branch_name("foo")
    'patch-queue/foo'
    N(   R   R   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   pq_branch_name2   s    c         C   s   t  |  ƒ r |  t t ƒ Sd S(   sŸ   
    get the branch corresponding to the given patch queue branch

    >>> pq_branch_base("patch-queue/master")
    'master'
    >>> pq_branch_base("foo")
    N(   R   t   lenR   (   t	   pq_branch(    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   pq_branch_base>   s    c   	      C   s  t  j d | d t  j ƒ} i  } xæ |  d j ƒ  D]Ô } t  j | | ƒ } | r3 | j d ƒ j ƒ  } | r¾ | | k r¾ | j d ƒ r | j d ƒ | | <qt j j	 d | |  d f ƒ q| ræ | | k ræ | j d ƒ | | <qt j j	 d | |  d f ƒ q3 q3 W| S(	   s&   Parse gbp commands from commit messages+   ^%s:\s*(?P<cmd>[a-z-]+)(\s+(?P<args>\S.*))?t   flagst   bodyt   cmdt   argss=   Ignoring gbp-command '%s' in commit %s: missing cmd argumentst   ids-   Ignoring unknow gbp-command '%s' in commit %s(
   t   ret   compilet   It
   splitlinest   matcht   groupt   lowert   gbpt   logt   warn(	   t   infot   cmd_tagt
   noarg_cmdst   arg_cmdst   cmd_ret   commandst   lineR   R   (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   parse_gbp_commandsJ   s"    	c         C   sf   | rY g  } xS |  j  ƒ  D]9 } x0 | D]( } t j | | ƒ s& | j | ƒ q& q& Wq Wn	 d g } | S(   sM   
    Create patch include paths, i.e. a "negation" of the exclude paths.
    t   .(   t   valuesR   R   t   append(   t   file_statust   exclude_regext   include_pathst	   file_listt   fname(    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   patch_path_filtera   s    	c      	   C   sû  | s t  j j d |  ƒ d Sy³t |  d ƒ ž} t ƒ  } t d ƒ } d | _ t | _	 | d d } | d d } t
 j d | ƒ r• d | } n  t t | d ƒ | d	 d
 ƒ } | j t d | ƒ ƒ | | d <| d j }	 |	 j d ƒ }
 t t |
 d ƒ | d	 d ƒ | d <t t | d d ƒ | d	 d ƒ | d <| d r—| d j ƒ  d } y | j | j d ƒ ƒ Wq—t k
 r“| j | | ƒ q—Xn  | j | j d t ƒ ƒ | j d ƒ | j | ƒ Wd QXWn# t k
 rö} t d | ƒ ‚ n X|  S(   s   Write patch files   I won't generate empty diff %st   ws   utf-8t   authort   namet   emails   [,.@()\[\]\\:;]s   "%s"iM   t   froms   <%s>t   Froms   %a, %-d %b %Y %H:%M:%S %zt   datet   Datet   subjectt   SubjectR   s   
t   asciit   unixfroms   ---
Ns   Unable to create patch file: %s(   R   R   t   debugt   Nonet   openR    R   t   body_encodingR   t   header_encodingR   t   searchR   t   unicodeR+   t   datetimet   strftimet   rstript   set_payloadt   encodet   UnicodeDecodeErrort   writet	   as_stringR   t   IOErrorR   (   t   filenamet   commit_infot   difft   patcht   msgt   charsetR4   R5   t   from_headerR8   t   datestrR   t   err(    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   write_patch_fileq   sB    			
"
t    c      
   C   s#  | d } d }	 x | d j  ƒ  D]m }
 | r€ t j | |
 d t j ƒ} | r€ | j d ƒ } t j j d | | f ƒ q! q€ n  |	 |
 d 7}	 q! W|	 | d <t j	 j
 |  | ƒ }  t j	 j |  ƒ sÓ t j |  ƒ n  d t | ƒ d	 } d
 } d t | ƒ t | ƒ } | d |  } | r!| n d | | } t j	 j
 |  | ƒ } | | k rªd t | ƒ } | | t | ƒ  | } | r„| n d | | } t j	 j
 |  | ƒ } n  t | d | ƒ } d } | r| j d | d | d d d t d t ƒ} t | | | ƒ } | r| j | ƒ qn  | S(   s   Create patch of a single commitR   RX   R   R   t   topics   Topic %s found for %ss   
s   %04d-i   s   .patchi?   t	   patchnames   -%dt   filess   %s^!t   pathst   statiP   t   summaryt   textN(   R   R   R   R   R   R   R   R>   t   ost   patht   joint   existst   makedirsR   R1   R?   RP   R	   RW   R+   (   t   outdirt   repoRO   t   seriest   numberedt   topic_regext   path_exclude_regexRY   t   committ   mangled_bodyR'   R   t
   num_prefixt   suffixt   base_maxlent   baseRN   t   filepatht	   presuffixR\   RQ   RP   (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   format_patch   sD    
	
"	c         C   s  i | j  ƒ  d 6} t j j ƒ  j d t t j ƒ ƒ } | d j | ƒ d | | f | d <d | j | ƒ | | j | ƒ | f | d <| s¢ d | | f } n  t	 j
 j |  | ƒ } | j | | ƒ } t | | ƒ }	 |	 r| j | | d |	 d	 d
 d t d t ƒ}
 t | | |
 ƒ Sd S(   s5   Create a patch of diff between two repository objectsR3   t   tzinfos   Raw diff %s..%sR:   s%   Raw diff between %s '%s' and
%s '%s'
R   s   %s-to-%s.diffR\   R]   iP   R^   R_   N(   t   get_author_infoRE   t   nowt   replaceR   t   timet   timezonet   set_datet   get_obj_typeR`   Ra   Rb   t   diff_statusR1   RP   R	   RW   R?   (   Re   RN   Rf   t   startt   endRj   R!   Rv   R,   R\   RP   (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   format_diffÍ   s"    %!	c         C   s¯   t  j j |  j d d ƒ } d | } t j | d t d t j ƒj j ƒ  } t	 | ƒ d k r¨ | d j
 ƒ  } t j d | ƒ } | r¨ t | j d ƒ | j d	 ƒ ƒ Sn  t ƒ  S(
   s(   Get the maintainer from the control filet   debiant   controls*   sed -n -e "s/Maintainer: \+\(.*\)/\1/p" %st   shellt   stdouti    s!   (?P<name>.*[^ ]) *<(?P<email>.*)>R4   R5   (   R`   Ra   Rb   t
   subprocesst   PopenR	   t   PIPERƒ   t	   readlinesR   t   stripR   R   R   R   (   Rf   R   R   t   cmdoutt
   maintainert   m(    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   get_maintainer_from_controlä   s    
"c         C   sˆ   t  | ƒ r d St | ƒ } |  j | ƒ sc y |  j | ƒ Wqc t k
 r_ t d | ƒ ‚ qc Xn  t j j d | ƒ |  j	 | ƒ d S(   sb   
    Switch to patch-queue branch if not already there, create it if it
    doesn't exist yet
    Ns<   Cannot create patch-queue branch '%s'. Try 'rebase' instead.s   Switching to '%s'(
   R   R   t
   has_brancht   create_branchR   R   R   R   R!   t
   set_branch(   Rf   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   switch_to_pq_branchõ   s    c         C   s$   t  |  | ƒ t |  | | | ƒ d  S(   N(   R   t   apply_and_commit_patch(   Rf   R   RQ   t   fallback_authorRY   (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   apply_single_patch	  s    c   	      C   s6  i | j  d 6| j d 6| j d 6} t j j | j ƒ } | d oJ | d s¢ | r‹ | d r‹ | } t j j d | | d | d f ƒ q¢ t j j d | ƒ n  |  j	 | j d | j
 ƒ|  j ƒ  } d | j | j f } | rô | d | 7} n  |  j | | |  j g d	 | ƒ} |  j d
 | d d | j ƒd S(   s=   apply a single patch 'patch', add topic 'topic' and commit itR4   R5   R8   s9   Patch '%s' has no authorship information, using '%s <%s>'s(   Patch '%s' has no authorship informationRˆ   s   %s

%ss   
Gbp-Pq-Topic: %sR3   t   HEADRR   s   gbp-pq import %sN(   R3   R5   R8   R`   Ra   t   basenameR   R   R    t   apply_patchRˆ   t
   write_treeR:   t	   long_desct   commit_treet   headt
   update_ref(	   Rf   RQ   R’   RY   R3   t   patch_fnt   treeRR   Rk   (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyR‘     s$    

!c         C   sx   t  | ƒ r% t j j d ƒ t ‚ n t | ƒ } |  j | ƒ rd |  j | ƒ t j j d | ƒ n t j j d ƒ d  S(   Ns'   On a patch-queue branch, can't drop it.s   Dropped branch '%s'.s,   No patch queue branch found - doing nothing.(	   R   R   R   RV   R   R   R   t   delete_branchR!   (   Rf   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   drop_pq'  s    	(&   t   __doc__R   R`   R„   RE   Rx   t   email.messageR    t   email.headerR   t   email.charsetR   R   t   gbp.gitR   t   gbp.git.modifierR   R   t
   gbp.errorsR   t   gbp.logR   R   R   R   R   R(   R?   R1   RW   R	   Rs   R   RŒ   R   R“   R‘   RŸ   (    (    (    s9   /usr/lib/python2.7/dist-packages/gbp/scripts/common/pq.pyt   <module>   s6   					,/		