
UY                 @   s  d  Z  d d l m Z d d l m 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 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 dS Z dT Z d d   Z d d   Z d d   Z d d   Z  d d d d  Z! d d d   Z" Gd! d"   d" e j  Z# Gd# d$   d$ e j  Z$ Gd% d&   d& e j%  Z& Gd' d(   d( e j'  Z( d) d*   Z) y3 d d+ l* m+ Z+ Gd, d-   d- e+ j,  Z- d. Z. Wn e/ k
 rTd Z. Yn Xd/ d0   Z0 d1 Z1 d2 Z2 d3 e2 d4 e2 d5 e1 i Z3 e	 j4 j5 e j6  d d6 d7  Z7 Gd8 d9   d9 e j  Z8 Gd: d;   d; e j  Z9 Gd< d=   d= e j:  Z; Gd> d?   d? e j  Z< d@ dA   Z= GdB dC   dC e j  Z> y, d dD l m? Z? d. Z@ e? jA ZA e? jB ZB Wn$ e/ k
 rd Z@ d ZA d ZB Yn XdE dF   ZC dG dH   ZD dI dJ   ZE dK dL   ZF d dM dN  ZG dO dP   ZH d dQ dR  ZI e jJ jK ZK d S)Uz4
Utilities with minimum-depends for use in setup.py
    )unicode_literals)install)logN)develop)easy_install)egg_info)install_scripts)sdist)extra_files)git)options)testr_command)versionrequirements.txttools/pip-requirestest-requirements.txttools/test-requiresc              C   sx   t  j j d  }  |  r8 t d d   |  j d  D  St t d t t j	 d  j
 t t  j j t    t t  S)NZPBR_REQUIREMENTS_FILESc             s   s   |  ] } | j    Vq d  S)N)strip).0f r   //usr/lib/python3/dist-packages/pbr/packaging.py	<genexpr>5   s    z)get_requirements_files.<locals>.<genexpr>,z-pyr   )osenvirongettuplesplitlistmapstrsysversion_infojoinpathsplitextREQUIREMENTS_FILES)filesr   r   r   get_requirements_files2   s     r)   c             C   sO   g  } |  j  | d  } | r+ | j |  | j |  d j |  |  | <d S)z5Append a 
 separated list to possibly existing value. 
