U
    jPŒ^2z  ã                   @   s<  d Z ddlmZ ddlmZ ddlmZ zddlm	Z	 W n  e
k
rX   ddl	m	Z	 Y nX 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ZddlmZ ddlmZ dd	lmZ ddlmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlmZ dZ dd„ dD ƒZ!dZ"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dYd!d"„Z(dZd#d$„Z)G d%d&„ d&ejƒZ*G d'd(„ d(ejƒZ+G d)d*„ d*ej,ƒZ-G d+d,„ d,ej.ƒZ/G d-d.„ d.ej.ƒZ0d/d0„ Z1z&dd1l2m3Z3 G d2d3„ d3e3j4ƒZ5d4Z6W n e
k
r   d Z6Y nX d5d6„ Z7d7Z8d8Z9e9e9e8d9œZ:d:d;„ Z;ej< =ej>¡d fd<d=„Z?G d>d?„ d?ejƒZ@G d@dA„ dAejƒZAG dBdC„ dCejBƒZCG dDdE„ dEejƒZDdFdG„ ZEG dHdI„ dIejƒZFzddJlmGZG d4ZHeGjIZIW n e
k
rò   d ZHdZIY nX dKdL„ ZJdMdN„ ZKdOdP„ ZLdQdR„ ZMd[dSdT„ZNdUdV„ ZOd\dWdX„ZPejQjRZRdS )]z4
Utilities with minimum-depends for use in setup.py
é    )Úunicode_literals)Úinstall)Úlog)ÚurlparseN)Údevelop)Úeasy_install)Úegg_info)Úinstall_scripts)Úsdist)Úextra_files)Úgit)Úoptions)Útestr_command)Úversion)zrequirements.txtztools/pip-requiresc                 C   s   g | ]}|t jd   ‘qS )r   )ÚsysÚversion_info)Ú.0Úx© r   ú//usr/lib/python3/dist-packages/pbr/packaging.pyÚ
<listcomp>;   s     r   )zrequirements-py%d.txtztools/pip-requires-py%d)ztest-requirements.txtztools/test-requiresc                  C   s4   t j d¡} | r(tdd„ |  d¡D ƒƒS tttƒ S )NZPBR_REQUIREMENTS_FILESc                 s   s   | ]}|  ¡ V  qd S ©N©Ústrip©r   Úfr   r   r   Ú	<genexpr>C   s     z)get_requirements_files.<locals>.<genexpr>ú,)ÚosÚenvironÚgetÚtupleÚsplitÚPY_REQUIREMENTS_FILESÚlistÚREQUIREMENTS_FILES)Úfilesr   r   r   Úget_requirements_files@   s    r'   c                 C   s:   g }|   |d¡}|r| |¡ | |¡ d |¡| |< dS )z5Append a 
 separated list to possibly existing value.Ú Ú
N)r    ÚappendÚextendÚjoin)ZconfigÚkeyZ	text_listZ	new_valueZcurrent_valuer   r   r   Úappend_text_listL   s    

