B
    lz]_              
   @   sn  d dl mZmZmZmZ d dlmZ d dlZd dl	Z	ddl
mZ ddlmZmZmZmZmZ ddlmZ eeZdd	 Zd
d Ze	dZe	dZi ZG dd deZeeG dd deZdZ dd Z!d'ddZ"dd Z#e	dZ$e	dZ%ej&ej'ej(ej)ej*ej+dd dd e#d	Z,e-d Z.G d!d" d"eZ/eeG d#d$ d$e/Z0e0Z1eeG d%d& d&e/Z2dS )(    )absolute_importdivisionprint_functionunicode_literals)	getLoggerN   )excepts)string_typeszipzip_longest	text_typewith_metaclass)InvalidVersionSpecc             C   s   t | S )N)VersionOrder)version r   3lib/python3.7/site-packages/conda/models/version.pynormalized_version   s    r   c             C   s   t || S )N)VersionSpecmatch)Zvtestspecr   r   r   ver_eval   s    r   z^[\*\.\+!_0-9a-z]+$z([0-9]+|[*]+|[^0-9*]+)c                   s   e Zd Z fddZ  ZS )SingleStrArgCachingTypec                sh   t || r|S t |trTy
| j| S  tk
rP   tt| | }| j|< |S X ntt| |S d S )N)
isinstancer	   _cache_KeyErrorsuperr   __call__)clsargval)	__class__r   r   r   !   s    


z SingleStrArgCachingType.__call__)__name__
__module____qualname__r   __classcell__r   r   )r!   r   r      s   r   c               @   sl   e Zd ZdZi 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dS )r   ah  
    This class implements an order relation between version strings.
    Version strings can contain the usual alphanumeric characters
    (A-Za-z0-9), separated into components by dots and underscores. Empty
    segments (i.e. two consecutive dots, a leading/trailing underscore)
    are not permitted. An optional epoch number - an integer
    followed by '!' - can preceed the actual version string
    (this is useful to indicate a change in the versioning
    scheme itself). Version comparison is case-insensitive.

    Conda supports six types of version strings:

    * Release versions contain only integers, e.g. '1.0', '2.3.5'.
    * Pre-release versions use additional letters such as 'a' or 'rc',
      for example '1.0a1', '1.2.beta3', '2.3.5rc3'.
    * Development versions are indicated by the string 'dev',
      for example '1.0dev42', '2.3.5.dev12'.
    * Post-release versions are indicated by the string 'post',
      for example '1.0post1', '2.3.5.post2'.
    * Tagged versions have a suffix that specifies a particular
      property of interest, e.g. '1.1.parallel'. Tags can be added
      to any of the preceding four types. As far as sorting is concerned,
      tags are treated like strings in pre-release versions.
    * An optional local version string separated by '+' can be appended
      to the main (upstream) version string. It is only considered
      in comparisons when the main versions are equal, but otherwise
      handled in exactly the same manner.

    To obtain a predictable version ordering, it is crucial to keep the
    version number scheme of a given package consistent over time.
    Specifically,

    * version strings should always have the same number of components
      (except for an optional tag suffix or local version string),
    * letters/strings indicating non-release versions should always
      occur at the same position.

    Before comparison, version strings are parsed as follows:

    * They are first split into epoch, version number, and local version
      number at '!' and '+' respectively. If there is no '!', the epoch is
      set to 0. If there is no '+', the local version is empty.
    * The version part is then split into components at '.' and '_'.
    * Each component is split again into runs of numerals and non-numerals
    * Subcomponents containing only numerals are converted to integers.
    * Strings are converted to lower case, with special treatment for 'dev'
      and 'post'.
    * When a component starts with a letter, the fillvalue 0 is inserted
      to keep numbers and strings in phase, resulting in '1.1.a1' == 1.1.0a1'.
    * The same is repeated for the local version part.

    Examples:

        1.2g.beta15.rc  =>  [[0], [1], [2, 'g'], [0, 'beta', 15], [0, 'rc']]
        1!2.15.1_ALPHA  =>  [[1], [2], [15], [1, '_alpha']]

    The resulting lists are compared lexicographically, where the following
    rules are applied to each pair of corresponding subcomponents:

    * integers are compared numerically
    * strings are compared lexicographically, case-insensitive
    * strings are smaller than integers, except
    * 'dev' versions are smaller than all corresponding versions of other types
    * 'post' versions are greater than all corresponding versions of other types
    * if a subcomponent has no correspondent, the missing correspondent is
      treated as integer 0 to ensure '1.1' == '1.1.0'.

    The resulting order is:

           0.4
         < 0.4.0
         < 0.4.1.rc
        == 0.4.1.RC   # case-insensitive comparison
         < 0.4.1
         < 0.5a1
         < 0.5b3
         < 0.5C1      # case-insensitive comparison
         < 0.5
         < 0.9.6
         < 0.960923
         < 1.0
         < 1.1dev1    # special case 'dev'
         < 1.1a1
         < 1.1.0dev1  # special case 'dev'
        == 1.1.dev1   # 0 is inserted before string
         < 1.1.a1
         < 1.1.0rc1
         < 1.1.0
        == 1.1
         < 1.1.0post1 # special case 'post'
        == 1.1.post1  # 0 is inserted before string
         < 1.1post1   # special case 'post'
         < 1996.07.12
         < 1!0.4.1    # epoch increased
         < 1!3.1.1.6
         < 2!0.4.1    # epoch increased again

    Some packages (most notably openssl) have incompatible version conventions.
    In particular, openssl interprets letters as version counters rather than
    pre-release identifiers. For openssl, the relation

      1.0.1 < 1.0.1a   =>   True   # for openssl

    holds, whereas conda packages use the opposite ordering. You can work-around
    this problem by appending a dash to plain version numbers:

      1.0.1a  =>  1.0.1post.a      # ensure correct ordering for openssl
    c       	      C   s"  |    }|dkr"t|dt| }|rZd|krZd|krZ|dd}t| }|rht|d|| _d| _|	d}t