N)r   appendextendr$   )configkeyZ	text_listZ	new_valueZcurrent_valuer   r   r   append_text_list?   s    r0   c             C   s   d d   |  D S)Nc             S   s(   g  |  ] } t  j j |  r |  q Sr   )r   r%   exists)r   r   r   r   r   
<listcomp>J   s   	 z!_any_existing.<locals>.<listcomp>r   )Z	file_listr   r   r   _any_existingI   s    r3   c          
   C   sG   x@ t  |   D]2 } t | d   } | j   j d  SWd  QRXq Wg  S)Nrr+   )r3   openreadr   )requirements_filesZrequirements_fileZfilr   r   r   get_reqs_from_filesN   s    r8   Fc       	      C   s  |  d  k r t    }  d d   } g  } xt |   D]} | j   s4 | j d  rY q4 | j d  r | j d  d } | t | g d | 7} q4 y t j j |  j	 } Wn t
 k
 r d  } Yn Xt j d |  r t j d	 | |  } nH t j d
 |  rt j d | |  } n t j d |  r=d  } d } | d  k	 rt j d d |  } | r| j d  } | d k  rd  } | d  |  } | j |  q4 t j d | | f  q4 W| S)Nc             S   s   t  j d d |  j d   S)Nz([\w.]+)-([\w.-]+)z\1>=\2   )resubgroup)matchr   r   r   egg_fragmentZ   s    	z(parse_requirements.<locals>.egg_fragment#z-r    strip_markersz\s*-e\s+z\s*-e\s+.*#egg=(.*)$z
\s*https?:z\s*https?:.*#egg=(.*)$z\s*-f\s+zIndex Locationz#.*$r*   ;r   z[pbr] Excluding %s: %s)r)   r8   r   
startswith	partitionparse_requirementspkg_resourcesZRequirementparseproject_name
ValueErrorr:   r=   r;   findr,   r   info)	r7   rB   r>   ZrequirementslineZreq_filerI   reasonZsemi_posr   r   r   rF   U   sD    	rF   c             C   s   |  d  k r t    }  g  } xy t |   D]k } t j d |  rC q( t j d |  rt | j t j d d |   q( t j d |  r( | j |  q( W| S)Nz(\s*#)|(\s*$)z\s*-[ef]\s+r*   z
\s*https?:)r)   r8   r:   r=   r,   r;   )r7   Zdependency_linksrM   r   r   r   parse_dependency_links   s    	rO   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)InstallWithGitzExtracts 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 j |   S)N)	_from_gitdistributionr   run)selfr   r   r   rS      s    zInstallWithGit.runN)__name__
__module____qualname____doc__command_namerS   r   r   r   r   rP      s   rP   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)LocalInstallzRuns 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 j |   S)N)rQ   rR   
du_installr   rS   )rT   r   r   r   rS      s    zLocalInstall.runN)rU   rV   rW   rX   rY   rS   r   r   r   r   rZ      s   rZ   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)	TestrTestz&Make setup.py test do the right thing.testc             C   s   t  j j |   d  S)N)r   TestrrS   )rT   r   r   r   rS      s    zTestrTest.runN)rU   rV   rW   rX   rY   rS   r   r   r   r   r\      s   r\   c               @   sL   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  j d  |  j j   } t t j |  j   j    d  S)Nz[pbr] Extracting rpm version)	r   rL   rR   Zget_nameprintr   ZVersionInfoZsemantic_versionZ
rpm_string)rT   namer   r   r   rS      s    zLocalRPMVersion.runc             C   s   d  S)Nr   )rT   r   r   r   initialize_options   s    z"LocalRPMVersion.initialize_optionsc             C   s   d  S)Nr   )rT   r   r   r   finalize_options   s    z LocalRPMVersion.finalize_optionsN)
rU   rV   rW   rX   ZdescriptionZuser_optionsrY   rS   rb   rc   r   r   r   r   r_      s   r_   c               C   s   t  j S)N)r   
have_testrr   r   r   r   rd      s    rd   )commandsc               @   s(   e  Z d  Z d Z d Z d d   Z d S)NoseTestz)Fallback test runner if testr is a no-go.r]   c             C   s   t  j j |   d  S)N)re   	nosetestsrS   )rT   r   r   r   rS      s    zNoseTest.runN)rU   rV   rW   rX   rY   rS   r   r   r   r   rf      s   rf   Tc               C   s   t  S)N)
_have_noser   r   r   r   	have_nose   s    ri   a5  #PBR Generated from %(group)r

import threading

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

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

    my_ip = socket.gethostbyname(socket.gethostname())
    parser = argparse.ArgumentParser(
        description=%(import_target)s.__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--port', '-p', type=int, default=8000,
                        help='TCP port to listen on')
    args = parser.parse_args()
    server = wss.make_server('', args.port, %(invoke_target)s())

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

    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_scriptsZwsgi_scriptsc       	      c   s   t  j d | |  } x t j   D] \ } } x |  j |  j   D] \ } } | j so t | j  d k r{ t d   | t d | d | j	 d | j d d d	 j
 | j   } | | | f VqD Wq" Wd
 S)z$Override entrypoints console_script.r*   rA   zBScript targets must be of the form 'func' or 'Class.class_method'.r<   module_nameZimport_targetr   Zinvoke_target.N)r   Zget_script_headerENTRY_POINTS_MAPitemsZget_entry_mapZattrslenrJ   dictrj   r$   )	dist