r.   c                 C   s   dd„ | D ƒS )Nc                 S   s   g | ]}t j |¡r|‘qS r   )r   ÚpathÚexistsr   r   r   r   r   W   s      z!_any_existing.<locals>.<listcomp>r   )Z	file_listr   r   r   Ú_any_existingV   s    r1   c                 C   sv   t | ƒ}dd„ |D ƒ}|r*t d| t¡ dd„ |D ƒ}|D ]4}t|dƒ }| ¡  d¡W  5 Q R £   S Q R X q<g S )Nc                 S   s   g | ]}|t kr|‘qS r   ©r#   r   r   r   r   r   _   s      z'get_reqs_from_files.<locals>.<listcomp>zœSupport for '-pyN'-suffixed requirements files is removed in pbr 5.0 and these files are now ignored. Use environment markers instead. Conflicting files: %rc                 S   s   g | ]}|t kr|‘qS r   r2   r   r   r   r   r   g   s      Úrr)   )r1   ÚwarningsÚwarnÚDeprecationWarningÚopenÚreadr"   )Úrequirements_filesÚexistingZ
deprecatedZrequirements_fileZfilr   r   r   Úget_reqs_from_files[   s    ýü&r;   c                 C   s   t  dd|  ¡ d ¡S )Naÿ  (?P<PackageName>[\w.-]+)-(?P<GlobalVersion>(?P<VersionTripple>(?P<Major>0|[1-9][0-9]*)\.(?P<Minor>0|[1-9][0-9]*)\.(?P<Patch>0|[1-9][0-9]*)){1}(?P<Tags>(?:\-(?P<Prerelease>(?:(?=[0]{1}[0-9A-Za-z-]{0})(?:[0]{1})|(?=[1-9]{1}[0-9]*[A-Za-z]{0})(?:[0-9]+)|(?=[0-9]*[A-Za-z-]+[0-9A-Za-z-]*)(?:[0-9A-Za-z-]+)){1}(?:\.(?=[0]{1}[0-9A-Za-z-]{0})(?:[0]{1})|\.(?=[1-9]{1}[0-9]*[A-Za-z]{0})(?:[0-9]+)|\.(?=[0-9]*[A-Za-z-]+[0-9A-Za-z-]*)(?:[0-9A-Za-z-]+))*){1}){0,1}(?:\+(?P<Meta>(?:[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))){0,1}))z"\g<PackageName>>=\g<GlobalVersion>éÿÿÿÿ)ÚreÚsubÚgroups)Úmatchr   r   r   Úegg_fragmento   s    
ðrA   Fc           
   	   C   sV  | d krt ƒ } g }t| ƒD ]4}| ¡ r| d¡r4qt d|¡rBq| d¡rn| d¡d }|t|g|d7 }qztj	 
|¡j}W n tk
r˜   d }Y nX t d|¡r¼t d|¡}| d	¡}t|ƒ}|jrÜt d
t|j¡}nt d|¡rðd }d}|d k	r>t dd|¡}|r2| d¡}	|	dk r&d }	|d |	… }| |¡ qt d||f ¡ q|S )Nú#z5^\s*(-i|--index-url|--extra-index-url|--find-links).*z-rú é   )Ústrip_markersz\s*-e\s+z\s*-e\s+(.*)$é   zegg=([^&]+).*$z\s*-f\s+zIndex Locationz#.*$r(   ú;r   z[pbr] Excluding %s: %s)r'   r;   r   Ú
startswithr=   r@   Ú	partitionÚparse_requirementsÚpkg_resourcesZRequirementÚparseÚproject_nameÚ
ValueErrorÚgroupr   Zschemer>   rA   ZfragmentÚfindr*   r   Úinfo)
r9   rE   ZrequirementsÚlineZreq_filerM   ÚextractZeggÚreasonZsemi_posr   r   r   rJ   ƒ   sT    ÿ
 ÿ





ÿrJ   c                 C   sj   | d krt ƒ } g }t| ƒD ]J}t d|¡r,qt d|¡rN| t dd|¡¡ qt d|¡r| |¡ q|S )Nz(\s*#)|(\s*$)z\s*-[ef]\s+r(   z+^\s*(https?|git(\+(https|ssh))?|svn|hg)\S*:)r'   r;   r=   r@   r*   r>   )r9   Zdependency_linksrR   r   r   r   Úparse_dependency_linksÆ   s    rU   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚInstallWithGitzœExtracts ChangeLog and AUTHORS from git then installs.

    This is useful for e.g. readthedocs where the package is
    installed and then docs built.
    r   c                 C   s   t | jƒ tj | ¡S r   )Ú	_from_gitÚdistributionr   Úrun©Úselfr   r   r   rY   â   s    
zInstallWithGit.runN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úcommand_namerY   r   r   r   r   rV   Ù   s   rV   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚLocalInstallzÈRuns python setup.py install in a sensible manner.

    Force a non-egg installed in the manner of
    single-version-externally-managed, which allows us to install manpages
    and config files.
    r   c                 C   s   t | jƒ tj | ¡S r   )rW   rX   Ú
