B
    '\/                 @   s  d dl mZmZmZmZ d dlZd dlmZ d dlmZm	Z	 d dl
Z
d dlZddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ yd dlmZmZmZmZ W n, ek
r   d dlmZmZmZmZ Y nX dd Zedd Z 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*e*Z+d$d% Z,d&d' Z-d(d) Z.d*d+ Z/d,d- Z0d.d/ Z1d0d1 Z2d2d3 Z3d4d5 Z4ed6d7 Z5d8d9 Z6d@d;d<Z7d=d> Z8e9d?krd dl:Z:e:;  dS )A    )absolute_importdivisionprint_functionunicode_literalsN)getpass)abspath
expanduser   )inputon_win)split_filename   )memoize)LocationParseError)Url	parse_url)quote
quote_plusunquoteunquote_plusc             C   sd   t | } t d}t d}t d}t d}| |kr@| |kr@| | n| |kr\| |kr\| | d nd }|S )N09AF
   )ord)ZhoZo0Zo9ZoAZoFZres r   /lib/python3.7/site-packages/conda/common/url.pyhex_octal_to_int   s    8r   c       
   
   C   s   d| kr| S g }x*t d| D ]}|| | f qW t|sH| S d}d}xt| D ]\}}|dkrt|d8 }qZ|d}|}|dkrxR|D ]J}||d krdd l}	|		dt
| |d  d	 t
| |d
   }d
}P qW |rZ||7 }qZW t|d S )N%z(%[0-9A-F]{2})    r   r	   ascii   %B   r   )refinditerappendstartendlen	enumerateencodestructZpackr   codecsutf_8_decode)
pathZrangesmresultZskipsicemitrr-   r   r   r   percent_decode$   s4    

(r7   zfile://c                s   | st d|  | trNy| d W n  tk
rH   t d|  Y nX | S tt| dd} d  fdd}td	d
 | D r|t	| dr| dnt
| dd} | d dkrtd |  } nt|  } | S )NzNot allowed: %rr!   z8Non-ascii not allowed for things claiming to be URLs: %r\/z!'()*-._/\:c                s   d  fdd| D S )N c             3   s6   | ].}d t | |g|dk r$| p*| k V  qdS )z%%%02X{N)r   isalnum).0r4   )percent_encode_charsr   r   	<genexpr>n   s   z0path_to_url.<locals>.<lambda>.<locals>.<genexpr>)join)s)r>   r   r   <lambda>n   s   zpath_to_url.<locals>.<lambda>c             s   s   | ]}t |d kV  qdS )   N)r   )r=   charr   r   r   r?   q   s    zpath_to_url.<locals>.<genexpr>decodezunicode-escapezutf-8r	   :)
ValueError
startswithfile_schemerE   UnicodeDecodeErrorr   r   replaceanyhasattrbytes)r0   Zpercent_encoder   )r>   r   path_to_urlU   s&    
rO   c             C   s"   t r| dr| dd t| S )Nzfile:r8   r9   )r   rH   rK   r   )urlr   r   r   urlparse~   s    rQ   c             C   s4   t | }|jdkstd|  |j|j }}||fS )zConvert an s3 url to a tuple of bucket and key.

    Examples:
        >>> url_to_s3_info("s3://bucket-name.bucket/here/is/the/key")
        ('bucket-name.bucket', '/here/is/the/key')
    Zs3z"You can only use s3: urls (not %r))r   schemeAssertionErrorhostr0   )rP   Z
parsed_urlZbucketkeyr   r   r   url_to_s3_info   s    rV   c             C   s0   | sdS yt | jdk	S  tk
r*   dS X dS )zn
    Examples:
        >>> is_url(None)
        False
        >>> is_url("s3://some/bucket")
        True
    FN)rQ   rR   r   )rP   r   r   r   is_url   s    rW   c             C   s4   yt |  W n t jk
