U
    ±OX=  ã                   @   sâ   d Z ddlmZmZ ddlZddlZddlZejdk r<dZndZe	Z
dd„ ZzeZW n ek
rp   d	d
„ ZY nX dd„ ZeG dd„ deƒƒZeƒ ZG dd„ deƒZeG dd„ deƒƒZdd„ Zdd„ ZddlmZ dddgZdS )z0
Versions for Python packages.

See L{Version}.
é    )ÚdivisionÚabsolute_importN)é   r   FTc                 C   s`   t | ttfƒstd|  ƒ‚tr>t | tƒr2|  d¡S |  d¡ nt | tƒrR|  d¡S |  d¡ | S )a	  
    Convert C{bytes} or C{unicode} to the native C{str} type, using ASCII
    encoding if conversion is necessary.

    @raise UnicodeError: The input string is not ASCII encodable/decodable.
    @raise TypeError: The input is neither C{bytes} nor C{unicode}.
    z%r is neither bytes nor unicodeÚascii)Ú
isinstanceÚbytesÚunicodeÚ	TypeErrorÚ_PY3ÚdecodeÚencode)Ús© r   ú6/usr/lib/python3/dist-packages/incremental/__init__.pyÚ_nativeString   s    




r   c                 C   s    | |k rdS | |krdS dS dS )z™
        Compare two objects.

        Returns a negative number if C{a < b}, zero if they are equal, and a
        positive number if C{a > b}.
        éÿÿÿÿr   é   Nr   )ÚaÚbr   r   r   Ú_cmp7   s
    r   c                 C   s`   t s| S dd„ }dd„ }dd„ }dd„ }d	d
„ }dd„ }|| _|| _|| _|| _|| _|| _| S )zü
    Class decorator that ensures support for the special C{__cmp__} method.

    On Python 2 this does nothing.

    On Python 3, C{__eq__}, C{__lt__}, etc. methods are added to the class,
    relying on C{__cmp__} to implement their comparisons.
    c                 S   s   |   |¡}|tkr|S |dkS ©Nr   ©Ú__cmp__ÚNotImplemented©ÚselfÚotherÚcr   r   r   Ú__eq__S   s    
z_comparable.<locals>.__eq__c                 S   s   |   |¡}|tkr|S |dkS r   r   r   r   r   r   Ú__ne__Y   s    
z_comparable.<locals>.__ne__c                 S   s   |   |¡}|tkr|S |dk S r   r   r   r   r   r   Ú__lt___   s    
z_comparable.<locals>.__lt__c                 S   s   |   |¡}|tkr|S |dkS r   r   r   r   r   r   Ú__le__e   s    
z_comparable.<locals>.__le__c                 S   s   |   |¡}|tkr|S |dkS r   r   r   r   r   r   Ú__gt__k   s    
z_comparable.<locals>.__gt__c                 S   s   |   |¡}|tkr|S |dkS r   r   r   r   r   r   Ú__ge__q   s    
z_comparable.<locals>.__ge__)r
   r    r"   r!   r#   r   r   )Úklassr   r   r    r!   r"   r#   r   r   r   Ú_comparableF   s    
r%   c                   @   s   e Zd ZdZdd„ ZdS )Ú_infz:
    An object that is bigger than all other objects.
    c                 C   s   |t krdS dS )z‘
        @param other: Another object.
        @type other: any

        @return: 0 if other is inf, 1 otherwise.
        @rtype: C{int}
        r   r   )r&   )r   r   r   r   r   r   ‰   s    z_inf.__cmp__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r&   „   s   r&   c                   @   s   e Zd ZdZdS )ÚIncomparableVersionsz-
    Two versions could not be compared.
    N)r'   r(   r)   r*   r   r   r   r   r+   ˜   s   r+   c                   @   sž   e Zd ZdZd#dd„Zedd„ ƒZdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZeZeZdd„ Zdd „ Zd!d"„ ZdS )$ÚVersiona*  
    An encapsulation of a version for a project, with support for outputting
    PEP-440 compatible version strings.

    This class supports the standard major.minor.micro[rcN] scheme of
    versioning, with support for "local versions" which may include a SVN
    revision or Git SHA1 hash.
    Nc                 C   sv   |r|rt dƒ‚n|r.|s.|}tjdtdd |dkrN|sF|sF|sF|rNt dƒ‚|| _|| _|| _|| _|| _|| _	dS )aŠ  
        @param package: Name of the package that this is a version of.
        @type package: C{str}
        @param major: The major version number.
        @type major: C{int} or C{str} (for the "NEXT" symbol)
        @param minor: The minor version number.
        @type minor: C{int}
        @param micro: The micro version number.
        @type micro: C{int}
        @param release_candidate: The release candidate number.
        @type release_candidate: C{int}
        @param prerelease: The prerelease number. (Deprecated)
        @type prerelease: C{int}
        @param dev: The development release number.
        @type dev: C{int}
        z Please only return one of these.zvPassing prerelease to incremental.Version was deprecated in Incremental 16.9.0. Please pass release_candidate instead.é   ©Ú
