
    k$                        S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SrSr	S	r
S
R                  \\
5      rSR                  \5      rSR                  \	5      rSR                  \\\S9r " S S\5      r " S S\5      r " S S5      rg)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                       \ rS rSrSrSrg)
ParseErrorD   zAAn exception for when a string failed to parse as a valid semver. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       &lib/googlecloudsdk/core/util/semver.pyr   r   D   s    Ir   r   c                   ~    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	S r
S rS	 rS
 rS rS rS rS rSrg)SemVerI   z0Object to hold a parsed semantic version string.c                 l    [         R                  U5      u  U l        U l        U l        U l        U l        g)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versions     r   __init__SemVer.__init__L   s,     	7# FTZTZ$*r   c                    Uc  [        S5      e [        R                  " [        U5      nU(       d  [        SR                  U5      5      eUR                  5       n[        US   5      [        US   5      [        US   5      US   US	   4$ ! [        [        R
                  4 a  n[        SR                  X5      5      eSnAff = f)
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partss        r   r   SemVer._FromString[   s     GHH,hhw(e
 
9
@
@
IK K OOEE'NSw0#eGn2ElU7^- - rxx  ,Bw*, ,,s   B C'CCc                     X:  X:  -
  $ )z;Just a helper equivalent to the cmp() function in Python 2.r   )r,   xys      r   
_CmpHelperSemVer._CmpHelperp   s     Eaer   c                    U(       a  UR                  S5      O/ nU(       a  UR                  S5      O/ n[        X5       H  u  p4Uc    gUc    gX4:X  a  M  UR                  5       (       a?  UR                  5       (       a*  [        R	                  [        U5      [        U5      5      s  $ [        R	                  UR                  5       UR                  5       5      s  $    g)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.
.   r   )splitr   isdigitr   r3   r+   lower)r,   s1s2thisothers        r   _ComparePrereleaseStrings SemVer._ComparePrereleaseStringsu   s     #"B#"B$R, 
= 
	EMMOO  TCJ77 tzz|U[[];;# -& r   c                    [         R                  U R                  U R                  U R                  4UR                  UR                  UR                  45      nU=(       d*    [         R                  U R                  UR                  5      nU$ )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   r3   r   r   r   r@   r   )r   r?   results      r   _CompareSemVer._Compare   sm     	TZZ,	ekk5;;/1F
  +v77))+FMr   c                     U R                   UR                   -
  nU R                  UR                  -
  nU R                  UR                  -
  nX#U4$ )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   r?   
major_diff
minor_diff
patch_diffs        r   DistanceSemVer.Distance   sE     ekk)Jekk)Jekk)J:--r   c                     U=(       as    U R                   U R                  U R                  U R                  U R                  4UR                   UR                  UR                  UR                  UR                  4:H  $ N)r   r   r   r   r	   r   r?   s     r   __eq__SemVer.__eq__   s]     P	TZZT__djjI	ekk5;;0@0@%++N	OPr   c                     X:X  + $ rM   r   rN   s     r   __ne__SemVer.__ne__   s    r   c                 *    U R                  U5      S:  $ Nr   rD   rN   s     r   __gt__SemVer.__gt__       ==!##r   c                 *    U R                  U5      S:  $ rU   rV   rN   s     r   __lt__SemVer.__lt__   rY   r   c                     X:  + $ rM   r   rN   s     r   __ge__SemVer.__ge__       r   c                     X:  + $ rM   r   rN   s     r   __le__SemVer.__le__   r`   r   )r	   r   r   r   r   N)r   r   r   r   r   r!   classmethodr   r3   r@   rD   rJ   rO   rR   rW   r[   r^   rb   r   r   r   r   r   r   I   sp    8% - -(      D&.P
$$r   r   c                       \ rS rSrSr\R                  " S\R                  5      rSS jr	S r
S rS rS	 rS
 rS rS rS rS rSrg)LooseVersion   ag  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                 6    U(       a  U R                  U5        g g rM   )parse)r   vstrings     r   r!   LooseVersion.__init__   s    
jj r   c                     U R                   $ rM   )rj   r   s    r   __str__LooseVersion.__str__   s    <<r   c                     S[        U 5      -  $ )NzLooseVersion('%s'))strrm   s    r   __repr__LooseVersion.__repr__   s    #d)++r   c                    Xl         U R                  R                  U5       Vs/ s H  nU(       d  M  US:w  d  M  UPM     nn[        U5       H  u  pE [	        U5      X4'   M     X0l        gs  snf ! [
         a     M0  f = f)zInstantiate self from string.r6   N)rj   component_rer9   	enumerater+   
ValueErrorr    )r   rj   r1   
componentsiobjs         r   ri   LooseVersion.parse   s    
 L!..44W= %= 8 =J %J'C
 ( L%
  s!   A,A,A,A11
A?>A?c                 D    U R                  U5      nU[        L a  U$ US:H  $ rU   _cmpNotImplementedr   r?   cs      r   rO   LooseVersion.__eq__  &    		%ANh6Mr   c                 D    U R                  U5      nU[        L a  U$ US:  $ rU   r}   r   s      r   r[   LooseVersion.__lt__  &    		%ANhq5Lr   c                 D    U R                  U5      nU[        L a  U$ US:*  $ rU   r}   r   s      r   rb   LooseVersion.__le__  r   r   c                 D    U R                  U5      nU[        L a  U$ US:  $ rU   r}   r   s      r   rW   LooseVersion.__gt__  r   r   c                 D    U R                  U5      nU[        L a  U$ US:  $ rU   r}   r   s      r   r^   LooseVersion.__ge__$  r   r   c                    [        U[        5      (       a  [        U5      nO[        U[        5      (       d  [        $ U R                  UR                  :X  a  gU R                  UR                  :  a  gU R                  UR                  :  a  gg)zCompare self with other.r   r8   r7   N)
isinstancerq   rf   r   r    rN   s     r   r~   LooseVersion._cmp*  sj    %5!e|,,||u}}$||emm#||emm# $r   )r    rj   rM   )r   r   r   r   r   r$   compileVERBOSEru   r!   rn   rr   ri   rO   r[   rb   rW   r^   r~   r   r   r   r   rf   rf      sQ    B 2BJJ?,, r   rf   )r   
__future__r   r   r   r$   	six.movesr   _DIGITS
_ALPHA_NUM_STRICT_ALPHA_NUMr)   _PRE_RELEASE_IDENTIFIER_PRE_RELEASE_BUILDr&   	Exceptionr   objectr   rf   r   r   r   <module>r      s    2 '  ' 	 ! 
 < (//9JK #**+BC		$	$Z	0B&V&<  
BV BJh hr   