ó
X¾÷Xc           @  sg  d  d l  m Z d  d l Z d  d l m 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 e e d d „ Z d „  Z d „  Z d	 „  Z d
 „  Z d d d „ Z d „  Z d „  Z d „  Z d d „ Z d „  Z d d „ Z d „  Z d „  Z d e j  j! d d d „ Z" d d e# ƒ  d „ Z$ d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   log(   t   options(   t   versionc   	      C  sö   | r t  j } t  j } n d  } d  } t j j ƒ  } | rL | j | ƒ n  t  j |  d | d | d | ƒ} | j ƒ  } | j	 rª | rª t
 j j d |  | j	 f ƒ ‚ n  t | ƒ d k sØ | d sØ | d j ƒ  rÜ d S| d j ƒ  j d d ƒ S(	   Nt   stdoutt   stderrt   envu   %s returned %di    u    u   utf-8u   replace(   t
   subprocesst   PIPEt   Nonet   ost   environt   copyt   updatet   Popent   communicatet
   returncodet	   distutilst   errorst   DistutilsErrort   lent   stript   decode(	   t   cmdt   throw_on_errort   bufferR   t   out_locationt   err_locationt   newenvt   outputt   out(    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _run_shell_command"   s&    			.c         K  s<   t  |  t t f ƒ s! |  g }  n  t d d | g |  |  S(   Nu   gitu   --git-dir=%s(   t
   isinstancet   listt   tupleR   (   R   t   git_dirt   kwargs(    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _run_git_command=   s    c          C  sJ   y t  d d d g ƒ SWn, t k
 rE }  |  j t j k r? d S‚  n Xd  S(   Nu   gitu	   rev-parseu	   --git-diru    (   R   t   OSErrort   errnot   ENOENT(   t   e(    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _get_git_directoryD   s    c           C  s-   y t  d d g ƒ Wn t k
 r( t SXt S(   Nu   gitu	   --version(   R   R&   t   Falset   True(    (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _git_is_installedN   s
    c         C  s   t  |  d t j ƒS(   u¦   Find the highest tag from a list.

    Pass in a list of tag strings and this will return the highest
    (latest) as sorted by the pkg_resources version parser.
    t   key(   t   maxt   pkg_resourcest   parse_version(   t   tags(    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _get_highest_tagY   s    u    c         C  s~   g  } | d k r t ƒ  } n  | ra t j d ƒ t d d g | ƒ } | j d j d ƒ ƒ } n  g  | D] } | rh | ^ qh S(   uÈ   Behave like a file finder entrypoint plugin.

    We don't actually use the entrypoints system for this because it runs
    at absurd times. We only want to do this when we are building an sdist.
    u2   [pbr] In git context, generating filelist from gitu   ls-filesu   -zt    u   utf-8N(   R	   t   _run_git_functionsR   t   infoR%   t   splitR   (   t   dirnameR#   t	   file_listt   f(    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _find_git_filesb   s    c         C  sI   t  d d g |  ƒ } d | k r5 | j d d ƒ d Sd | k rE d Sd  S(   Nu   describeu   --alwaysu   -i   iþÿÿÿu   .i    (   R%   t   rsplitR	   (   R#   t   describe(    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _get_raw_tag_infot   s    c         C  s   t  |  ƒ d k S(   Ni    (   R>   (   R#   (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   get_is_release}   s    c          C  s%   d  }  t ƒ  r t ƒ  }  n  |  p$ d  S(   N(   R	   R-   R*   (   R#   (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyR5      s    	c         C  s2   |  s t  ƒ  }  n  |  r. t d d d g |  ƒ Sd S(   u1   Return the short sha for this repo, if it exists.u   logu   -n1u   --pretty=format:%hN(   R5   R%   R	   (   R#   (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   get_git_short_shaˆ   s    c         c  sØ   t  } d } | d f Vxº |  D]² \ } } } | r„ t | ƒ } t | ƒ d } | sc | d f Vn  | d t d | d | ƒ f Vn  | j d ƒ sÊ | j d ƒ r¯ | d	  } n  | d
 t d | ƒ f Vn  t } q Wd S(   uç   Convert a oneline log iterator to formatted strings.

    :param changelog: An iterator of one line log entries like
        that given by _iter_log_oneline.
    :return: An iterator over (release, formatted changelog) tuples.
    u   CHANGES
=======

u   -u   
u   %(tag)s
%(underline)s

t   tagt	   underlineu   Merge u   .iÿÿÿÿu
   * %(msg)s
t   msgN(   R,   R	   R3   R   t   dictt
   startswitht   endswithR+   (   t	   changelogt
   first_linet   current_releaset   hashR2   RC   RB   (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _iter_changelog’   s"    c         C  s,   |  d k r t ƒ  }  n  |  s" g  St |  ƒ S(   uQ  Iterate over --oneline log entries if possible.

    This parses the output into a structured form but does not apply
    presentation logic to the output - making it suitable for different
    uses.

    :return: An iterator of (hash, tags_set, 1st_line) tuples, or None if
        changelog generation is disabled / not available.
    N(   R	   R*   t   _iter_log_inner(   R#   (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _iter_log_oneline­   s
    
c         C  s1   y t  j j |  ƒ t SWn t k
 r, t SXd  S(   N(   R   t   SemanticVersiont   from_pip_stringR,   t
   ValueErrorR+   (   t	   candidate(    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   _is_valid_version¾   s
    c         c  s  t  j d ƒ d d d g } t | |  ƒ } xÓ | j d ƒ D]Â } | j d ƒ } t | ƒ d k rh q; n  | \ } } } t ƒ  } d | k rï | j ƒ  d	 d
 !} xM | j d ƒ d	 D]5 }	 |	 j d ƒ d }
 t |
 ƒ r³ | j |
 ƒ q³ q³ Wn  | | | f Vq; Wd S(   u  Iterate over --oneline log entries.

    This parses the output intro a structured form but does not apply
    presentation logic to the output - making it suitable for different
    uses.

    :return: An iterator of (hash, tags_set, 1st_line) tuples.
    u   [pbr] Generating ChangeLogu   logu   --decorate=fullu   --format=%h%x00%s%x00%du   
u    i   u
   refs/tags/i   iÿÿÿÿu   , i    N(	   R   R6   R%   R7   R   t   setR   RR   t   add(   R#   t   log_cmdRG   t   linet
   line_partst   shaRC   t   refnameR2   t
   tag_stringRQ   (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyRL   Æ   s     		c         C  s6  t  j  ƒ  } | s i  } n  t j | d d ƒ } | r: d S| sg t d |  ƒ } | rg t | ƒ } qg n  | sq d St j j | d ƒ } t j j | ƒ r¿ t j	 | t j
 ƒ r¿ t j d ƒ d St j d ƒ t j | d d	 d
 ƒ+ } x! | D] \ } }	 | j |	 ƒ qî WWd QXt  j  ƒ  }
 t j d |
 | ƒ d S(   u-   Write a changelog based on the git changelog.u   skip_changelogu   SKIP_WRITE_GIT_CHANGELOGNR#   u	   ChangeLoguI   [pbr] ChangeLog not written (file already exists and it is not writeable)u   [pbr] Writing ChangeLogu   wt   encodingu   utf-8u!   [pbr] ChangeLog complete (%0.1fs)(   t   timeR   t   get_boolean_optionRM   RK   R
   t   patht   joint   existst   accesst   W_OKR   R6   t   iot   opent   write(   R#   t   dest_dirt   option_dictRG   t   startt   should_skipt   new_changelogt   changelog_filet   releaset   contentt   stop(    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   write_git_changelogí   s0    		u   .c         C  s&  t  j | d d ƒ } | r d St j ƒ  } t j j | d ƒ } t j j | d ƒ } t j j | ƒ r t j | t j ƒ r d St	 j
 d ƒ d } |  d k r¬ t ƒ  }  n  |  rg  } d d	 g }	 | t |	 |  ƒ j d
 ƒ 7} g  | D] }
 t j | |
 ƒ sç |
 ^ qç } t d |  ƒ } t j d | t j ƒ } g  | D]( } | r9| j d d ƒ d j ƒ  ^ q9} | | 7} t t | ƒ ƒ } t | d ƒ j } t j j | ƒ rÕt | d ƒ  } | j | j ƒ  ƒ Wd QXn  | j d
 j | ƒ d
 j d ƒ ƒ Wd QXn  t j ƒ  } t	 j
 d | | ƒ d S(   u&   Create AUTHORS file using git commits.u   skip_authorsu   SKIP_GENERATE_AUTHORSNu
   AUTHORS.inu   AUTHORSu   [pbr] Generating AUTHORSu.   (jenkins@review|infra@lists|jenkins@openstack)u   logu   --format=%aN <%aE>u   
u   Co-authored-by:.+u   :i   u   wbu   rbu   utf-8u   [pbr] AUTHORS complete (%0.1fs)(   R   R]   R\   R
   R^   R_   R`   Ra   Rb   R   R6   R	   R*   R%   R7   t   ret   searcht   findallt	   MULTILINER   t   sortedRS   Rd   Re   t   readt   encode(   R#   Rf   Rg   Ri   Rh   t   old_authorst   new_authorst   ignore_emailst   authorst   git_log_cmdt   at   co_authors_outt
   co_authorst   signedt   new_authors_fht   old_authors_fhRn   (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   generate_authors  sD    	+2
(%   t
   __future__R    t   distutils.errorsR   R   R'   Rc   R
   Rp   R   R\   R0   t   pbrR   R   R+   R,   R	   R   R%   R*   R-   R3   R;   R>   R?   R5   R@   RK   RM   RR   RL   R^   t   curdirRo   RD   R‚   (    (    (    s$   /tmp/pip-build-h1VYrz/pbr/pbr/git.pyt   <module>   s8   		
							
			'