executable
is_wininstheaderr<   templatera   ZepZscript_textr   r   r   override_get_script_args6  s    "		ru   c               @   s"   e  Z d  Z d Z d d   Z d S)LocalDevelopr   c             C   sS   t  j d k r" t j j |  |  S|  j sO x! t |  D] } |  j |   q8 Wd  S)Nwin32)r"   platformr   install_wrapper_scriptsZexclude_scriptsru   write_script)rT   rp   argsr   r   r   ry   L  s
    	z$LocalDevelop.install_wrapper_scriptsN)rU   rV   rW   rY   ry   r   r   r   r   rv   H  s   rv   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)LocalInstallScriptsz(Intercepts console scripts entry_points.r   c       	      C   s.  d d  l  } |  j d  |  j j r> | j j j j |   n	 g  |  _ |  j rT d  S|  j	 d  } t
 j | j t
 j | j | j  | j | j  } |  j	 d  } t | d t j  } t |  j	 d  d d  } t j d k r t } n t j } d	 | } x' | | | |  D] } |  j |   qWd  S)
Nr   r   Zbuild_scriptsrq   Zbdist_wininstZ_is_runningFntz"%s")Z!distutils.command.install_scriptsZrun_commandrR   scriptscommandr   rS   ZoutfilesZno_epget_finalized_commandrG   ZDistributionZegg_baseZPathMetadatar   Zegg_nameZegg_versiongetattrr   Zsys_executabler   ra   ru   get_script_argsrz   )	rT   	distutilsei_cmdrp   Zbs_cmdrq   rr   r   r{   r   r   r   rS   X  s.    				
zLocalInstallScripts.runN)rU   rV   rW   rX   rY   rS   r   r   r   r   r|   T  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   s4   x- d d d d d g D] } |  j  j |  q Wd  S)Nzinclude AUTHORSzinclude ChangeLogzexclude .gitignorezexclude .gitreviewzglobal-exclude *.pyc)filelistZprocess_template_line)rT   Ztemplate_liner   r   r   _add_pbr_defaults  s    z$LocalManifestMaker._add_pbr_defaultsc             C   s   |  j  j d  } t j j |   |  j j |  j  |  j j |  j  |  j j t	 j
    t j | d d  } | s t j   } | r |  j j |  n t j j |  j  r |  j   |  j d  } |  j   |  j j d d | j d  S)NpbrZskip_git_sdistZSKIP_GIT_SDISTr   *prefix)rR   get_option_dictr	   add_defaultsr   r,   rt   manifestr-   r
   Zget_extra_filesr   Zget_boolean_optionr   Z_find_git_filesr   r%   r1   Zread_manifestr   r   Zinclude_patternr   )rT   option_dictZshould_skipZrcfilesr   r   r   r   r     s     	

zLocalManifestMaker.add_defaultsN)rU   rV   rW   rX   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 |  j d  } t  j j |  sL t  j j d  sL d t j k r t j d  t	 |  j
  } | | _ | j   | j |  _ nU t j d  t j   |  _ x6 t | d  j   j d  D] } |  j j |  q Wd 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.txtr4   r+   N)r   r%   r$   r   r1   r"   argvr   rL   r   rR   r   rS   r   ZFileListr5   r6   r   r,   )rT   Zmanifest_filenameZmmentryr   r   r   find_sources  s    		
%zLocalEggInfo.find_sourcesN)rU   rV   rW   rX   rY   r   r   r   r   r   r     s   r   c             C   sZ   |  j  d  } t j   } | r0 t j |  } t j d | d |  t j d |  d  S)Nr   r   	changelog)r   r   _iter_log_onelineZ_iter_changelogZwrite_git_changelogZgenerate_authors)rR   r   r   r   r   r   rQ     s    rQ   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)
LocalSDistz5Builds the ChangeLog and Authors files from VC first.r	   c             C   s!   t  |  j  t j j |   d  S)N)rQ   rR   r	   rS   )rT   r   r   r   rS     s    zLocalSDist.runN)rU   rV   rW   rX   rY   rS   r   r   r   r   r     s   r   )builddocc               C   s   t  S)N)_have_sphinxr   r   r   r   have_sphinx  s    r   c       	         s*  i   | r | d } n d } t  j d | g |   } t d      f d d   | j d  D } t   } x1 | D]) } | j d d   | j d	  D  qu W f d
 d   } | d | d  | d | d  | d | d  | d | d  x | D] } t j d |  q W j 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    sem-ver:c                s;   g  |  ]1 } | j    j d   r |   d  j    q S)z    sem-ver:N)lowerrD   r   )r   rM   )