r$   dS X | ddkS )a  
    Examples:
        >>> [is_ipv4_address(ip) for ip in ('8.8.8.8', '192.168.10.10', '255.255.255.255')]
        [True, True, True]
        >>> [is_ipv4_address(ip) for ip in ('8.8.8', '192.168.10.10.20', '256.255.255.255', '::1')]
        [False, False, False, False]
    F.   )socketZ	inet_atonerrorcount)	string_ipr   r   r   is_ipv4_address   s
    r^   c             C   sP   y
t j}W n tk
r"   t| S X y|t j|  W n t jk
rJ   dS X dS )z
    Examples:
        >> [is_ipv6_address(ip) for ip in ('::1', '2001:db8:85a3::370:7334', '1234:'*7+'1234')]
        [True, True, True]
        >> [is_ipv6_address(ip) for ip in ('192.168.10.10', '1234:'*8+'1234')]
        [False, False]
    FT)rZ   	inet_ptonAttributeErroris_ipv6_address_win_py27ZAF_INET6r[   )r]   r_   r   r   r   is_ipv6_address   s    

rb   c             C   s   t tjd| tjtjB dS )z
    Examples:
        >>> [is_ipv6_address_win_py27(ip) for ip in ('::1', '1234:'*7+'1234')]
        [True, True]
        >>> [is_ipv6_address_win_py27(ip) for ip in ('192.168.10.10', '1234:'*8+'1234')]
        [False, False]
    z^(((?=.*(::))(?!.*\3.+\3))\3?|[\dA-F]{1,4}:)([\dA-F]{1,4}(\3|:\b)|\2){5}(([\dA-F]{1,4}(\3|:\b|$)|\2){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\Z)flags)boolr%   matchDOTALL
IGNORECASE)r]   r   r   r   ra      s    	ra   c             C   s   t | pt| S )z
    Examples:
        >> is_ip_address('192.168.10.10')
        True
        >> is_ip_address('::1')
        True
        >> is_ip_address('www.google.com')
        False
    )r^   rb   )r]   r   r   r   is_ip_address   s    
rh   c              G   s@   | d r| d  drdnd}|ddd dd | D D  S )Nr   r9   r:   c             s   s   | ]}|r|V  qd S )Nr   )r=   yr   r   r   r?      s    zjoin.<locals>.<genexpr>c             s   s   | ]}|r| d V  qdS )r9   N)strip)r=   xr   r   r   r?      s    )rH   r@   )argsr(   r   r   r   r@      s    r@   c             C   s   t d| S )Nz[a-z][a-z0-9]{0,11}://)r%   re   )valuer   r   r   
has_scheme   s    rn   c             C   s   |  ddd S )z
    Examples:
        >>> strip_scheme("https://www.conda.io")
        'www.conda.io'
        >>> strip_scheme("s3://some.bucket/plus/a/path.ext")
        'some.bucket/plus/a/path.ext'
    z://r	   )split)rP   r   r   r   strip_scheme   s    rq   c             C   s"   t | \}}|r| |ddS | S )Nz<TOKEN>r	   )split_anaconda_tokenrK   )rP   _tokenr   r   r   mask_anaconda_token   s    ru   c             C   sL   t d| }|r| d nd}|dk	r:| d| ddn| }|d|fS )a  
    Examples:
        >>> split_anaconda_token("https://1.2.3.4/t/tk-123-456/path")
        (u'https://1.2.3.4/path', u'tk-123-456')
        >>> split_anaconda_token("https://1.2.3.4/t//path")
        (u'https://1.2.3.4/path', u'')
        >>> split_anaconda_token("https://some.domain/api/t/tk-123-456/path")
        (u'https://some.domain/api/path', u'tk-123-456')
        >>> split_anaconda_token("https://1.2.3.4/conda/t/tk-123-456/path")
        (u'https://1.2.3.4/conda/path', u'tk-123-456')
        >>> split_anaconda_token("https://1.2.3.4/path")
        (u'https://1.2.3.4/path', None)
        >>> split_anaconda_token("https://10.2.3.4:8080/conda/t/tk-123-45")
        (u'https://10.2.3.4:8080/conda', u'tk-123-45')
    z/t/([a-zA-Z0-9-]*)r   Nz/t/r:   r	   r9   )r%   searchgroupsrK   rstrip)rP   Z_token_matchrt   cleaned_urlr   r   r   rr      s    rr   c             C   sh   dd dd |D  }t|| tj}|r8| d nd}|dk	rV| d| dd	n| }|d|fS )