|dkrd	g}n8t
|d
kr|d  st|d|d g}n
t|d|d 	d}t
|dkrg | _n2t
|d
kr|d dd	d| _n
t|d||d dd	d | _x| j| jfD ]}xtt
|D ]}t|| }|s|t|dxhtt
|D ]X}||  rt|| ||< n2|| dkrtd||< n|| dkrd||< qW || d  r|||< n| jg| ||< qZW qHW d S )N zempty version string-_zinvalid character(s)r   !   0r   zepoch must be an integerzduplicated epoch separator '!'+.z&duplicated local version separator '+'zempty version componentZpostinfZdevZDEV)striprstriplowerr   version_check_rer   replacenorm_version	fillvaluesplitlenisdigitlocalr   rangeversion_split_refindallintfloat)	selfZvstrr   ZinvalidZepochvkcjr   r   r   __init__   sR    







zVersionOrder.__init__c             C   s   | j S )N)r5   )r@   r   r   r   __str__   s    zVersionOrder.__str__c             C   s   d| j j| f S )Nz%s("%s"))r!   r"   )r@   r   r   r   __repr__   s    zVersionOrder.__repr__c             C   sJ   xDt ||g dD ]2\}}x(t ||| jdD ]\}}||kr*dS q*W qW dS )N)r6   FT)r   r6   )r@   t1t2v1v2c1c2r   r   r   _eq   s
    zVersionOrder._eqc             C   s    |  | j|jo|  | j|jS )N)rN   r   r:   )r@   otherr   r   r   __eq__   s    zVersionOrder.__eq__c       	      C   s   |j r(| | j|jsdS | j }|j }n| j}|j}t|d }| |d | |d | s`dS t||krpg n|| }|| }t|d }| |d | g|d | gsdS t||kr| jn|| }|| }t|trt|to||S ||kS )NFr*   )r:   rN   r   r8   r6   r   r	   
startswith)	r@   rO   rH   rI   ntrJ   rK   rL   rM   r   r   r   rQ      s(     
zVersionOrder.startswithc             C   s
   | |k S )Nr   )r@   rO   r   r   r   __ne__  s    zVersionOrder.__ne__c             C   s   xt | j| jg|j|jgD ]|\}}xrt||g dD ]`\}}xVt||| jdD ]B\}}||krbqNn(t|tr|t|tsdS nt|trdS ||k S W q4W qW dS )N)r6   TF)r
   r   r:   r   r6   r   r	   )r@   rO   rH   rI   rJ   rK   rL   rM   r   r   r   __lt__  s    $