header_lenr   r   r2     s   	 z)_get_increment_kwargs.<locals>.<listcomp>r+   c             S   s   g  |  ] } | j     q Sr   )r   )r   symbolr   r   r   r2     s   	 r   c                s'   |  | k r# d   | <| j  |   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_commandrn   r   setupdater   rL   pop)	git_dirtagZversion_specr   re   r   r   r   r   r   )r   r   r   _get_increment_kwargs  s&    "	'r   c             C   s   t  j d |   } d } x t |  D] \ } \ } } } t   } xF t |  D]8 } y | j t j j |   WqP t	 k
 r YqP XqP W| r% t
 |  j   | f Sq% Wd | f S)zReturn 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   addr   SemanticVersionfrom_pip_string	Exceptionmaxrelease_string)r   r   Z	row_countZignoredZtag_setZversion_tagsr   r   r   r   _get_revno_and_last_tag  s    "		r   c             C   s   t  |   \ } } t j j | p$ d  } | d k r? | } n | j t |  |    } | d k	 r | | k r t d t d | d |    | d k r | S| j |  } | d k	 r | j |  } | | 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   rJ   ro   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  j   } | r y@ t  j d d g | d d j d d  } t j j |  } Wn3 t k
 r |  r} t j j |   } n d } Yn Xt | |  } | j	   Sy t
   SWn t k
 r d SYn Xd 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-matchZthrow_on_errorT-rk   Nr*   )r   Z_run_git_functionsr   replacer   r   r   r   r   r   Zunicode	NameError)pre_versionr   Ztaggedr   r   r   r   r   _get_version_from_git>  s$    	
r   c             C   s   d d g } i  } xm | D]e } y t  | d  } Wn t t f k
 rO w Yn Xy t j |  } Wq t j k
 r} w Yq Xq W| j d d  |  k r d S| j d d  S)zGet 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METADATAr4   NameNZVersion)r5   IOErrorOSErroremailZmessage_from_fileZMessageErrorr   )package_nameZpkg_metadata_filenamesZpkg_metadatafilenameZpkg_metadata_filer   r   r   _get_version_from_pkg_metadatad  s    r   c             C   s   t  j j d t  j j d d   } | r. | St |   } | rD | St |  } t j d d k rr | j d  } | r| | St 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   rA   zutf-8zVersioning for this project requires either an sdist tarball, or access to an upstream git repository. Are you sure that git is installed?)	r   r   r   r   r   r"   r#   encoder   )r   r   r   r   r   r   get_version|  s    	r   )r   r   )r   r   )LrX   Z
__future__r   Zdistutils.commandr   r[   r   r   r   r   r:   r"   rG   Z
setuptoolsZsetuptools.commandr   r   r   r   r	   r   r
   r   r   Zpbr.pbr_jsonr   r   r'   ZTEST_REQUIREMENTS_FILESr)   r0   r3   r8   rF   rO   rP   rZ   r^   r\   ZCommandr_   rd   Znosere   rg   rf   rh   ImportErrorri   Z
_wsgi_textZ_script_textrl   r%   normpathrq   ru   rv   r|   Zmanifest_makerr   r   rQ   r   r   r   ZLocalBuildDocZLocalBuildLatexr   r   r   r   r   r   r   Zpbr_jsonZwrite_pbr_jsonr   r   r   r   <module>   s   
@
	
'!(!	
	%%&)