
U&                 @   s{  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 d d d  Z d	 d
   Z d d   Z d d   Z d d   Z d d d d  Z d d   Z d d   Z d d   Z d d d  Z d d   Z d d d  Z d  d!   Z d e j j d d d" d#  Z d d$ e   d% d&  Z d S)'    )unicode_literalsN)log)optionsFTc       	      C   s   | r t  j } t  j } n d  } d  } t j j   } | rI | j |  t  j |  d | d | d | } | j   } | j r | r t	 j
 j d |  | j f   t |  d k s | d s | d j   r d S| d j   j d d  S)	Nstdoutstderrenvz%s returned %dr    zutf-8replace)
subprocessPIPEosenvironcopyupdatePopenZcommunicate
returncode	distutilserrorsZDistutilsErrorlenstripdecode)	cmdZthrow_on_errorbufferr   Zout_locationZerr_locationZnewenvoutputout r   )/usr/lib/python3/dist-packages/pbr/git.py_run_shell_command!   s&    			.r   c             K   s9   t  |  t t f  s |  g }  t d d | g |  |  S)Ngitz--git-dir=%s)
isinstancelisttupler   )r   git_dirkwargsr   r   r   _run_git_command<   s    	r$   c              C   s\   y t  d d d g  SWn> t k
 rW }  z |  j t j k rB d S  WYd  d  }  ~  Xn Xd  S)Nr   z	rev-parsez	--git-dirr   )r   OSErrorerrnoENOENT)er   r   r   _get_git_directoryC   s    r)   c               C   s1   y t  d d g  Wn t k
 r, d SYn Xd S)Nr   z	--versionFT)r   r%   r   r   r   r   _git_is_installedM   s
    	r*   c             C   s   t  |  d t j S)zFind 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.
    key)maxpkg_resourcesZparse_version)tagsr   r   r   _get_highest_tagX   s    r/   r   c             C   sl   g  } | d k r t    } | r[ t j d  t d d g |  } | j d j d   } d d   | D S)	zBehave 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.
    Nz2[pbr] In git context, generating filelist from gitzls-filesz-zs    zutf-8c             S   s   g  |  ] } | r |  q Sr   r   ).0fr   r   r   
<listcomp>p   s   	 z#_find_git_files.<locals>.<listcomp>)_run_git_functionsr   infor$   splitr   )dirnamer"   Z	file_listr   r   r   _find_git_filesa   s    	r7   c             C   sI   t  d d g |   } d | k r5 | j d d  d Sd | k rE d Sd  S)Ndescribez--always-   .r   )r$   rsplit)r"   r8   r   r   r   _get_raw_tag_infos   s    r>   c             C   s   t  |   d k S)Nr   )r>   )r"   r   r   r   get_is_release|   s    r?   c              C   s"   d  }  t    r t   }  |  p! d  S)N)r*   r)   )r"   r   r   r   r3      s    		r3   c             C   s/   |  s t    }  |  r+ t d d d g |   Sd S)z1Return the short sha for this repo, if it exists.r   z-n1z--pretty=format:%hN)r3   r$   )r"   r   r   r   get_git_short_sha   s    	r@   c             c   s   d } d } | d f Vx |  D] \ } } } | r~ t  |  } t |  d } | s` | d f V| d t d | d |  f V| j d	  s | j d
  r | d d  } | d t d |  f Vd } q Wd S)zConvert 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.
    TNzCHANGES
=======

r9   
z%(tag)s
%(underline)s

tag	underlinezMerge r;      z
* %(msg)s
msgF)r/   r   dict
startswithendswith)	changelogZ
first_lineZcurrent_releasehashr.   rE   rC   r   r   r   _iter_changelog   s"    rL   c             C   s)   |  d k r t    }  |  s g  St |   S)aQ  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)   _iter_log_inner)r"   r   r   r   _iter_log_oneline   s
    
	rN   c             c   s!  t  j d  d d d g } t | |   } x | j d  D] } | j   } t |  d k  rb q; | d j d  r d	 | k r | j d	  d j   } n d
 j | d d   } d | k r t d d   | j d	  d j d  d d  D  } n	 t   } | d | | f Vq; Wd S)a  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.
    z[pbr] Generating ChangeLogr   z	--onelinez
--decoraterA   r:   rD   () Nztag:c             S   s#   g  |  ] } | j  d   d  q S),r   )r5   )r0   rB   r   r   r   r2      s   	z#_iter_log_inner.<locals>.<listcomp>r   ztag: )	r   r4   r$   r5   r   rH   r   joinset)r"   Zlog_cmdrJ   lineZ
line_partsrE   r.   r   r   r   rM      s     		0	rM   c             C   s!  t  j    } | s i  } t j | d d  } | r7 d S| s^ t d |   } | r^ t |  } | sh d St j d  t j j	 | d  } t j j
 |  r t j | t j  r d St j | d d d	 + } x! | D] \ } }	 | j |	  q WWd QRXt  j    }
 t j d
 |
 |  d S)z-Write a changelog based on the git changelog.Zskip_changelogZSKIP_WRITE_GIT_CHANGELOGNr"   z[pbr] Writing ChangeLogZ	ChangeLogwencodingzutf-8z![pbr] ChangeLog complete (%0.1fs))timer   get_boolean_optionrN   rL   r   r4   r   pathrS   existsaccessW_OKioopenwrite)r"   dest_diroption_dictrJ   startshould_skipZnew_changelogZchangelog_filereleaseZcontentstopr   r   r   write_git_changelog   s.    	rg   r;   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   }  |  rg  } d d	 g } | t | |   j d
  7}   f d d   | D } t d |   }	 t j d |	 t j  }
 d d   |
 D }
 | |
 7} t t |   } t | d  h } t j j |  rt | d   } | j | j    Wd QRX| j d
 j |  d
 j d   Wd QRXt j   } t	 j
 d | |  d S)z&Create AUTHORS file using git commits.Zskip_authorsZSKIP_GENERATE_AUTHORSNz
AUTHORS.inZAUTHORSz[pbr] Generating AUTHORSz.(jenkins@review|infra@lists|jenkins@openstack)r   z--format=%aN <%aE>rA   c                s(   g  |  ] } t  j   |  s |  q Sr   )research)r0   a)ignore_emailsr   r   r2     s   	 z$generate_authors.<locals>.<listcomp>zCo-authored-by:.+c             S   s2   g  |  ]( } | r | j  d  d  d j    q S):rD   )r5   r   )r0   Zsignedr   r   r   r2     s   	wbrbzutf-8z[pbr] AUTHORS complete (%0.1fs))r   rY   rX   r   rZ   rS   r[   r\   r]   r   r4   r)   r$   r5   rh   findall	MULTILINEsortedrT   r_   r`   readencode)r"   ra   rb   rd   rc   Zold_authorsZnew_authorsZauthorsZgit_log_cmdZco_authors_outZ
co_authorsZnew_authors_fhZold_authors_fhrf   r   )rk   r   generate_authors   sD    			

rt   ) Z
__future__r   Zdistutils.errorsr   r   r&   r^   r   rh   r
   rX   r-   Zpbrr   r   r$   r)   r*   r/   r7   r>   r?   r3   r@   rL   rN   rM   rZ   curdirrg   rG   rt   r   r   r   r   <module>   s4   
		
"