zVersionOrder.__lt__c             C   s   || k S )Nr   )r@   rO   r   r   r   __gt__"  s    zVersionOrder.__gt__c             C   s
   || k  S )Nr   )r@   rO   r   r   r   __le__%  s    zVersionOrder.__le__c             C   s
   | |k  S )Nr   )r@   rO   r   r   r   __ge__(  s    zVersionOrder.__ge__N)r"   r#   r$   __doc__r   rE   rF   rG   rN   rP   rQ   rS   rT   rU   rV   rW   r   r   r   r   r   .   s   mGr   z \s*\^[^$]*[$]|\s*[()|,]|[^()|,]+c                s   t tstttd }g  g  fdd}x|D ]}| }|dkrd|d d q<|dkr|d d q<|dkrd q<|dkr|d rd	 dkrtd
	  q< | q<W rtd  d S )aN  
    Examples:
        >>> treeify("1.2.3")
        '1.2.3'
        >>> treeify("1.2.3,>4.5.6")
        (',', '1.2.3', '>4.5.6')
        >>> treeify("1.2.3,4.5.6|<=7.8.9")
        ('|', (',', '1.2.3', '4.5.6'), '<=7.8.9')
        >>> treeify("(1.2.3|4.5.6),<=7.8.9")
        (',', ('|', '1.2.3', '4.5.6'), '<=7.8.9')
        >>> treeify("((1.5|((1.6|1.7), 1.8), 1.9 |2.0))|2.1")
        ('|', '1.5', (',', ('|', '1.6', '1.7'), '1.8', '1.9'), '2.0', '2.1')
        >>> treeify("1.5|(1.6|1.7),1.8,1.9|2.0|2.1")
        ('|', '1.5', (',', ('|', '1.6', '1.7'), '1.8', '1.9'), '2.0', '2.1')
    z(%s)c                s   xrd | krt  dk r(td }  }|d |krP|dd  n|f}  }|d |krv|dd  n|f} |f| |  qW d S )Nr,   r   zcannot join single expressionr   r*   )r8   r   popappend)ZcstoprC   rleft)outputspec_strstackr   r   	apply_opsI  s    
