U
    jP^<-                     @   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lmZ d,dd	Zd
d Zdd Zdd Zdd Zd-ddZdd Zdd Zdd Zd.ddZdd Zdd  Zd/d!d"Zd#d$ Zd%d& Zdejjddfd'd(Z dd)e! fd*d+Z"dS )0    )unicode_literalsN)log)options)versionFTc           	      C   s   |rt j}t j}nd }d }tj }|r2|| t j| |||d}| }|jrl|rlt	j
d| |jf t|dks|d r|d  sdS |d  ddS )N)stdoutstderrenvz%s returned %dr    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   s,   t | ttfs| g} tdd| g|  f|S )Ngitz--git-dir=%s)
isinstancelisttupler   )r   git_dirkwargsr   r   r   _run_git_command=   s    r&   c               
   C   sP   zt dddgW S  tk
rJ }  z| jtjkr8W Y 
dS  W 5 d } ~ X Y nX d S )Nr    z	rev-parsez	--git-dirr	   )r   OSErrorerrnoZENOENT)er   r   r   _get_git_directoryD   s    
r*   c                   C   s,   zt ddg W n tk
r&   Y dS X dS )Nr    z	--versionFT)r   r'   r   r   r   r   _git_is_installedN   s
    r+   c                 C   s   t | tjd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_tagY   s    r0   r	   c                 C   sL   g }|dkrt  }|r>td tddg|}|d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-z    r
   c                 S   s   g | ]}|r|qS r   r   ).0fr   r   r   
<listcomp>q   s      z#_find_git_files.<locals>.<listcomp>)_run_git_functionsr   infor&   splitr   )dirnamer$   Z	file_listr   r   r   _find_git_filesb   s    
r9   c                 C   s6   t ddg| }d|kr&|ddd S d|kr2dS d S )Ndescribez--always-   .r   )r&   rsplit)r$   r:   r   r   r   _get_raw_tag_infot   s    r@   c                 C   s   t | dkS )Nr   )r@   r$   r   r   r   get_is_release}   s    rB   c                  C   s   d } t  rt } | pd S )N)r+   r*   rA   r   r   r   r5      s    r5   c                 C   s"   | s
t  } | rtdddg| S dS )z1Return the short sha for this repo, if it exists.r   z-n1z--pretty=format:%hN)r5   r&   rA   r   r   r   get_git_short_sha   s     rC   c                 C   s(   |  dd} |  dd} |  dd} | S )a  Cleans any instances of invalid sphinx wording.

    This escapes/removes any instances of invalid characters
    that can be interpreted by sphinx as a warning or error
    when translating the Changelog into an HTML file for
    documentation building within projects.

    * Escapes '_' which is interpreted as a link
    * Escapes '*' which is interpreted as a new line
    * Escapes '`' which is interpreted as a literal
    *z\*_z\_`z\`)r   msgr   r   r   _clean_changelog_message   s    rI   c                 c   s   d}d}|dfV  | D ]\}}}|r\t |}t|d }|sF|dfV  |dt||d fV  |ds|d	r||dd
 }t|}|dt|d fV  d}qd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
=======

r;   
z%(tag)s
%(underline)s

)tag	underlinezMerge r>   z
* %(msg)s
rG   F)r0   r   dict
startswithendswithrI   )	changelogZ
first_lineZcurrent_releasehashr/   rH   rL   r   r   r   _iter_changelog   s(    




rS   c                 C   s   | dkrt  } | sg S t| 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_innerrA   r   r   r   _iter_log_oneline   s
    
rU   c                 C   s.   zt j|  W dS  tk
r(   Y dS X d S )NTF)r   ZSemanticVersionZfrom_pip_string
ValueError)	candidater   r   r   _is_valid_version   s
    rX   c                 c   s   t d dddg}t|| }|dD ]}|d}t|dkrDq(|\}}}t }d|kr| d	d
 }|dd	d D ],}	|	dd dd}
t|
r~|	|
 q~|||fV  q(d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.

    .. caution:: this function risk to return a tag that doesn't exist really
                 inside the git objects list due to replacement made
                 to tag name to also list pre-release suffix.
                 Compliant with the SemVer specification (e.g 1.2.3-rc1)

    :return: An iterator of (hash, tags_set, 1st_line) tuples.
    z[pbr] Generating ChangeLogr   z--decorate=fullz--format=%h%x00%s%x00%drJ       z
refs/tags/   rM   Nz, r   r;   r>   )
r   r6   r&   r7   r   setr   r   rX   add)r$   Zlog_cmdrQ   lineZ
line_partsZsharH   Zrefnamer/   Z
tag_stringrW   r   r   r   rT      s     




rT   c              	   C   s   t   }|si }t|dd}|r&dS |s@t| d}|r@t|}|sHdS tj|d}tj|r~t	|tj
s~td dS td tj|dd	d
}|D ]\}}	||	 qW 5 Q R X t   }
td|
|   dS )z-Write a changelog based on the git changelog.Zskip_changelogZSKIP_WRITE_GIT_CHANGELOGNrA   Z	ChangeLogzI[pbr] ChangeLog not written (file already exists and it is not writeable)z[pbr] Writing ChangeLogwr
   )encodingz![pbr] ChangeLog complete (%0.1fs))timer   get_boolean_optionrU   rS   r   pathjoinexistsaccessW_OKr   r6   ioopenwrite)r$   dest_diroption_dictrQ   startshould_skipZnew_changelogZchangelog_filereleaseZcontentstopr   r   r   write_git_changelog  s0    


rq   r>   c              
      sh  t |dd}|rdS t }tj|d}tj|d}tj|rXt|tjsXdS t	
d d | dkrtt } | rJg }dd	g}|t|| d
7 } fdd|D }td| }	td|	tj}
dd |
D }
||
7 }tt|}t|dR}tj|r&t|d}||  W 5 Q R X |d
|d
 d W 5 Q R X t }t	
d||   dS )z&Create AUTHORS file using git commits.Zskip_authorsZSKIP_GENERATE_AUTHORSNz
AUTHORS.inZAUTHORSz[pbr] Generating AUTHORSz5((jenkins|zuul)@review|infra@lists|jenkins@openstack)r   z--format=%aN <%aE>rJ   c                    s   g | ]}t  |s|qS r   )research)r2   aZignore_emailsr   r   r4   ?  s      z$generate_authors.<locals>.<listcomp>zCo-authored-by:.+c                 S   s$   g | ]}|r| d dd  qS ):r[   )r7   r   )r2   Zsignedr   r   r   r4   E  s    wbrbr
   z[pbr] AUTHORS complete (%0.1fs))r   rb   ra   r   rc   rd   re   rf   rg   r   r6   r*   r&   r7   rr   findall	MULTILINEsortedr\   ri   rj   readencode)r$   rk   rl   rn   rm   Zold_authorsZnew_authorsZauthorsZgit_log_cmdZco_authors_outZ
co_authorsZnew_authors_fhZold_authors_fhrp   r   ru   r   generate_authors'  sJ    

r~   )FTN)r	   N)N)N)#Z
__future__r   Zdistutils.errorsr   r   r(   rh   r   rr   r   ra   r.   Zpbrr   r   r   r&   r*   r+   r0   r9   r@   rB   r5   rC   rI   rS   rU   rX   rT   rc   curdirrq   rN   r~   r   r   r   r   <module>   s>   

	
	


, 
 