du_installr   rY   rZ   r   r   r   rY   ñ   s    
zLocalInstall.runNr\   r   r   r   r   rb   ç   s   rb   c                   @   s    e Zd ZdZdZdZdd„ ZdS )Ú	TestrTestz&Make setup.py test do the right thing.Útestz&DEPRECATED: Run unit tests using testrc                 C   s   t  dt¡ tj | ¡ d S )Nzytestr integration is deprecated in pbr 4.2 and will be removed in a future release. Please call your test runner directly)r4   r5   r6   r   ÚTestrrY   rZ   r   r   r   rY   ü   s    ýzTestrTest.runN©r]   r^   r_   r`   ra   ÚdescriptionrY   r   r   r   r   rd   ö   s   rd   c                   @   s4   e Zd ZdZeZg ZdZdd„ Zdd„ Zdd„ Z	d	S )
ÚLocalRPMVersionz:Output the rpm *compatible* version string of this packageZrpm_versionc                 C   s.   t  d¡ | j ¡ }tt |¡ ¡  ¡ ƒ d S )Nz[pbr] Extracting rpm version)	r   rQ   rX   Úget_nameÚprintr   ÚVersionInfoÚsemantic_versionZ
rpm_string©r[   Únamer   r   r   rY     s    

zLocalRPMVersion.runc                 C   s   d S r   r   rZ   r   r   r   Úinitialize_options  s    z"LocalRPMVersion.initialize_optionsc                 C   s   d S r   r   rZ   r   r   r   Úfinalize_options  s    z LocalRPMVersion.finalize_optionsN©
r]   r^   r_   r`   rh   Zuser_optionsra   rY   rp   rq   r   r   r   r   ri     s   ri   c                   @   s4   e Zd ZdZeZg ZdZdd„ Zdd„ Zdd„ Z	d	S )
ÚLocalDebVersionz:Output the deb *compatible* version string of this packageZdeb_versionc                 C   s.   t  d¡ | j ¡ }tt |¡ ¡  ¡ ƒ d S )Nz[pbr] Extracting deb version)	r   rQ   rX   rj   rk   r   rl   rm   Zdebian_stringrn   r   r   r   rY      s    

zLocalDebVersion.runc                 C   s   d S r   r   rZ   r   r   r   rp   %  s    z"LocalDebVersion.initialize_optionsc                 C   s   d S r   r   rZ   r   r   r   rq   (  s    z LocalDebVersion.finalize_optionsNrr   r   r   r   r   rs     s   rs   c                   C   s   t jS r   )r   Ú
have_testrr   r   r   r   rt   ,  s    rt   )Úcommandsc                   @   s    e Zd ZdZdZdZdd„ ZdS )ÚNoseTestz)Fallback test runner if testr is a no-go.re   z%DEPRECATED: Run unit tests using nosec                 C   s   t  dt¡ tj | ¡ d S )Nzpnose integration in pbr is deprecated. Please use the native nose setuptools configuration or call nose directly)r4   r5   r6   ru   Ú	nosetestsrY   rZ   r   r   r   rY   9  s    ýzNoseTest.runNrg   r   r   r   r   rv   3  s   rv   Tc                   C   s   t S r   )Ú
_have_noser   r   r   r   Ú	have_noseH  s    ry   a  #PBR Generated from %(group)r

import threading

from %(module_name)s import %(import_target)s

if __name__ == "__main__":
    import argparse
    import socket
    import sys
    import wsgiref.simple_server as wss

    parser = argparse.ArgumentParser(
        description=%(import_target)s.__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        usage='%%(prog)s [-h] [--port PORT] [--host IP] -- [passed options]')
    parser.add_argument('--port', '-p', type=int, default=8000,
                        help='TCP port to listen on')
    parser.add_argument('--host', '-b', default='',
                        help='IP to bind the server to')
    parser.add_argument('args',
                        nargs=argparse.REMAINDER,
                        metavar='-- [passed options]',
                        help="'--' is the separator of the arguments used "
                        "to start the WSGI server and the arguments passed "
                        "to the WSGI application.")
    args = parser.parse_args()
    if args.args:
        if args.args[0] == '--':
            args.args.pop(0)
        else:
            parser.error("unrecognized arguments: %%s" %% ' '.join(args.args))
    sys.argv[1:] = args.args
    server = wss.make_server(args.host, args.port, %(invoke_target)s())

    print("*" * 80)
    print("STARTING test server %(module_name)s.%(invoke_target)s")
    url = "http://%%s:%%d/" %% (server.server_name, server.server_port)
    print("Available at %%s" %% url)
    print("DANGER! For testing only, do not use in production")
    print("*" * 80)
    sys.stdout.flush()

    server.serve_forever()