ztreeify.<locals>.apply_ops|(,z|()r,   zexpression must start with '('z(unable to convert to expression tree: %sr   )
r   r	   AssertionErrorrer=   VSPEC_TOKENSr0   rZ   r   rY   )r^   tokensr`   itemr   )r]   r^   r_   r   treeify2  s0    


rj   Fc                s   t | tr| d dkrLdt fdd| dd }|sB dkr|d| }n0dt fd	d| dd } dkr|d| }|S | S )
a  
    Examples:
        >>> untreeify('1.2.3')
        '1.2.3'
        >>> untreeify((',', '1.2.3', '>4.5.6'))
        '1.2.3,>4.5.6'
        >>> untreeify(('|', (',', '1.2.3', '4.5.6'), '<=7.8.9'))
        '(1.2.3,4.5.6)|<=7.8.9'
        >>> untreeify((',', ('|', '1.2.3', '4.5.6'), '<=7.8.9'))
        '(1.2.3|4.5.6),<=7.8.9'
        >>> untreeify(('|', '1.5', (',', ('|', '1.6', '1.7'), '1.8', '1.9'), '2.0', '2.1'))
        '1.5|((1.6|1.7),1.8,1.9)|2.0|2.1'
    r   ra   c                s   t |  d dS )Nr*   )depth)	untreeify)x)rk   r   r   <lambda>      zuntreeify.<locals>.<lambda>r*   Nz(%s)rc   c                s   t | d d dS )NTr*   )_inandrk   )rl   )rm   )rk   r   r   rn     ro   )r   tuplejoinmap)r   rp   rk   Zresr   )rk   r   rl   r  s    
 
 rl   c             C   s2   t | |o0| tdt|dd d S )Nr.   r,   )oprW   rQ   r   rr   r   r7   )rm   yr   r   r   compatible_release_operator  s    rv   z(^(=|==|!=|<=|>=|<|>|~=)(?![=<>!~])(\S+)$z
.*[()|,^$]c             C   s
   |  |S )N)rQ   )rm   ru   r   r   r   rn     ro   rn   c             C   s   |  | S )N)rQ   )rm   ru   r   r   r   rn     ro   )	z==z!=z<=z>=<>=z!=startswithz~=)ry   rw   rx   r)   ~c               @   s   e Zd Z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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#S )$BaseSpecc             C   s   || _ || _|| _d S )N)r^   	_is_exactr   )r@   r^   matcheris_exactr   r   r   rE     s    zBaseSpec.__init__c             C   s   | j S )N)r^   )r@   r   r   r   r     s    zBaseSpec.specc             C   s   | j S )N)r|   )r@   r   r   r   r~     s    zBaseSpec.is_exactc             C   s6   y
|j }W n  tk
r*   | |j }Y nX | j |kS )N)r   AttributeErrorr!   )r@   rO   Z
other_specr   r   r   rP     s
    
zBaseSpec.__eq__c             C   s   |  | S )N)rP   )r@   rO   r   r   r   rS     s    zBaseSpec.__ne__c             C   s
   t | jS )N)hashr   )r@   r   r   r   __hash__  s    zBaseSpec.__hash__c             C   s   | j S )N)r   )r@   r   r   r   rF     s    zBaseSpec.__str__c             C   s   d| j j| jf S )Nz%s('%s'))r!   r"   r   )r@   r   r   r   rG     s    zBaseSpec.__repr__c             C   s   | j S )N)r   )r@   r   r   r   	raw_value  s    zBaseSpec.raw_valuec             C   s   |   r| jpd S )N)r~   r   )r@   r   r   r   exact_value  s    zBaseSpec.exact_valuec             C   s
   t  d S )N)NotImplementedError)r@   rO   r   r   r   merge  s    zBaseSpec.mergec             C   s   t | j|S )N)boolregexr   )r@   r^   r   r   r   regex_match  s    zBaseSpec.regex_matchc             C   s   |  tt|| jS )N)operator_funcr   r   
matcher_vo)r@   r^   r   r   r   operator_match  s    zBaseSpec.operator_matchc                s   t  fdd| jD S )Nc             3   s   | ]}|  V  qd S )N)r   ).0s)r^   r   r   	<genexpr>  s    z%BaseSpec.any_match.<locals>.<genexpr>)anytup)r@   r^   r   )r^   r   	any_match  s    zBaseSpec.any_matchc                s   t  fdd| jD S )Nc             3   s   | ]}|  V  qd S )N)r   )r   r   )r^   r   r   r     s    z%BaseSpec.all_match.<locals>.<genexpr>)allr   )r@   r^   r   )r^   r   	all_match  s    zBaseSpec.all_matchc             C   s
   | j |kS )N)r   )r@   r^   r   r   r   exact_match  s    zBaseSpec.exact_matchc             C   s   dS )NTr   )r@   r^   r   r   r   always_true_match  s    zBaseSpec.always_true_matchN)r"   r#   r$   rE   propertyr   r~   rP   rS   r   rF   rG   r   r   r   r   r   r   r   r   r   r   r   r   r   r{     s"   r{   c                   s8   e Zd Zi Z fddZdd Zdd Zdd Z  ZS )	r   c                s(   |  |\}}}tt| ||| d S )N)get_matcherr   r   rE   )r@   vspec	vspec_strr}   r~   )r!   r   r   rE     s    zVersionSpec.__init__c             C   s  t |trt|rt|}t |tr|}|d dkr<| jn| j}tdd |dd  D }t|d ftdd |D  }|| _	|}d}|||fS t
| }|d dks|d	 d
kr|d dks|d	 d
krt|dt|| _| j}d}n|d tkrt|}|d kr t|d| \}	}
|
dd  dkr|	dkrV|
d d }
nX|	dkrr|
d d }
d}	n<|	dkrt|dn&td|
|
d d  |
d d }
yt|	 | _W n$ tk
r   t|d|	 Y nX t|
| _| j}|	dk}n|dkr| j}d}nd|dkr^|dddddd}d| }t|| _| j}d}n|d	 dkr|dd  dkr|d d	 d }|dd}
tj | _t|
| _| j}d}n4d|krtd | _t|| _| j}d}n
| j!}d}|||fS )Nr   ra   c             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r     s    z*VersionSpec.get_matcher.<locals>.<genexpr>r*   c             s   s   | ]}|j V  qd S )N)r   )r   tr   r   r   r     s    F^r,   $z0regex specs must start with '^' and end with '$'zinvalid operatorz.*)ry   z>=z!=z!=startswithz~=zinvalid operator with '.*'zUsing .* with relational operator is superfluous and deprecated and will be removed in a future version of conda. Your spec was {}, but conda is ignoring the .* and treating it as {}zinvalid operator: %sz==*r.   z\.r-   z\+z^(?:%s)$@T)"r   r	   regex_split_rer   rj   rq   r   r   rl   r   r   r0   r   rf   compiler   r   OPERATOR_STARTversion_relation_regroupslogZwarningformatOPERATOR_MAPr   r   r   r   r   r   r1   r4   rQ   r   )r@   r   Z
vspec_treeZ_matcherr   r   r}   r~   moperator_strvo_strZrxr   r   r   r     s    
 














zVersionSpec.get_matcherc             C   s,   t || jst| dt| j|jfS )Nrc   )r   r!   re   rr   sortedr   )r@   rO   r   r   r   r   B  s    zVersionSpec.mergec             C   s.   t || jstt| j|jf}dt|S )Nra   )r   r!   re   setr   rr   r   )r@   rO   optionsr   r   r   unionF  s    zVersionSpec.union)	r"   r#   r$   r   rE   r   r   r   r%   r   r   )r!   r   r     s
   Wr   c                   sT   e Zd Zi Z fddZdd Zdd Zdd Zed	d
 Z	dd Z
dd Z  ZS )BuildNumberMatchc                s(   |  |\}}}tt| ||| d S )N)r   r   r   rE   )r@   r   r   r}   r~   )r!   r   r   rE   V  s    zBuildNumberMatch.__init__c             C   s@  yt |}W n tk
r    Y nX | j}d}|||fS t| }|dkrV| j}d}n|drt|}|d kr|t	|d|
 \}}yt| | _W n" tk
r   t	|d| Y nX t|| _| j}|dk}nb|d d	ks|d
 dkr,|d d	ks
|d
 dkrt	|dt|| _| j}d}n
| j}d}|||fS )NTr   F)ry   rw   rx   r)   zinvalid operatorzinvalid operator: %sz==r   r   r,   r   z0regex specs must start with '^' and end with '$')r>   
ValueErrorr   r   r0   r   rQ   r   r   r   r   r   r   r   r   r   r   rf   r   r   r   )r@   r   r}   r~   r   r   r   r   r   r   r   r   Z  s@    






zBuildNumberMatch.get_matcherc             C   s&   | j |j kr td| j |j f | j S )Nz+Incompatible component merge:
  - %r
  - %r)r   r   )r@   rO   r   r   r   r     s    zBuildNumberMatch.mergec             C   s   t | j|jf}d|S )Nra   )r   r   rr   )r@   rO   r   r   r   r   r     s    zBuildNumberMatch.unionc             C   s   t tt| jS )N)r   r   r>   r   )r@   r   r   r   r     s    zBuildNumberMatch.exact_valuec             C   s
   t | jS )N)r   r   )r@   r   r   r   rF     s    zBuildNumberMatch.__str__c             C   s
   t | jS )N)r   r   )r@   r   r   r   rG     s    zBuildNumberMatch.__repr__)r"   r#   r$   r   rE   r   r   r   r   r   rF   rG   r%   r   r   )r!   r   r   R  s   *r   )Fr   )3Z
__future__r   r   r   r   Zloggingr   operatorrt   rf   Z_vendor.toolzr   Zcommon.compatr	   r
   r   r   r   
exceptionsr   r"   r   r   r   r   r3   r<   Zversion_cachetyper   objectr   rg   rj   rl   rv   r   r   rP   rS   rV   rW   rT   rU   r   	frozensetr   r{   r   ZVersionMatchr   r   r   r   r   <module>   sN   

   @


?k