stacklevelÚNEXTz;When using NEXT, all other values except Package must be 0.N)
Ú
ValueErrorÚwarningsÚwarnÚDeprecationWarningÚpackageÚmajorÚminorÚmicroÚrelease_candidateÚdev)r   r5   r6   r7   r8   r9   Ú
prereleaser:   r   r   r   Ú__init__¨   s"    
 ýzVersion.__init__c                 C   s   t jdtddf | jS )NzuAccessing incremental.Version.prerelease was deprecated in Incremental 16.9.0. Use Version.release_candidate instead.r-   r.   )r2   r3   r4   r9   ©r   r   r   r   r;   Ï   s
     ýzVersion.prereleasec                 C   sB   |   ¡ }|  ¡ }|s2|  ¡ }|r>|dt|ƒ 7 }n|d| 7 }|S )zx
        Return a string in canonical short version format,
        <major>.<minor>.<micro>[+rSVNVer/+gitsha1].
        z+rú+)ÚbaseÚ_getGitVersionÚ_getSVNVersionr   )r   r   ÚgitverÚsvnverr   r   r   Úshort×   s    zVersion.shortc                 C   s   |   ¡ S )aT  
        Return a PEP440-compatible "local" representation of this L{Version}.

        This includes a SVN revision or Git commit SHA1 hash, if available.

        Examples:

          - 14.4.0+r1223
          - 1.2.3rc1+rb2e812003b5d5fcf08efd1dffed6afa98d44ac8c
          - 12.10.1
          - 3.4.8rc2
          - 11.93.0rc1dev3
        )rD   r=   r   r   r   Úlocalç   s    zVersion.localc                 C   s   |   ¡ S )zÆ
        Return a PEP440-compatible "public" representation of this L{Version}.

        Examples:

          - 14.4.0
          - 1.2.3rc1
          - 14.2.1rc1dev9
          - 16.04.0dev0
        )r?   r=   r   r   r   Úpublic÷   s    zVersion.publicc                 C   s`   | j dkr| j S | jdkr d}nd| jf }| jdkr<d}nd| jf }d| j | j| j||f S )zF
        Like L{short}, but without the +rSVNVer or @gitsha1.
        r0   NÚ zrc%szdev%sz%r.%d.%d%s%s)r6   r9   r:   r7   r8   )r   Zrcr:   r   r   r   r?     s    


 ýzVersion.basec              	   C   sˆ   |   ¡ }|rd| }|  ¡ }|r(d| }| jd kr8d}nd| jf }| jd krTd}nd| jf }d| jj| j| j| j| j	|||p‚|f S )Nz  #rG   z, release_candidate=%rz, dev=%rz%s(%r, %r, %d, %d%s%s)%s)
Ú_formatGitVersionÚ_formatSVNVersionr9   r:   Ú	__class__r'   r5   r6   r7   r8   )r   rB   rC   r9   r:   r   r   r   Ú__repr__  s0    
ÿ
øzVersion.__repr__c                 C   s   d| j |  ¡ f S )Nz[%s, version %s]©r5   rD   r=   r   r   r   Ú__str__:  s    þzVersion.__str__c           	      C   sæ   t || jƒstS | j ¡ |j ¡ kr8td| j|jf ƒ‚| jdkrHt}n| j}| jdkr^t}n| j}| j	dkrtt}n| j	}|jdkrŠt}n|j}|jdkr t}n|j}|j	dkr¶t}n|j	}t
|| j| j||f||j|j||fƒ}|S )a‡  
        Compare two versions, considering major versions, minor versions, micro
        versions, then release candidates. Package names are case insensitive.

        A version with a release candidate is always less than a version
        without a release candidate. If both versions have release candidates,
        they will be included in the comparison.

        @param other: Another version.
        @type other: L{Version}

        @return: NotImplemented when the other object is not a Version, or one
            of -1, 0, or 1.

        @raise IncomparableVersions: when the package names of the versions
            differ.
        z%r != %rr0   N)r   rJ   r   r5   Úlowerr+   r6   r&   r9   r:   r   r7   r8   )	r   r   r6   r9   r:   Z
othermajorZotherrcZotherdevÚxr   r   r   r   ?  sL    
ÿ





üüû
zVersion.__cmp__c              
   C   sŽ   t j t j |d¡¡}t|dƒ}| ¡  ¡ }W 5 Q R X | d¡rŠtt j t j || d¡d ¡¡ƒ}| ¡ }| ¡ W  5 Q R £ S Q R X |S )NZHEADÚrzref: ú r   )	ÚosÚpathÚabspathÚjoinÚopenÚreadÚstripÚ
startswithÚsplit)r   Z	directoryZheadFileÚfZheadContentZcommitr   r   r   Ú_parseGitDir  s    
ÿÿzVersion._parseGitDirc                 C   s¦   t j | j¡}|r¢tj |j¡}tj tj 	|d¡¡}dt 
|¡krX|  tj 	|d¡¡S tj tj 	|d¡¡}||krzdS dt 
|¡krœ|  tj 	|d¡¡S |}qXdS )zQ
        Given a package directory, walk up and find the git commit sha.
        z..z.gitN)ÚsysÚmodulesÚgetr5   rR   rS   ÚdirnameÚ__file__rT   rU   Úlistdirr\   )r   ÚmodZbasepathZupOneZ	upOneMorer   r   r   r@   ‘  s    zVersion._getGitVersionc                 C   sP   ddl m} ||ƒj}|jD ].}t|dƒr| d¡}|dk	r| d¡  S qdS )zû
        Given a readable file object which represents a .svn/entries file in
        format version 4, return the revision as a string.  We do this by
        reading first XML element in the document that has a 'revision'
        attribute.
        r   )ÚparseÚgetAttributeZrevisionNr   )Zxml.dom.minidomrd   ZdocumentElementZ
childNodesÚhasattrre   r   )r   ÚentriesFilerd   ÚdocZnodeZrevr   r   r   Ú_parseSVNEntries_4ª  s    



zVersion._parseSVNEntries_4c                 C   s$   |  ¡  |  ¡  |  ¡  |  ¡  ¡ S )z‘
        Given a readable file object which represents a .svn/entries file in
        format version 8, return the revision as a string.
        )ÚreadlinerX   )r   rg   r   r   r   Ú_parseSVNEntries_8¹  s    zVersion._parseSVNEntries_8c           	   	   C   sê   t j | j¡}|rætj tj |j¡d¡}tj 	|¡s:dS tj |d¡}tj 	|¡rŽt
|dƒ}| ¡  ¡ }W 5 Q R X t| d| d¡ dƒ}n| j}|dkr dS tj |d¡}t
|dƒ}zz||ƒW ¢W S | ¡  X W n   Y dS X dS )	a?  
        Figure out the SVN revision number based on the existence of
        <package>/.svn/entries, and its contents. This requires discovering the
        format version from the 'format' file and parsing the entries file
        accordingly.

        @return: None or string containing SVN Revision number.
        z.svnNÚformatÚrbZ_parseSVNEntries_r   s   UnknownÚentries)r]   r^   r_   r5   rR   rS   rU   r`   ra   ÚexistsrV   rW   rX   Úgetattrr   Ú_parseSVNEntriesTenPlusÚclose)	r   rc   ZsvnZ
