U
    b                     @   s  d Z ddlmZmZ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mZmZ dd
lmZmZmZmZmZmZ ddlmZ ddlmZ ddl m!Z!m"Z"m#Z# ddl$Z$ee%Z&eddddgZ'edZ(d0ddZ)dd Z*dd Z+dd Z,dd Z-dd  Z.d!d" Z/d#d$ Z0G d%d& d&eZ1G d'd( d(e
Z2d1d+d,Z3e%d-kree$j4d. Z5e.ee5d/Z6e/e5e6 dS )2a<  
=====
Usage
=====

Method #1: auxlib.packaging as a run time dependency
---------------------------------------------------

Place the following lines in your package's main __init__.py

from auxlib import get_version
__version__ = get_version(__file__)



Method #2: auxlib.packaging as a build time-only dependency
----------------------------------------------------------


import auxlib

# When executing the setup.py, we need to be able to import ourselves, this
# means that we need to add the src directory to the sys.path.
here = os.path.abspath(os.path.dirname(__file__))
src_dir = os.path.join(here, "auxlib")
sys.path.insert(0, src_dir)

setup(
    version=auxlib.__version__,
    cmdclass={
        'build_py': auxlib.BuildPyCommand,
        'sdist': auxlib.SDistCommand,
        'test': auxlib.Tox,
    },
)



Place the following lines in your package's main __init__.py

from auxlib import get_version
__version__ = get_version(__file__)


Method #3: write .version file
------------------------------



Configuring `python setup.py test` for Tox
------------------------------------------

must use setuptools (distutils doesn't have a test cmd)

setup(
    version=auxlib.__version__,
    cmdclass={
        'build_py': auxlib.BuildPyCommand,
        'sdist': auxlib.SDistCommand,
        'test': auxlib.Tox,
    },
)


    )absolute_importdivisionprint_function)
namedtuple)build_py)sdist)convert_pathfnmatchcase)	getLogger)getenvlistdirremove)abspathdirname
expanduserisdirisfilejoin)compile)shlex_split_unicode)CalledProcessErrorPIPEPopenNResponsestdoutstderrrczT(?:[_-a-zA-Z]*)(?P<version>[a-zA-Z0-9.]+)(?:-(?P<post>\d+)-g(?P<hash>[0-9a-f]{7,}))$Tc              	   C   s   |d krt jnt|}tt| |ttd}| \}}|j}t	d
|| ||| |rt|dkrtt|| d
||t|d|dt|S )N)cwdr   r   z0{0} $  {1}
  stdout: {2}
  stderr: {3}
  rc: {4}r   zstdout: {0}
stderr: {1}zutf-8)sysprefixr   r   r   r   Zcommunicate
returncodelogdebugformatr   r   decodeint)Zcommandpathraise_on_errorpr   r   r    r*   5lib/python3.8/site-packages/conda/auxlib/packaging.pycallZ   s        r,   c              
   C   s@   t | d}t|r<t|d}|  W  5 Q R  S Q R X d S )N.versionr)r   r   openreadstrip)r'   Z	file_pathZfhr*   r*   r+   _get_version_from_version_filei   s    
r2   c              
   C   s   zt d| dd W n6 tk
rH } ztt| W Y d S d }~X Y nX t d| dd}|jdkrl|j S |jdkrd|j	 krd S |jdkrd|j	 krd S |jd	krt
d
tdd  t|j|jnt|j|jd S )Nzgit update-index --refreshF)r(   zgit describe --tags --longr      zno names foundznot a git repository   zgit not found on path: PATH={0}PATH)r,   r   r"   warnreprr   r   r1   r   lowererrorr$   r   )r'   eZresponser*   r*   r+   _git_describe_tagsp   s     


r;   c                 C   s>   t | }|dkrdS | \}}}|dkr0|S d|||S )zReturn a PEP440-compliant version derived from the git status.
    If that fails for any reason, return the changeset hash.
    N0z{0}.post{1}+{2})GIT_DESCRIBE_REGEXmatchgroupsr$   )tagmversionZpost_commithashr*   r*   r+   _get_version_from_git_tag   s
    
