o
    k$                     @   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mZ dZdZ	d	Z
d
ee
ZdeZde	ZdjeeedZG dd deZG dd deZG dd dZdS )a  Helper functions for comparing semantic versions.

Basic rules of semver:

Format: major.minor.patch-prerelease+build

major, minor, patch, must all be present and integers with no leading zeros.
They are compared numerically by segment.

prerelease is an optional '.' separated series of identifiers where each is
either an integer with no leading zeros, or an alphanumeric string
(including '-'). Prereleases are compared by comparing each identifier in
order.  Integers are compared numerically, alphanumeric strings are compared
lexigraphically.  A prerelease version is lower precedence than it's associated
normal version.

The build number is optional and not included in the comparison.  It is '.'
separated series of alphanumeric identifiers.

Two SemVer objects are considered equal if they represent the exact same string
(including the build number and including case differences).  For comparison
operators, we follow the SemVer spec of precedence and ignore the build number
and case of alphanumeric strings.
    )absolute_import)division)unicode_literalsN)zip_longestz(?:0|[1-9][0-9]*)z[-0-9A-Za-z]+z$[-0-9A-Za-z]*[-A-Za-z]+[-0-9A-Za-z]*z(?:{0}|{1})z(?:{0}(?:\.{0})*)zx^(?P<major>{digits})\.(?P<minor>{digits})\.(?P<patch>{digits})(?:\-(?P<prerelease>{release}))?(?:\+(?P<build>{build}))?$)digitsreleasebuildc                   @   s   e Zd ZdZdS )
ParseErrorzAAn exception for when a string failed to parse as a valid semver.N)__name__
__module____qualname____doc__ r   r   </tmp/google-cloud-sdk/lib/googlecloudsdk/core/util/semver.pyr	   D   s    r	   c                   @   s|   e Zd ZdZdd Zedd Ze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S )SemVerz0Object to hold a parsed semantic version string.c                 C   s"   t |\| _| _| _| _| _dS )zCreates a SemVer object from the given version string.

    Args:
      version: str, The version string to parse.

    Raises:
      ParseError: If the version could not be correctly parsed.

    Returns:
      SemVer, The parsed version.
    N)r   _FromStringmajorminorpatch
prereleaser   )selfversionr   r   r   __init__L   s   zSemVer.__init__c              
   C   s   |du rt dztt|}W n ttjfy' } zt d||d}~ww |s1t d|| }t|d t|d t|d |d |d	 fS )
z.Parse the given version string into its parts.Nz.The value is not a valid SemVer string: [None]z)Error parsing version string: [{0}].  {1}z-The value is not a valid SemVer string: [{0}]r   r   r   r   r   )	r	   rematch_SEMVER	TypeErrorerrorformat	groupdictint)clsr   r   epartsr   r   r   r   [   s$   zSemVer._FromStringc                 C   s   ||k||k  S )z;Just a helper equivalent to the cmp() function in Python 2.r   )r!   xyr   r   r   
_CmpHelperp   s   zSemVer._CmpHelperc                 C   s   |r| dng }|r| dng }t||D ]6\}}|du r" dS |du r) dS ||kr.q| rB| rBtt|t|  S t| |   S dS )zCompares the two given prerelease strings.

    Args:
      s1: str, The first prerelease string.
      s2: str, The second prerelease string.

    Returns:
      1 if s1 is greater than s2, -1 if s2 is greater than s1, and 0 if equal.
    .N   r   )splitr   isdigitr   r&   r    lower)r!   s1s2thisotherr   r   r   _ComparePrereleaseStringsu   s   z SemVer._ComparePrereleaseStringsc                 C   s<   t | j| j| jf|j|j|jf}|pt | j|j}|S )zCompare this SemVer to other.

    Args:
      other: SemVer, the other version to compare this one to.

    Returns:
      1 if self > other, -1 if other > self, 0 if equal.
    )r   r&   r   r   r   r1   r   )r   r0   resultr   r   r   _Compare   s   
zSemVer._Comparec                 C   s.   | j |j  }| j|j }| j|j }|||fS )zCompare this SemVer to other and returns the distances.

    Args:
      other: SemVer, the other version to compare this one to.

    Returns:
      Distances between the major, minor and patch versions.
    )r   r   r   )r   r0   