formatFileZfObjrl   Úparserrg   rn   r   r   r   rA   È  s0    	þ
zVersion._getSVNVersionc                 C   s   |   ¡ }|d krdS d|f S )NrG   z
 (SVN r%s))rA   ©r   Zverr   r   r   rI   ò  s    zVersion._formatSVNVersionc                 C   s   |   ¡ }|d krdS d|f S )NrG   z	 (Git %s))r@   rt   r   r   r   rH   ø  s    zVersion._formatGitVersion)NNN)r'   r(   r)   r*   r<   Úpropertyr;   rD   rE   rF   r?   rK   rM   r   r\   r@   ri   rk   Z_parseSVNEntries_9rq   rA   rI   rH   r   r   r   r   r,   ž   s.       ÿ
'
 B*r,   c                 C   s   d| j |  ¡ f }|S )z®
    Get a friendly string for the given version object.

    @param version: A L{Version} object.
    @return: A string containing the package and short version number.
    z%s %srL   )ÚversionÚresultr   r   r   ÚgetVersionStringÿ  s    rx   c              
   C   sŒ   |sdS ddl m} | | ¡}| ¡  | ¡ D ]P}|d dkr.i }t|d ƒ}t| ¡ |ƒ W 5 Q R X |d  ¡ | j_	 dS q.t
dƒ‚dS )	zF
    Get the version from the package listed in the Distribution.
    Nr   )Úbuild_pyr   Ú_versionr-   Ú__version__zNo _version.py found.)Zdistutils.commandry   Zfinalize_optionsZfind_all_modulesrV   ÚexecrW   rF   Zmetadatarv   Ú	Exception)ZdistÚkeywordÚvaluery   Z
sp_commandÚitemZversion_filer[   r   r   r   Ú_get_version
  s    
r   r   )r{   r{   )r*   Z
__future__r   r   rR   r]   r2   Úversion_infor
   Ústrr   r   Zcmpr   Ú	NameErrorr%   Úobjectr&   r	   r+   r,   rx   r   rz   r{   Ú__all__r   r   r   r   Ú<module>   s4   
>  b