else:
    application = None
    app_lock = threading.Lock()

    with app_lock:
        if application is None:
            application = %(invoke_target)s()

z™# PBR Generated from %(group)r

import sys

from %(module_name)s import %(import_target)s


if __name__ == "__main__":
    sys.exit(%(invoke_target)s())
)Zconsole_scriptsZgui_scriptsÚwsgi_scriptsc                 C   sH   |j rt|j ƒdkrtdƒ‚|t| |j|j d d |j ¡d }|| S )a`  Generate the script based on the template.

    :param str group:
        The entry-point group name, e.g., "console_scripts".
    :param str header:
        The first line of the script, e.g., "!#/usr/bin/env python".
    :param str template:
        The script template.
    :returns:
        The templated script content
    :rtype:
        str
    rD   zBScript targets must be of the form 'func' or 'Class.class_method'.r   Ú.)rO   Úmodule_nameZimport_targetZinvoke_target)ZattrsÚlenrN   Údictr|   r,   )rO   Zentry_pointÚheaderÚtemplateZscript_textr   r   r   Úgenerate_script—  s    
ür   c                 c   sP   t  d||¡}t ¡ D ]4\}}|  |¡ ¡ D ]\}}|t||||ƒfV  q,qdS )z$Override entrypoints console_script.r(   N)r   Úget_script_headerÚENTRY_POINTS_MAPÚitemsÚget_entry_mapr   )ÚdistÚ
executableÚ
is_wininstr   rO   r€   ro   Úepr   r   r   Úoverride_get_script_args±  s    rŠ   c                   @   s   e Zd ZdZdd„ ZdS )ÚLocalDevelopr   c                 C   s:   t jdkrtj | |¡S | js6t|ƒD ]}| j|Ž  q&d S )NZwin32)r   Úplatformr   Úinstall_wrapper_scriptsZexclude_scriptsrŠ   Úwrite_script)r[   r†   Úargsr   r   r   r   ¾  s
    
z$LocalDevelop.install_wrapper_scriptsN)r]   r^   r_   ra   r   r   r   r   r   r‹   º  s   r‹   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚLocalInstallScriptsz(Intercepts console scripts entry_points.r	   c           	      C   sL   t  d||¡}td }| d¡ ¡ D ]"\}}td|||ƒ}|  ||¡ q$d S )Nr(   rz   )r   r‚   rƒ   r…   r„   r   rŽ   )	r[   r†   r‡   rˆ   r   Zwsgi_script_templatero   r‰   Zcontentr   r   r   Ú_make_wsgi_scripts_onlyÊ  s       ÿz+LocalInstallScripts._make_wsgi_scripts_onlyc           	      C   sê   dd l }|  d¡ | jjr,|jjj | ¡ ng | _|  d¡}t	 
|jt	 |j|j¡|j|j¡}|  d¡}t|dtjƒ}t|  d¡ddƒ}d| jjkr¢|  |||¡ | jr¬d S tjd	kr¼t}ntj}d
| }||||ƒD ]}| j|Ž  qÖd S )Nr   r   Zbuild_scriptsr‡   Zbdist_wininstZ_is_runningFZbdist_wheelÚntz"%s")Z!distutils.command.install_scriptsZrun_commandrX   ZscriptsÚcommandr	   rY   ZoutfilesÚget_finalized_commandrK   ZDistributionZegg_baseZPathMetadatar   Zegg_nameZegg_versionÚgetattrr   Zsys_executableZhave_runr‘   Zno_epr   ro   rŠ   Úget_script_argsrŽ   )	r[   Ú	distutilsÚei_cmdr†   Zbs_cmdr‡   rˆ   r–   r   r   r   r   rY   Ò  sB    

 ý
  ÿ  ÿ
zLocalInstallScripts.runN)r]   r^   r_   r`   ra   r‘   rY   r   r   r   r   r   Æ  s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚLocalManifestMakerz?Add any files that are in git and some standard sensible files.c                 C   s   dD ]}| j  |¡ qd S )N)zinclude AUTHORSzinclude ChangeLogzexclude .gitignorezexclude .gitreviewzglobal-exclude *.pyc)ÚfilelistZprocess_template_line)r[   Ztemplate_liner   r   r   Ú_add_pbr_defaults  s    z$LocalManifestMaker._add_pbr_defaultsc                 C   s®   | j  d¡}tj | ¡ | j | j¡ | j | j¡ | j t	 
¡ ¡ t |dd¡}|spt ¡ }|r†| j |¡ ntj | j¡r†|  ¡  |  d¡}|  ¡  | jjd|jd dS )a_  Add all the default files to self.filelist:

        Extends the functionality provided by distutils to also included
        additional sane defaults, such as the ``AUTHORS`` and ``ChangeLog``
        files generated by *pbr*.

        Warns if (``README`` or ``README.txt``) or ``setup.py`` are missing;
        everything else is optional.
        ÚpbrZskip_git_sdistZSKIP_GIT_SDISTr   Ú*)ÚprefixN)rX   Úget_option_dictr
   Úadd_defaultsrš   r*   r€   Úmanifestr+   r   Zget_extra_filesr   Úget_boolean_optionr   Z_find_git_filesr   r/   r0   Zread_manifestr”   r›   Zinclude_patternr   )r[   Úoption_dictÚshould_skipZrcfilesr˜   r   r   r   r      s"    
ÿ
zLocalManifestMaker.add_defaultsN)r]   r^   r_   r`   r›   r    r   r   r   r   r™      s   
r™   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚLocalEggInfozAOverride the egg_info command to regenerate SOURCES.txt sensibly.r   c                 C   sœ   t j | jd¡}t j |¡r2t j d¡s2dtjkr^t d¡ t	| j
ƒ}||_| ¡  |j| _n:t d¡ t ¡ | _t|dƒ ¡  d¡D ]}| j |¡ q†dS )	ap  Generate SOURCES.txt only if there isn't one already.

        If we are in an sdist command, then we always want to update
        SOURCES.txt. If we are not in an sdist command, then it doesn't
        matter one flip, and is actually destructive.
        However, if we're in a git context, it's always the right thing to do
        to recreate SOURCES.txt
        zSOURCES.txtz.gitr
   z[pbr] Processing SOURCES.txtz"[pbr] Reusing existing SOURCES.txtr3   r)   N)r   r/   r,   r   r0   r   Úargvr   rQ   r™   rX   r¡   rY   rš   ZFileListr7   r8   r"   r*   )r[   Zmanifest_filenameZmmÚentryr   r   r   Úfind_sources/  s    	
ÿþ




zLocalEggInfo.find_sourcesN)r]   r^   r_   r`   ra   r¨   r   r   r   r   r¥   *  s   r¥   c                 C   s>   |   d¡}t ¡ }|r t |¡}tj||d tj|d d S )Nrœ   )r£   Ú	changelog)r£   )rŸ   r   Ú_iter_log_onelineZ_iter_changelogZwrite_git_changelogZgenerate_authors)rX   r£   r©   r   r   r   rW   H  s    