rD   c                 C   s   t | p
d}t|S )N )r;   rD   )r'   r@   r*   r*   r+   _get_version_from_git_clone   s    rF   c              
   C   s   t tt| }zt|p t|W S  tk
rX } ztt| W Y dS d}~X Y n2 t	k
r } zt
| W Y dS d}~X Y nX dS )a  Returns a version string for the current package, derived
    either from git or from a .version file.

    This function is expected to run in two contexts. In a development
    context, where .git/ exists, the version is pulled from git tags.
    Using the BuildPyCommand and SDistCommand classes for cmdclass in
    setup.py will write a .version file into any dist.

    In an installed context, the .version file written at dist build
    time is the source of version information.

    N)r   r   r   r2   rF   r   r"   r6   r7   	ExceptionZ	exception)Zdunder_filer'   r:   r*   r*   r+   get_version   s    
rH   c              	   C   s   t | d}t|s td|t|d}| }W 5 Q R X tt|D ]D}|| drld|||< qJ|| dsd|| krJd ||< qJt	d| t
| t|d	}|d
 td | W 5 Q R X d S )N__init__.pyzFile not found: {0}r.   __version__z__version__ = "{0}"
)zfrom auxlibzimport auxlibzauxlib.packagingzUPDATING {0}wrE   )r   r   AssertionErrorr$   r/   	readlinesrangelen
startswithprintr   writefilter)
target_dirrB   Ztarget_init_filefZ
init_linesqr*   r*   r+   write_version_into_init   s    


rW   c              	   C   sT   t | std| t| d}td|| t|d}|| W 5 Q R X d S )NzDirectory not found: {0}r-   zWRITING {0} with version {1}rK   )r   rL   r$   r   rQ   r/   rR   )rT   rB   Ztarget_filerU   r*   r*   r+   write_version_file   s
    
rX   c                   @   s   e Zd Zdd ZdS )BuildPyCommandc                 C   s@   t |  t| j| jjj}t|| jjj t	|| jjj d S N)
r   runr   Z	build_libdistributionmetadatanamerW   rB   rX   )selfrT   r*   r*   r+   r[      s    
zBuildPyCommand.runN)__name__
__module____qualname__r[   r*   r*   r*   r+   rY      s   rY   c                   @   s   e Zd Zdd ZdS )SDistCommandc                 C   sB   t | || t|| jjj}t|| jjj t|| jjj d S rZ   )	r   make_release_treer   r\   r]   r^   rW   rB   rX   )r_   Zbase_dirfilesrT   r*   r*   r+   rd      s    zSDistCommand.make_release_treeN)r`   ra   rb   rd   r*   r*   r*   r+   rc      s   rc   .r*   c                    s   g }t | dfg}|r|d\} }t| D ]P}t| |}d|kr,t|r,tt|dr,|||  |||| d f q,qt|ddg D ]  fdd|D }q|S )	NrE   r   rf   rI   Zez_setupZdistribute_setupc                    s   g | ]}t | s|qS r*   r	   ).0itempatr*   r+   
<listcomp>   s     
 z!find_packages.<locals>.<listcomp>)r   popr   r   r   r   appendlist)whereZexcludeoutstackr    r^   fnr*   ri   r+   find_packages   s    
rs   __main__   rI   )NT)rf   r*   )7__doc__Z
__future__r   r   r   collectionsr   Zdistutils.command.build_pyr   Zdistutils.command.sdistr   Zdistutils.utilr   Zfnmatchr
   Zloggingr   osr   r   r   os.pathr   r   r   r   r   r   rer   Zconda.auxlib.compatr   
subprocessr   r   r   r   r`   r"   r   r=   r,   r2   r;   rD   rF   rH   rW   rX   rY   rc   rs   argvrT   rB   r*   r*   r*   r+   <module>   s>   A 
		