z

    Examples:
        >>> from conda.base.constants import PLATFORM_DIRECTORIES
        >>> split_platform("https://1.2.3.4/t/tk-123/osx-64/path", PLATFORM_DIRECTORIES)
        (u'https://1.2.3.4/t/tk-123/path', u'osx-64')

    z/(%s)/?|c             s   s   | ]}d | V  qdS )z%sNr   )r=   dr   r   r   r?     s    z!split_platform.<locals>.<genexpr>r   Nr9   r:   r	   )r@   r%   rv   rg   rw   rK   rx   )rP   known_subdirsZ_platform_match_regexZ_platform_matchplatformry   r   r   r   split_platform  s
    	r~   c             C   s4   t | \}}|sd S |ddd }||kr0|p2d S )Nr9   r	   ro   )r   rsplit)rP   r|   Zurl_no_package_namers   Zmaybe_a_platformr   r   r   has_platform%  s
    r   c             C   s*   |  dr| ddn| d f\}}||fS )N)z.tar.bz2z.jsonr9   r	   )endswithr   )rP   ry   package_filenamer   r   r   _split_package_filename-  s    r   c             C   sF   | sdS t | \}}t|}t|j|j|j|jdj}||j|j	|fS )z
    Examples:
        >>> split_scheme_auth_token("https://u:p@conda.io/t/x1029384756/more/path")
        ('conda.io/more/path', 'https', 'u:p', 'x1029384756')
        >>> split_scheme_auth_token(None)
        (None, None, None, None)
    )NNNN)rT   portr0   query)
rr   rQ   r   rT   r   r0   r   rP   rR   auth)rP   ry   rt   	url_partsZremainder_urlr   r   r   split_scheme_auth_token3  s    r   c          	   C   sP   t | \}}t||\}}t|\}}t|}|j|j||||j|j|j|j	f	S )N)
rr   r~   r   rQ   rR   r   rT   r   r0   r   )rP   r|   ry   rt   r}   r   r   r   r   r   split_conda_url_easy_partsD  s    r   c             C   s   t d|  }td}||fS )Nz
%s proxy username: z
Password: )r
   r   )rR   usernameZpasswdr   r   r   get_proxy_username_and_passR  s    r   c             C   s.   t |  }|d t|d |d< tf |jS )NrF   r:   r   )r   _asdictr   r   rP   )rP   r   Zpasswordr   r   r   r   add_username_and_passwordY  s    r   Fc             C   s8   |s| S t |  }|d r$|s$| S ||d< tf |jS )a~  Add auth if the url doesn't currently have it.

    By default, does not replace auth if it already exists.  Setting ``force`` to ``True``
    overrides this behavior.

    Examples:
        >>> maybe_add_auth("https://www.conda.io", "user:passwd")
        'https://user:passwd@www.conda.io'
        >>> maybe_add_auth("https://www.conda.io", "")
        'https://www.conda.io'
    r   )rQ   r   r   rP   )rP   r   Zforcer   r   r   r   maybe_add_auth_  s    r   c             C   s   | rt | S | S )N)r   )rP   r   r   r   maybe_unquotet  s    r   __main__)F)<Z
__future__r   r   r   r   r.   r   os.pathr   r   r%   rZ   compatr
   r   r0   r   Z_vendor.auxlib.decoratorsr   Z_vendor.urllib3.exceptionsr   Z_vendor.urllib3.util.urlr   r   Zurllib.parser   r   r   r   ImportErrorZurllibr   r7   rI   rO   rQ   rV   rW   r^   rb   ra   rh   r@   Zjoin_urlrn   rq   ru   rr   r~   r   r   r   r   r   r   r   r   __name__ZdoctestZtestmodr   r   r   r   <module>   sV   
#)