rW   c                   @   s>   e Zd ZdZdZdd„ Zdefgejj Zdd„ Zdd	„ Z	d
S )Ú
LocalSDistz5Builds the ChangeLog and Authors files from VC first.r
   c                 C   sÎ   t | dƒr| jS | j d¡}t |dd¡}|r8d| _dS zddlm} W n& tk
rn   t	 
d¡ d| _Y dS X | | j¡\}}}tj tj |j|j¡¡s°t	 
d	¡ d| _dS ||g| _t	 
d
¡ d| _dS )z´Ensure reno is installed and configured.

        We can't run reno-based commands if reno isn't installed/available, and
        don't want to if the user isn't using it.
        Ú	_has_renorœ   Z	skip_renoZSKIP_GENERATE_RENOFr   )Úsetup_commandz>[pbr] reno was not found or is too old. Skipping release noteszC[pbr] reno does not appear to be configured. Skipping release notesz[pbr] Generating release notesT)Úhasattrr¬   rX   rŸ   r   r¢   Zrenor­   ÚImportErrorr   rQ   Zload_configr   r/   r0   r,   ZreporootZ	notespathÚ_files)r[   r£   r¤   r­   ZconfZoutput_fileZ
cache_filer   r   r   Úchecking_renoV  s4    
ÿ
ÿ



zLocalSDist.checking_renoZ
build_renoc                 C   s   t | jƒ tj | ¡ d S r   )rW   rX   r
   rY   rZ   r   r   r   rY   ‚  s    
