
AqzRc           @   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 j Z	 d d l
 j Z d d l Z d d l m Z m Z d d l m 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 m Z i  Z e j d	  Z  d
   Z! d   Z" d   Z# d   Z$ d d  Z% d   Z& d   Z' e( d  Z) d   Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ d   Z0 e1 d k re j2 e0 e j3   n  d S(   s:   Generate Debian changelog entries from git commit messagesiN(   t   GbpOptionParserDebiant   GbpOptionGroup(   t   GbpError(   t   compare_versions(   t   DebianSourcet   DebianSourceError(   t   GitRepositoryErrort   DebianGitRepository(   t	   ChangeLogt   NoChangeLogErrors4   \s*\*\* SNAPSHOT build @(?P<commit>[a-z0-9]+)\s+\*\*c         C   st   yL |  j  | d | j } t j j d |  t | | j  d k rK | SWn! t k
 ro t j j d  n Xd S(   sN   
    Guess the version based on the latest version on the upstream branch
    t   epochs   Found upstream version %s.i    s   No upstream tag foundN(	   t   debian_version_from_upstreamR
   t   gbpt   logt   debugR   t   versionR   t   None(   t   repot   upstream_tag_formatt   cpR   (    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   guess_version_from_upstream'   s    c         C   sk   d } } | ra y |  j d  } Wn t k
 r6 n Xy |  j d  } Wqa t k
 r] qa Xn  | | f S(   s+   Get author and email from git configurations	   user.names
   user.emailN(   R   t
   get_configt   KeyError(   R   t   use_git_configt   authort   email(    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   get_author_email6   s    
    c   
   
   C   s   t  |  |  \ } } d d g } g  } | j s xj | D]O } t | |  }	 |	 r7 t j j d | |	 f  | j d | |	 f  q7 q7 Wn t j j d  t j d d d | d	 | d
 | |  d S(   s   
    Fixup the changelog header and trailer's comitter and email address

    It might otherwise point to the last git committer instead of the person
    creating the changelog
    This apply --distribution and --urgency options passed to git-dch
    t   distributiont   urgencys   Set header option '%s' to '%s's   --%s=%ss0   Snapshot enabled: do not fixup options in headert   msgt    R   R   t   dch_optionsN(	   R   t   snapshott   getattrR   R   R   t   appendR   t	   spawn_dch(
   R   t
   git_authort   optionsR   R   R   t   used_optionst   header_optst   optt   val(    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   fixup_sectionC   s    	!c         C   s   yX |  j  d d  \ } } | j d d  \ } } | j d  sK t  n t |  } Wn t k
 rw |  } d } n X| | f S(   s*  
    Get the current release and snapshot version.

    Format is <debian-version>~<release>.gbp<short-commit-id>

    >>> snapshot_version('1.0-1')
    ('1.0-1', 0)
    >>> snapshot_version('1.0-1~1.test0')
    ('1.0-1~1.test0', 0)
    >>> snapshot_version('1.0-1~2.gbp1234')
    ('1.0-1', 2)
    t   ~i   t   .R   i    (   t   rsplitt   splitt
   startswitht
   ValueErrort   int(   R   t   releaset   suffixR    t   commit(    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   snapshot_version]   s    	
R   c         C   s  y d |  | f } t  | d  } t  |  d  } | d | IJ| j   | j   | j   } t j |  r | j   d } n  | r | d | IJn  | r | | j   IJn  t j | |  | j   | j   t j	 |   t j
 | |   Wn# t k
 r} t d |   n Xd S(	   s   
    Mangle changelog to either add or remove snapshot markers

    @param snapshot: SHA1 if snapshot header should be added/maintained,
        empty if it should be removed
    @type  snapshot: C{str}
    s   %s.%st   wt   rsF   %(Source)s (%(MangledVersion)s) %(Distribution)s; urgency=%(urgency)s
R   s     ** SNAPSHOT build @%s **
s   Error mangling changelog %sN(   t   opent   readlinet   snapshot_ret   matcht   rstript   shutilt   copyfileobjt   closet   ost   unlinkt   renamet   OSErrorR   (   t	   changelogR   R    t   tmpfilet   cwt   crt   linet   e(    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   mangle_changelogw   s.    


	

c   	   	   C   sq   t  | |  \ } } t | d  \ } } | rK | | d <t |  |  n  | j d t d | d | d |  d S(   s3   Remove the snapshot header and set the distributiont   Versiont   MangledVersionR2   R   R   R   N(   R   R5   RJ   R#   t   True(	   RD   R   R   R$   R   R   R   R2   R    (    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt
   do_release   s    
c         C   s   | j  } t d |   } t | d  \ } } t t |   } d | d j | d d ! f } d | | f | d <t |  | |  | | f S(	   s   
    Add new snapshot banner to most recent changelog section.
    The next snapshot number is calculated by eval()'ing next_snapshot.
    t   filenameRK   s   %d.gbp%sR   i    i   s   %s~%sRL   (   t   headR   R5   R1   t   evalt   joinRJ   (   RD   R   t   next_snapshotR4   R   R2   R    R3   (    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   do_snapshot   s    	 c   	      C   so   |  j  |  } | d j } | d j } t j d  } | sJ t j } n  | | | d | } | | | f f S(   s;   Parse a commit and return message, author, and author emailR   t   format_changelog_entryt   last_commit(   t   get_commit_infot   nameR   t   user_customizationst   gett   dchRU   (	   R   t   commitidt   optsRV   t   commit_infoR   R   t   format_entryt   entry(    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   parse_commit   s    c         C   s   t  j t |  d  } | r) | j d  S| j | |  j  } | r\ t j j d |  | S| j	 d d d d  } | r t j j d | d	  | d	 Sd
 S(   s  
    Guess the last commit documented in the changelog from the snapshot banner,
    the last tagged version or the last point the changelog was touched.

    @param cp: the changelog
    @param repo: the git repository
    @param tagformat: the format for Debian tags
    @returns: the commit that was last documented in the changelog
    @rtype: C{str}
    @raises GbpError: In case we fail to find a commit to start at
    t   ChangesR4   s,   Found tag for topmost changelog version '%s't   pathss   debian/changelogt   numi   s   Changelog last touched at '%s'i    N(   t   ret   searchR:   t   groupt   find_versionR   R   R   t   infot   get_commitsR   (   R   R   t	   tagformatt   srR4   t   last(    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   guess_documented_commit   s    c         C   s$   t  j t |  d  } | r  t St S(   s+   Whether the changelog has a snapshot bannerRb   (   Re   Rf   R:   RM   t   False(   R   Rl   (    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   has_snapshot_banner   s    c         C   s   |  r t  |  t t  n  d  S(   N(   t   execfileRY   (   t   customization_file(    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   get_customizations   s    c         C   s   |  j  r" |  j r" | j d  n  |  j rD |  j rD | j d  n  g  } |  j rc | j d  n | j d  |  j r | j d  n | j d  |  j r | j d  n  t	 |  j
  | S(   Ns5   '--snapshot' and '--release' are incompatible optionss/   '--since' and '--auto' are incompatible optionss   --multimaint-merges   --nomultimaint-merges   --multimaints   --nomultimaints   --force-distribution(   R    R2   t   errort   sincet   autot   multimaint_mergeR"   t
   multimaintt   force_distributionRs   Rr   (   R%   t   parserR   (    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   process_options   s    			c         C   sV   d g } |  j  r" | j d  n |  j r; | j d  n  |  j | k rN d Sd Sd S(   s-   Determine text editor and check if we need itt   alwaysR    R2   s   sensible-editorN(   R    R"   R2   t   spawn_editorR   (   R%   t   states(    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   process_editor_option  s    			c         C   s   |  j  t d |  S(   NR   (   t
   commit_msgt   dict(   R%   R   (    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   changelog_commit_msg  s    c   #      C   s
  d } d } d } t  } i  } d  } y/ t d t j j |  d  d d d d  } Wn' t j k
 r| } t j	 j
 |  d	 SXt | d
 d  }	 t | d d  }
 t | d d  } t | d d  } t | d d  } | j |	  | j |
  | j |  | j |  | j |  | j d d d d  | j d d d d  | j d d d d  | j d d d d  | j d d d d d  d!  | j d d" d d# d  d$  | j d% d& d' d( d d) d* t  d  d+ | j d d, d d, d- d.  | j d d/ d d0  |	 j d1 d2 d d3 d  d4 |	 j d5 d6 d' d( d d7 d* t  d  d8 |
 j d9 d: d' d( d d; d* t  d  d< |
 j d= d> d' d( d d? d* t  d  d@ |
 j dA dB d dC d  dD |
 j dE d' d( d dF d* t  d  dG |
 j dH dI d dJ d  dK |
 j dL dM d dN d  dO |
 j dP d dQ d' d( d* t  d  dR |
 j dS d dT d' d( d* t  d  dU |
 j dV d dW d' d( d* t  d  dX |
 j dY d dZ d' d( d* t  d  d[ |
 j d\ d d] d' d( d* t  d  d^ |
 j d d_ d d`  | j d da d da  | j d db d dc d  dd  | j d de d de  | j d df d dg d  dh d- di dj dk  | j d dl d dm d  dn  | j d do d do  | j d dp d dq  | j d dr d ds  | j d dt d du  | j dv dw d' d( d dx d* t  d  dy dz } | j d d{ d d| d  |  | j |  d	  \ } } t j	 j | j | j | j  t | |  } t |  } y!y t d}  } Wn- t k
 rt d~ t j j d}    n Xy | j   } Wn  t k
 r| j s  qn X| j | k r| j rt j	 j
 d | j | f  t d   n  t d}  } | j } | j  rD| j  } n d } | j! rt" | | | j#  } | r{d | } n d } t j	 j$ |  t% |  } n6 | j& | j# | d  } | st d | d   n  | rt j	 j$ d d j' |   n  | j( d3 | d | d | d | j) j* d   } | j+   | j, sl| j- sl| j. sl| j/ sl| j0 sl| j1 r| j- rdP | d <ne | j. rdS | d <nO | j/ rdV | d <n9 | j0 rdY | d <n# | j1 rd\ | d <n | j, | d <t2 } nE | d d k r| r| rt2 } n | j3 r/| r/t2 } n t  } | rz| rz| j4   rzt5 | | j6 |  } | rz| | d <qzn  d } x | D] } | d	 7} t7 | | | d | t8 |  k } | \ } \ } }  | sqn  | r	| j9 dC d d | d | d | d |  d |  t  } q| j: | | |  |  qW| sS	t j	 j$ d | | f  n  | r	| j9 dC d d d g d | d |  n  t; | d` | j< d | d | | j= r	t> | | | d` | j< d | nA | j3 r
t? | | | j@  \ }! }" t j	 j$ d |! |" f  n  | r+
tA jB | d g    n  | jC r
tD d |  jE }" tF | |"  } | jG | g |  t j	 j$ d |"  n  WnP tA jH t t tI tJ f k
 r
} t8 | jK    r
t j	 j
 |  n  d	 } n X| S(   Ni    s   debian/changelogt   HEADt   commandt   prefixR   t   usages   %prog [options] pathsi   s   commit range optionss%   which commits to add to the changelogs    release & version number optionss&   what version number and release to uses   commit message formattings"   howto format the changelog entriess   branch and tag namings   branch names and tag formatst   customizations   options for customizationt   option_names   ignore-brancht   destt   ignore_branchs   debian-brancht   debian_branchs   upstream-tagt   upstream_tags
   debian-tagt
   debian_tags   snapshot-numbert   snapshot_numbert   helpsR   expression to determine the next snapshot number, default is '%(snapshot-number)s's   git-logt   git_logs4   options to pass to git-log, default is '%(git-log)s's   -vs	   --verboset   actiont
   store_truet   verboset   defaults   verbose command executiont   colort   typet   tristates   color-schemet   color_schemes   -ss   --sinceRu   s1   commit to start from (e.g. HEAD^^^, debian/0.4.3)s   -as   --autoRv   s0   autocomplete changelog from last snapshot or tags   -Rs	   --releaseR2   s   mark as releases   -Ss
   --snapshotR    s   mark as snapshot builds   -Ds   --distributionR   s   Set distributions   --force-distributionRy   sd   Force the provided distribution to be used, even if it doesn't match the list of known distributionss   -Ns   --new-versiont   new_versions+   use this as base for the new version numbers   -Us	   --urgencyR   s   Set urgency levels   --bpot   bposl   Increment the Debian release number for an upload to backports, and add a backport upload changelog comment.s   --nmut   nmus?   Increment the Debian release number for a non-maintainer uploads   --qat   qasg   Increment the Debian release number for a Debian QA Team upload, and add a QA upload changelog comment.s   --teamt   teamsf   Increment the Debian release number for a Debian Team upload, and add a Team upload changelog comment.s
   --securityt   securitysf   Increment the Debian release number for a security upload and add a security upload changelog comment.s
   git-authorR$   t   metas   meta-closest   meta_closessB   Meta tags for the bts close commands, default is '%(meta-closes)s't   fulls	   id-lengtht   idlensT   include N digits of the commit id in the changelog entry, default is '%(id-length)s'R1   t   metavart   Ns   ignore-regext   ignore_regexsA   Ignore commit lines matching regex, default is '%(ignore-regex)s'Rx   s   multimaint-mergeRw   s   spawn-editorR}   s
   commit-msgR   s   -cs   --commitR4   s&   commit changelog file after generatings   Load Python code from CUSTOMIZATION_FILE.  At the moment, the only useful thing the code can do is define a custom format_changelog_entry() function.t   customizationsRr   R,   s   %s is not a git repositorys&   You are not on branch '%s' but on '%s'sH   Use --ignore-branch to ignore or --debian-branch to set the branch name.s   Continuing from commit '%s's   Starting from first commitRK   s   Version %s not founds    Only looking for changes on '%s't    t   untilRc   R%   t	   incrementR   t   Distributiont
   UNRELEASEDRV   R   R   R   R   s"   No changes detected from %s to %s.s2   Changelog has been prepared for snapshot #%d at %sRO   s+   Changelog has been committed for version %s(L   Ro   R   R    R@   t   patht   basenamet   ConfigParsert   ParsingErrorR   R   t   errR   t   add_option_groupt   add_boolean_config_file_optiont   add_config_file_optiont
   add_optiont
   parse_argst   setupR   R   R   R{   R   R   R   R   t   abspatht
   get_branchR   R   R   RD   Ru   Rv   Rn   R   Ri   Rp   Rh   RR   Rj   R   R.   t   reverseR   R   R   R   R   R   RM   R    t	   is_nativeR   R   Ra   t   lent   add_sectiont	   add_entryR*   R$   R2   RN   RT   R   t   gbpct   CommandR4   R   R   R   t   commit_filest   CommandExecFailedR   R	   t   __str__(#   t   argvt   retRD   R   t   found_snapshot_bannert   version_changet   branchRz   R   t   range_groupt   version_groupt   commit_groupt   naming_groupt   custom_groupt   help_msgR%   t   argsR   t
   editor_cmdR   t   sourceR   Ru   R   t   commitsR   t   vt   it   ct   parsedR   t   commit_authort   commit_emailt   snapR   (    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   main  s|   "										!!!!! 	
			 
$								
	
	
		
t   __main__(4   t   __doc__R   t   os.pathR@   Re   t   sysR=   t   gbp.command_wrapperst   command_wrappersR   t   gbp.dchR[   t   gbp.logR   t
   gbp.configR    R   t
   gbp.errorsR   t   gbp.debR   t   gbp.deb.sourceR   R   t   gbp.deb.gitR   R   t   gbp.deb.changelogR   R	   RY   t   compileR:   R   R   R*   R5   RJ   RN   RT   Ro   Ra   Rn   Rp   Rs   R{   R   R   R   t   __name__t   exitR   (    (    (    s3   /usr/lib/python2.7/dist-packages/gbp/scripts/dch.pyt   <module>   sB   				%	
		"						