ó
æ0ÌRc           @   sš  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 Z d d l Z d d l m	 Z	 d d l
 m Z m Z d d l m Z m Z d d l m Z d d l Z d d l m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m  Z  m! Z! d	 Z" e j# j$ e" d
 ƒ Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* d „  Z+ d „  Z, d „  Z- e. d k r–e j/ e- e j0 ƒ ƒ n  d S(   s   manage patches in a patch queueiÿÿÿÿN(   t   GbpOptionParserDebian(   t   GitRepositoryErrort   GitRepository(   t
   GitCommandt   CommandExecFailed(   t   GbpError(   t   PatchSeriest   Patch(
   t   is_pq_brancht   pq_branch_namet   pq_branch_baset   parse_gbp_commandst   format_patcht   switch_to_pq_brancht   apply_single_patcht   apply_and_commit_patcht   drop_pqt   get_maintainer_from_controls   debian/patches/t   seriesc         C   s4  t  j j d | | f ƒ g  } x6 | | g D]( } |  j | ƒ s- t d | ƒ ‚ q- q- Wt |  j | | ƒ ƒ } d } x¶ | D]® }	 |  j |	 ƒ }
 t |
 d d d ƒ } | j	 t |
 d d d ƒ ƒ d | k rd | k ræ | d n d } t
 | |  |
 | | j d	 | d | ƒq~ t  j j d
 |
 d ƒ q~ W| S(   s'   
    Generate patch files from git
    s$   Generating patches from git (%s..%s)s   %s not a valid tree-ishs   gbp-pq-topic:\s*(?P<topic>\S.*)t   gbpt   ignoret   topics   gbp-pqt    t   topic_regexs   Ignoring commit %st   id(   R   t   logt   infot   has_treeishR   t   reversedt   get_commitst   get_commit_infoR   t   updateR   t   patch_numbers(   t   repot   startt   endt   outdirt   optionst   patchest   treeisht   rev_listR   t   commitR   t   cmdsR   (    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyt   generate_patches+   s"    c   
   	   C   sO  t  | ƒ rH t | ƒ } t j j d | | f ƒ | } |  j | ƒ n  t | ƒ } y t j t	 ƒ WnO t
 k
 r¶ \ } } | t j k rŸ t d | ƒ ‚ q· t j j d t	 ƒ n Xt |  | | t	 | ƒ } | r7t t d ƒ 8 } x. | D]& }	 | j t j j |	 t	 ƒ d ƒ qî WWd QXt d ƒ d t	 g ƒ n t j j d	 | ƒ d S(
   s5   Export patches from the pq branch into a patch seriess   On '%s', switching to '%s's   Failed to remove patch dir: %ss   %s does not exist.t   ws   
Nt   statuss   --s#   No patches on '%s' - nothing to do.(   R   R
   R   R   R   t
   set_branchR	   t   shutilt   rmtreet	   PATCH_DIRt   OSErrort   errnot   ENOENTR   t   debugR+   t   opent   SERIES_FILEt   writet   ost   patht   relpathR   (
   R!   t   branchR%   t   baset	   pq_brancht   et   msgR&   t   seriesfdt   patch(    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyt   export_patchesF   s&    *c         C   sš   t  j j |  ƒ } t  j j |  ƒ } t j d d d d ƒ } t  j j | d ƒ } t  j j | | ƒ }  t j j	 d | | f ƒ t
 j | | ƒ | |  f S(   s·   
    Safe the current patches in a temporary directory
    below .git/

    @param series: path to series file
    @return: tmpdir and path to safed series file
    @rtype: tuple
    t   dirs   .git/t   prefixs   gbp-pqR&   s   Safeing patches '%s' in '%s'(   R9   R:   t   dirnamet   basenamet   tempfilet   mkdtempt   joinR   R   R5   R/   t   copytree(   R   t   srct   namet   tmpdirR&   (    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyt   safe_patchesb   s    
c         C   s…  d } t | ƒ r] | r@ t | ƒ } t | ƒ } |  j | ƒ qi t j j d | ƒ t ‚ n t | ƒ } |  j	 | ƒ r¡ | rŽ t
 |  | ƒ q¡ t d | ƒ ‚ n  t |  ƒ } |  j d | d t ƒ } t | ƒ d k rì t | ƒ \ } } n  t j | ƒ }	 t | ƒ }
 xM| D]9} t | ƒ d k rUt j j d |
 |
 d k rGd n d f ƒ n  y( t j j d	 | ƒ |  j | | ƒ Wn! t k
 r t d
 | ƒ ‚ n X|  j | ƒ xŒ |	 D]ƒ } t j j d | j ƒ y t |  | | | j ƒ Wqµt t f k
 r7t j j d | j ƒ |  j | ƒ |  j | ƒ PqµXqµWP|
 d 8}
 qWt d ƒ ‚ | rt j j d | ƒ t j | ƒ n  d S(   sã  
    apply a series of quilt patches in the series file 'series' to branch
    the patch-queue branch for 'branch'

    @param repo: git repository to work on
    @param branch: branch to base pqtch queue on
    @param series; series file to read patches from
    @param tries: try that many times to apply the patches going back one
                  commit in the branches history after each failure.
    @param force: import the patch series even if the branch already exists
    s5   Already on a patch-queue branch '%s' - doing nothing.s>   Patch queue branch '%s'. already exists. Try 'rebase' instead.t   numt   first_parenti   s
   %d %s leftt   triest   trys   Trying to apply patches at '%s's&   Cannot create patch-queue branch '%s'.s   Applying %ss   Failed to apply '%s's   Couldn't apply patchess    Remove temporary patch safe '%s'N(   t   NoneR   R
   R	   t   checkoutR   R   t   errR   t
   has_branchR   R   R   t   Truet   lenRO   R   t   read_series_fileR   t   create_branchR   R.   R5   R:   R   R   t   delete_branchR/   R0   (   R!   R<   R   RR   t   forceRN   R>   t
   maintainert   commitst   queuet   iR)   RB   (    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyt   import_quilt_patchesy   sX    	/	c         C   sE   t  | ƒ r t | ƒ } n t |  | ƒ | } t d ƒ | g ƒ d  S(   Nt   rebase(   R   R
   R   R   (   R!   R<   R=   (    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyt	   rebase_pqÂ   s
    c         C   sM   t  | ƒ r< t | ƒ } t j j d | ƒ |  j | ƒ n t |  | ƒ d S(   s=   Switch to patch-queue branch if on base branch and vice versas   Switching to %sN(   R   R
   R   R   R   RU   R   (   R!   t   currentR=   (    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyt	   switch_pqË   s
    c         C   s0  y/ t  d t j j |  d ƒ d d d d ƒ } Wn' t j k
 rX } t j j | ƒ d! SX| j
 d d d	 d
 ƒ | j d d d d d	 d d t d d ƒ| j d d	 d d d ƒ| j d d d	 d d d ƒ | j d d	 d d d d t d d ƒ| j d d d	 d d d ƒ | j d d d	 d  ƒ | j |  ƒ S("   Nt   commandi    RE   R   t   usages  %prog [options] action - maintain patches on a patch queue branch
Actions:
  export         export the patch queue associated to the current branch
                 into a quilt patch series in debian/patches/ and update the
                 series file.
  import         create a patch queue branch from quilt patches in debian/patches.
  rebase         switch to patch queue branch associated to the current
                 branch and rebase against current branch.
  drop           drop (delete) the patch queue associated to the current branch.
  apply          apply a patch
  switch         switch to patch-queue branch and vice versat   option_names   patch-numberst   destR    s   -vs	   --verboset   actiont
   store_truet   verboset   defaultt   helps   verbose command executions   --topicR   s3   in case of 'apply' topic (subdir) to put patch intos   time-machinet   time_machinet   typet   ints   --forceR]   s:   in case of import even import if the branch already existst   colort   tristates   color-schemet   color_scheme(   NN(   R    R9   R:   RG   t   ConfigParsert   ParsingErrorR   R   RV   RT   t   add_boolean_config_file_optiont
   add_optiont   Falset   add_config_file_optiont
   parse_args(   t   argvt   parserRV   (    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyR|   Õ   s"    "!c         C   sÄ  d } t  |  ƒ \ } } | s" d St j j | j | j | j ƒ t | ƒ d k  rg t j j d ƒ d S| d } | d d k r„ n_ | d d k rÇ t | ƒ d k rº t j j d ƒ d S| d } n t j j d | d ƒ d Sy t	 t
 j j ƒ } Wn2 t k
 r-t j j d t
 j j d ƒ ƒ d SXy5| j ƒ  } | d k r\t | | | ƒ n| d k rÎt } | j d k r†| j n d }	 t | | | |	 | j ƒ | j ƒ  } t j j d | | f ƒ n” | d k rêt | | ƒ nx | d k rt | | ƒ n\ | d
 k rFt | ƒ }
 t | ƒ } t | | |
 | | j ƒ n | d	 k rbt | | ƒ n  WnZ t k
 r|d } nD t t f k
 r¿} t | j ƒ  ƒ r¶t j j | ƒ n  d } n X| S(   Ni    i   i   s   No action given.t   exportt   importRc   t   dropt   switcht   applyi   s   No patch name given.s   Unknown action '%s'.s   %s is not a git repositoryt   .s'   Patches listed in '%s' imported on '%s'(   R   R€   s   rebaseR   R‚   (   s   apply(    R|   R   R   t   setupRs   Rm   Ru   RY   RV   R   R9   R:   t   curdirR   t   abspatht
   get_branchRC   R7   Rp   Rb   R]   R   R   Rd   R   R   R   R   Rf   R   R   t   __str__(   R}   t   retvalR%   t   argsRk   t	   patchfileR!   Re   R   RR   RB   R^   RV   (    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyt   mainô   sf    
 	
t   __main__(1   t   __doc__Rv   R3   R9   R/   t   sysRH   t   ret
   gbp.configR    t   gbp.gitR   R   t   gbp.command_wrappersR   R   t
   gbp.errorsR   t   gbp.logR   t   gbp.patch_seriesR   R   t   gbp.scripts.common.pqR   R	   R
   R   R   R   R   R   R   R   R1   R:   RJ   R7   R+   RC   RO   Rb   Rd   Rf   R|   R   t   __name__t   exitR}   (    (    (    s2   /usr/lib/python2.7/dist-packages/gbp/scripts/pq.pyt   <module>   s4   F				I			
		?