zLocalSDist.runc                 C   s0   |   ¡ r | j | j¡ | j ¡  tj | ¡ d S r   )r±   rš   r+   r°   Úsortr
   Úmake_distributionrZ   r   r   r   r³   ‡  s    
zLocalSDist.make_distributionN)
r]   r^   r_   r`   ra   r±   r
   Zsub_commandsrY   r³   r   r   r   r   r«   Q  s   *r«   )Úbuilddocc                   C   s   t S r   )Ú_have_sphinxr   r   r   r   Úhave_sphinxœ  s    r¶   c           	         sÖ   i ‰|r|d }nd}t  dd|g| ¡}tdƒ‰ ‡ fdd„| d¡D ƒ}tƒ }|D ]}| d	d„ | d
¡D ƒ¡ qR‡fdd„}|d|dƒ |d|dƒ |d|dƒ |d|dƒ |D ]}t d| ¡ q²ˆ dd¡ ˆS )a  Calculate the sort of semver increment needed from git history.

    Every commit from HEAD to tag is consider for Sem-Ver metadata lines.
    See the pbr docs for their syntax.

    :return: a dict of kwargs for passing into SemanticVersion.increment.
    z..HEADZHEADr   z--pretty=%Búsem-ver:c                    s*   g | ]"}|  ¡  d ¡r|ˆ d…  ¡ ‘qS )r·   N)ÚlowerrH   r   )r   rR   )Ú
header_lenr   r   r   ³  s    ÿz)_get_increment_kwargs.<locals>.<listcomp>r)   c                 S   s   g | ]}|  ¡ ‘qS r   r   )r   Úsymbolr   r   r   r   ·  s     r   c                    s   | |krdˆ |< |  | ¡ d S )NT)Údiscard)rº   ÚsymbolsZimpact)Úresultr   r   Ú_handle_symbol¹  s    z-_get_increment_kwargs.<locals>._handle_symbolZbugfixZpatchZfeatureÚminorZdeprecationz	api-breakÚmajorz[pbr] Unknown Sem-Ver symbol %rN)	r   Ú_run_git_commandr}   r"   ÚsetÚupdater   rQ   Úpop)	Úgit_dirÚtagZversion_specr©   ru   r¼   r“   r¾   rº   r   )r¹   r½   r   Ú_get_increment_kwargs   s*    
ÿrÇ   c           	   
   C   sš   t j| d}d}t|ƒD ]x\}\}}}tƒ }tƒ }t|ƒD ]<}z"tj |¡}|||< | 	|¡ W q: t
k
rt   Y q:X q:|r|t|ƒ |f  S qd|fS )zÅReturn the commit data about the most recent tag.

    We use git-describe to find this out, but if there are no
    tags then we fall back to counting commits since the beginning
    of time.
    )rÅ   r   r(   )r   rª   Ú	enumeraterÂ   r~   r$   r   ÚSemanticVersionÚfrom_pip_stringÚaddÚ	ExceptionÚmax)	rÅ   r©   Z	row_countZignoredZtag_setZversion_tagsZsemver_to_tagrÆ   Zsemverr   r   r   Ú_get_revno_and_last_tagÉ  s    rÎ   c                 C   s˜   t | ƒ\}}tj |pd¡}|dkr*|}n|jf t| |ƒŽ}|dk	r`||kr`tdt||d ƒ‚|dkrl|S | |¡}|dk	r”| |¡}||kr”|S |S )a¥  Calculate a version from a target version in git_dir.

    This is used for untagged versions only. A new version is calculated as
    necessary based on git metadata - distance to tags, current hash, contents
    of commit messages.

    :param git_dir: The git directory we're working from.
    :param target_version: If None, the last tagged version (or 0 if there are
        no tags yet) is incremented as needed to produce an appropriate target
        version following semver rules. Otherwise target_version is used as a
        constraint - if semver rules would result in a newer version then an
        exception is raised.
    :return: A semver version object.
    Ú0r   NzRgit history requires a target version of %(new)s, but target version is %(target)s)ÚnewÚtarget)	rÎ   r   rÉ   rÊ   Z	incrementrÇ   rN   r~   Zto_dev)rÅ   Útarget_versionrÆ   ZdistanceZlast_semverZnew_versionZnew_devZ
target_devr   r   r   Ú_get_version_from_git_targetá  s*    ÿ
þÿ

rÓ   c                 C   sš   t  ¡ }|rvz,t jddg|dd dd¡}tj |¡}W n* tk
rb   | rZtj | ¡}nd}Y nX t||ƒ}| 	¡ S zt
ƒ W S  tk
r”   Y dS X dS )	až  Calculate a version string from git.

    If the revision is tagged, return that. Otherwise calculate a semantic
    version description of the tree.

    The number of revisions since the last tag is included in the dev counter
    in the version for untagged versions.

    :param pre_version: If supplied use this as the target version rather than
        inferring one from the last tag + commit messages.
    Zdescribez--exact-matchT)Zthrow_on_errorú-r{   Nr(   )r   Z_run_git_functionsrÁ   Úreplacer   rÉ   rÊ   rÌ   rÓ   Zrelease_stringZunicodeÚ	NameError)Úpre_versionrÅ   ZtaggedrÒ   r½   r   r   r   Ú_get_version_from_git  s0     þ þÿ

rØ   c              
   C   sŽ   ddg}i }|D ]\}zt |dƒ}W n ttfk
r>   Y qY nX zt |¡}W q tjjk
rj   Y qY qX q| dd¡| kr‚dS | dd¡S )z¾Get the version from package metadata if present.

    This looks for PKG-INFO if present (for sdists), and if not looks
    for METADATA (for wheels) and failing that will return None.
    zPKG-INFOZMETADATAr3   ÚNameNZVersion)r7   ÚIOErrorÚOSErrorÚemailZmessage_from_fileÚerrorsZMessageErrorr    )Úpackage_nameZpkg_metadata_filenamesZpkg_metadataÚfilenameZpkg_metadata_filer   r   r   Ú_get_version_from_pkg_metadata,  s    
rà   c                 C   sl   t j dt j dd¡¡}|r |S t| ƒ}|r0|S t|ƒ}tjd dkrP| d¡}|rX|S tdj	| dƒ‚dS )	a  Get the version of the project.

    First, try getting it from PKG-INFO or METADATA, if it exists. If it does,
    that means we're in a distribution tarball or that install has happened.
    Otherwise, if there is no PKG-INFO or METADATA file, pull the version
    from git.

    We do not support setup.py version sanity in git archive tarballs, nor do
    we support packagers directly sucking our git repo into theirs. We expect
    that a source tarball be made from our git repo - or that if someone wants
    to make a source tarball from a fork of our repo with additional tags in it
    that they understand and desire the results of doing that.

    :param pre_version: The version field from setup.cfg - if set then this
        version will be the next release.
    ZPBR_VERSIONZOSLO_PACKAGE_VERSIONNr   rD   zutf-8a(  Versioning for this project requires either an sdist tarball, or access to an upstream git repository. It's also possible that there is a mismatch between the package name in setup.cfg and the argument given to pbr.version.VersionInfo. Project name {name} was given, but was not able to be found.)ro   )
r   r   r    rà   rØ   r   r   ÚencoderÌ   Úformat)rÞ   r×   r   r   r   r   Úget_versionD  s"    þ
úrã   )NF)N)N)N)Sr`   Z
__future__r   Zdistutils.commandr   rc   r—   r   Zurllib.parser   r¯   rÜ   Zemail.errorsr   r=   r   r4   rK   Z
setuptoolsZsetuptools.commandr   r   r   r	   r
   rœ   r   r   r   Zpbr.pbr_jsonr   r   r%   r#   ZTEST_REQUIREMENTS_FILESr'   r.   r1   r;   rA   rJ   rU   rV   rb   rf   rd   ZCommandri   rs   rt   Znoseru   rw   rv   rx   ry   Z
_wsgi_textZ_script_textrƒ   r   r/   Únormpathr‡   rŠ   r‹   r   Zmanifest_makerr™   r¥   rW   r«   r´   rµ   ZLocalBuildDocr¶   rÇ   rÎ   rÓ   rØ   rà   rã   Zpbr_jsonZwrite_pbr_jsonr   r   r   r   Ú<module>   s¢   

C

6ý ÿ
	:*	@

)%
&
/