major_diff
minor_diff
patch_diffr   r   r   Distance   s   	
zSemVer.Distancec                 C   s4   |o| j | j| j| j| jf|j |j|j|j|jfkS N)r   r   r   r   r   r   r0   r   r   r   __eq__   s
   zSemVer.__eq__c                 C   s
   | |k S r8   r   r9   r   r   r   __ne__      
zSemVer.__ne__c                 C   s   |  |dkS Nr   r3   r9   r   r   r   __gt__      zSemVer.__gt__c                 C   s   |  |dk S r=   r>   r9   r   r   r   __lt__   r@   zSemVer.__lt__c                 C   s
   | |k  S r8   r   r9   r   r   r   __ge__   r<   zSemVer.__ge__c                 C   s
   | |k S r8   r   r9   r   r   r   __le__   r<   zSemVer.__le__N)r
   r   r   r   r   classmethodr   r&   r1   r3   r7   r:   r;   r?   rA   rB   rC   r   r   r   r   r   I   s"    


"r   c                   @   sp   e Zd ZdZedejZd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S )LooseVersiona  Version numbering for anarchists and software realists.


  This is mostly copied from distutils.version.

  Implements the standard interface for version number classes as
  described above.  A version number consists of a series of numbers,
  separated by either periods or strings of letters.  When comparing
  version numbers, the numeric components will be compared
  numerically, and the alphabetic components lexically.  The following
  are all valid version numbers, in no particular order:
      1.5.1
      1.5.2b2
      161
      3.10a
      8.02
      3.4j
      1996.07.12
      3.2.pl0
      3.1.1.6
      2g6
      11g
      0.960923
      2.2beta29
      1.13++
      5.5.kw
      2.0b1pl0
  In fact, there is no such thing as an invalid version number under
  this scheme; the rules for comparison are simple and predictable,
  but may not always give the results you want (for some definition
  of "want").
  z(\d+ | [a-z]+ | \.)Nc                 C   s   |r	|  | d S d S r8   )parse)r   vstringr   r   r   r      s   zLooseVersion.__init__c                 C   s   | j S r8   )rG   r   r   r   r   __str__   s   zLooseVersion.__str__c                 C   s   dt |  S )NzLooseVersion('%s'))strrH   r   r   r   __repr__   s   zLooseVersion.__repr__c              	   C   sZ   || _ dd | j|D }t|D ]\}}zt|||< W q ty'   Y qw || _dS )zInstantiate self from string.c                 S   s   g | ]
}|r|d kr|qS )r'   r   ).0r$   r   r   r   
<listcomp>  s    z&LooseVersion.parse.<locals>.<listcomp>N)rG   component_rer*   	enumerater    
ValueErrorr   )r   rG   
componentsiobjr   r   r   rF      s   
zLooseVersion.parsec                 C   s   |  |}|tu r|S |dkS r=   _cmpNotImplementedr   r0   cr   r   r   r:        
zLooseVersion.__eq__c                 C   s   |  |}|tu r|S |dk S r=   rT   rW   r   r   r   rA     rY   zLooseVersion.__lt__c                 C   s   |  |}|tu r|S |dkS r=   rT   rW   r   r   r   rC     rY   zLooseVersion.__le__c                 C   s   |  |}|tu r|S |dkS r=   rT   rW   r   r   r   r?     rY   zLooseVersion.__gt__c                 C   s   |  |}|tu r|S |dkS r=   rT   rW   r   r   r   rB   $  rY   zLooseVersion.__ge__c                 C   sV   t |tr
t|}nt |tstS | j|jkrdS | j|jk r!dS | j|jkr)dS dS )zCompare self with other.r   r)   r(   N)
isinstancerJ   rE   rV   r   r9   r   r   r   rU   *  s   


zLooseVersion._cmpr8   )r
   r   r   r   r   compileVERBOSErN   r   rI   rK   rF   r:   rA   rC   r?   rB   rU   r   r   r   r   rE      s    !
rE   )r   
__future__r   r   r   r   	six.movesr   _DIGITS
_ALPHA_NUM_STRICT_ALPHA_NUMr   _PRE_RELEASE_IDENTIFIER_PRE_RELEASE_BUILDr   	Exceptionr	   objectr   rE   r   r   r   r   <module>   s&   

 