o
    VhxB                     @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlZd dlZd dlZd dlZd dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl"m$Z$ d dl"m%Z% d dl&m'Z' d dl&m(Z( erd dl)m*Z* e+e,Z-ej.ddG dd dZ/ej.G dd dZ0G dd dZ1da2de3d< d"d d!Z4dS )#    )annotationsN)commonprefix)Path)TYPE_CHECKING)Any)CacheControlAdapter)	FileCache)
user_agent)__version__)Config)ConsoleMessage)PoetryRuntimeError)PoetryError)REQUESTS_TIMEOUT)RETRY_AFTER_HEADER)STATUS_FORCELIST)HTTPAuthCredential)PasswordManager)IOT)frozenc                   @  sX   e Zd ZU ejddZded< ejddZded< ejddZded< e	dddZ
dS )RepositoryCertificateConfigN)defaultzPath | Nonecertclient_certTboolverify
repositorystrconfigConfig | Nonereturnc                 C  s   |r|nt  }|d| d|d| dd}|d| d}| t|tr-t|nd |r4t|nd t|tr>|dS ddS )Nzcertificates.z.verifyz.certTz.client-cert)r   r   r   )r   createget
isinstancer   r   r   )clsr   r   r   r    r%   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/poetry/utils/authenticator.pyr!   0   s   
z"RepositoryCertificateConfig.create)r   r   r   r   r    r   )__name__
__module____qualname__dataclassesfieldr   __annotations__r   r   classmethodr!   r%   r%   r%   r&   r   *   s   
 r   c                   @  sd   e Zd ZU ded< ded< ejddZded< ejddZded< dd
dZdddZ	dddZ
dS )AuthenticatorRepositoryConfigr   nameurlF)initnetlocpathr    Nonec                 C  s"   t j| j}|j| _|j| _d S N)urllibparseurlsplitr0   r2   r3   )self
parsed_urlr%   r%   r&   __post_init__J   s   z+AuthenticatorRepositoryConfig.__post_init__r   r   r   c                 C  s   t | j|S r5   )r   r!   r/   )r9   r   r%   r%   r&   certsO   s   z#AuthenticatorRepositoryConfig.certspassword_managerr   r   c                 C  s:   | | j}|jd ur|S |jr|j| j| j|jd}|S )Nusername)get_http_authr/   passworduse_keyringget_credentialr0   r2   r?   )r9   r=   
credentialr%   r%   r&   get_http_credentialsR   s   
z2AuthenticatorRepositoryConfig.get_http_credentialsNr    r4   )r   r   r    r   )r=   r   r    r   )r'   r(   r)   r,   r*   r+   r2   r3   r;   r<   rE   r%   r%   r%   r&   r.   C   s   
 

r.   c                   @  s(  e Zd ZddddejjfdWddZdXddZdYdZddZd[ddZ	d[ddZ
d\ddZd]ddZ	 d^d_d&d'Zd`d,d-Zdad.d/Zdad0d1Zdad2d3Zdbd7d8Z	dcddd:d;Zded<d=Zded>d?ZdfdAdBZdgdDdEZdhdGdHZedidJdKZd[dLdMZdjdNdOZdkdPdQZ	dcdldSdTZdkdUdVZdS )mAuthenticatorNFr   r   io	IO | Nonecache_id
str | Nonedisable_cacher   	pool_sizeintr    r4   c                 C  s   |pt  | _|| _i | _i | _i | _d | _t| j| _	|s*t
| jj|p%d d nd | _tjd d| j| _|| _tdt| _d S )N_default_cache_http)maxsizepoetry)r   r!   _config_io_sessions_for_netloc_credentials_certs_configured_repositoriesr   _password_managerr   repository_cache_directory_cache_control	functools	lru_cache_get_repository_config_for_urlget_repository_config_for_url
_pool_sizer	   r
   _user_agent)r9   r   rH   rJ   rL   rM   r%   r%   r&   __init__e   s.   
	zAuthenticator.__init__requests.Sessionc                 C  sN   t  }| j|jd< | jd u r|S t| j| jd}|d| |d| |S )Nz
User-Agent)cachepool_maxsizezhttp://zhttps://)requestsSessionra   headersr[   r   r`   mount)r9   sessionadapterr%   r%   r&   create_session   s   
zAuthenticator.create_sessionr0   c                 C  sL   |s|   S tj|}|j}|| jvr!td| |   | j|< | j| S )NzCreating new session for %s)rl   r6   r7   r8   r2   rU   loggerdebug)r9   r0   r:   r2   r%   r%   r&   get_session   s   

zAuthenticator.get_sessionc                 C  s$   | j  D ]
}|d ur|  qd S r5   )rU   valuesclose)r9   rj   r%   r%   r&   rq      s
   zAuthenticator.closec                 C  s   |    d S r5   )rq   r9   r%   r%   r&   __del__      zAuthenticator.__del__r   c                 C  s    | j d ur| j j|d d S d S )N)key)r[   deleter9   r0   r%   r%   r&   delete_cache   s   
zAuthenticator.delete_cachec                 C  sv   t j|}| |}|jd ur9|jd ur9t jj|jdd}t jj|jdd}|j d| d| d|j |j	 S |S )N )safez://:@)
r6   r7   urlparseget_credentials_for_urlr?   rA   quoteschemer2   r3   )r9   r0   parsedrD   r?   rA   r%   r%   r&   authenticated_url   s   
"zAuthenticator.authenticated_urlTmethodraise_for_statuskwargsr   requests.Responsec              
   K  s   | d}tj|||d}| |}|jd us|jd ur+tj|jp#d|jp'd|}| j|d}|	|}	| di }
| d}| 
|}| dpP|jpP|j}| dpX|j}|d urat|}t|trjt|n|}||	j|
|||}| d	t| d
dd}|| d}d }	 |dk}z|j|	fi |}W nJ tjjtfy } z:|rtj|}tjd|j d|d}|t d!d"d#d |t d|j$ ddd |W Y d }~nd }~ww |j%t&vs|r|r|'  |S |s|d7 }| (||}t)*d| t+,| qq)Nrh   )rh   ry   )r0   proxiesstreamr   r   timeoutallow_redirectsT)r   r   r      z&<error>All attempts to connect to <c1>z</> failed.</>)reason	exceptionzthe server is not responding to requests at the moment
the hostname cannot be resolved by your DNS
your network is not connected to the internet
z    - zProbable Causeswarningz'<b>Note:</> The path requested was <c1>z</>.)rn      z$Retrying HTTP request in %s seconds.)/r"   rf   Requestr~   r?   rA   authHTTPBasicAuthro   prepare_requestget_certs_for_urlr   r   r   r   r#   r   merge_environment_settingsr0   r   updatesend
exceptionsConnectionErrorOSErrorr6   r7   r8   r   r!   r2   appendr   indentmake_sectionwrapr3   status_coder   r   _get_backoffrm   rn   timesleepr   upper)r9   r   r0   r   r   rh   requestrD   rj   prepared_requestr   r   r<   r   r   settingssend_kwargsattemptrespis_last_attempter:   excdelayr%   r%   r&   r      s   









zAuthenticator.requestresponserequests.Response | Noner   floatc                 C  s*   |d ur|j td}|rt|S d| S )Nry   g      ?)rh   r"   r   r   )r9   r   r   retry_afterr%   r%   r&   r     s
   zAuthenticator._get_backoffc                 K     | j d|fi |S )Nr"   r   r9   r0   r   r%   r%   r&   r"        zAuthenticator.getc                 K  s    | dd | jd|fi |S )Nr   Fhead)
setdefaultr   r   r%   r%   r&   r     s   zAuthenticator.headc                 K  r   )Npostr   r   r%   r%   r&   r      r   zAuthenticator.postr   r.   r   c                 C  s.   |j }|| jvr|j| jd| j|< | j| S )N)r=   )r0   rV   rE   rY   )r9   r   ru   r%   r%   r&   _get_credentials_for_repository#  s   

z-Authenticator._get_credentials_for_repositoryexact_matchc                 C  sj   |  ||}|d ur| j|dnt }|jd u r3tj|}|j}| jj	|||j
d}t|j
|jdS |S )Nr   r>   )r?   rA   )r_   r   r   rA   r6   r7   r8   r2   rY   rC   r?   )r9   r0   r   r   rD   r:   r2   r%   r%   r&   _get_credentials_for_url1  s   
z&Authenticator._get_credentials_for_urlc                 C  sL   t j|}|jdvrt S d| }|| jvr!| |d| j|< | j| S )N>   httphttpszgit+T)r6   r7   r8   r   r   rV   r   )r9   r0   r:   ru   r%   r%   r&   get_credentials_for_git_urlI  s   



z)Authenticator.get_credentials_for_git_urlc                 C  s   t j|}|j}|| jvrCd|vr| || j|< n(|dd\}}d|v r-|ddn|df\}}tt j	|t j	|| j|< | j| S )Nr|   r   r{   ry   )
r6   r7   r8   r2   rV   r   rsplitsplitr   unquote)r9   r0   r:   r2   r   userrA   r%   r%   r&   r~   V  s   
 



z%Authenticator.get_credentials_for_urlr/   c                 C  s   | j |S r5   )rY   get_pypi_tokenr9   r/   r%   r%   r&   r   o  rt   zAuthenticator.get_pypi_tokenHTTPAuthCredential | Nonec                 C  s8   |dkr
t |d}n|| jvrd S | j| }| j|dS )Npypizhttps://upload.pypi.org/legacy/r   )r.   configured_repositoriesr   )r9   r/   r   r%   r%   r&   r@   r  s   

zAuthenticator.get_http_authr   c                 C  s.   |  dks|| jvrt S | j| | jS )Nr   )lowerr   r   r<   rS   r   r%   r%   r&   get_certs_for_repository~  s   z&Authenticator.get_certs_for_repository(dict[str, AuthenticatorRepositoryConfig]c                 C  sN   | j d u r$i | _ | jdg D ]}| jd| d}t||| j |< q| j S )Nrepositorieszrepositories.z.url)rX   rS   r"   r.   )r9   repository_namer0   r%   r%   r&   r     s   

z%Authenticator.configured_repositoriesc                 C  s   | j   i | _d S r5   )r_   cache_clearrV   rr   r%   r%   r&   reset_credentials_cache  s   

z%Authenticator.reset_credentials_cachec                 C  s   t ||| j|< |   d S r5   )r.   r   r   )r9   r/   r0   r%   r%   r&   add_repository  s   zAuthenticator.add_repositoryc                 C  s$   || j vr| || j |< | j | S r5   )rW   _get_certs_for_urlrw   r%   r%   r&   r     s   

zAuthenticator.get_certs_for_url$AuthenticatorRepositoryConfig | Nonec                   s   t j| g }g }| j D ]/}|r j|jkr|  S q|j jkr> j|js3t j|jfr9|	| q|	| q|rD|}n|rI|}nd S t
|dkrltd jddd |D  |j fdddd	 |d
 S )Nr   z0Multiple source configurations found for %s - %sz, c                 s  s    | ]}|j V  qd S r5   )r/   ).0cr%   r%   r&   	<genexpr>  s    z?Authenticator._get_repository_config_for_url.<locals>.<genexpr>c                   s   t t j| jgS r5   )lenr   r3   )r   r:   r%   r&   <lambda>  s    z>Authenticator._get_repository_config_for_url.<locals>.<lambda>T)ru   reverser   )r6   r7   r8   r   rp   r3   r2   
startswithr   r   r   rm   rn   joinsort)r9   r0   r   candidates_netloc_onlycandidates_path_matchr   
candidatesr%   r   r&   r^     s>   


z,Authenticator._get_repository_config_for_urlc                 C  s"   |  |}|r|j| jdS t S )N)r   )r_   r<   rS   r   )r9   r0   selectedr%   r%   r&   r     s   
z Authenticator._get_certs_for_url)r   r   rH   rI   rJ   rK   rL   r   rM   rN   r    r4   )r    rc   r5   )r0   rK   r    rc   rF   )r0   r   r    r4   )r0   r   r    r   )T)
r   r   r0   r   r   r   r   r   r    r   )r   r   r   rN   r    r   )r0   r   r   r   r    r   )r   r.   r    r   )F)r0   r   r   r   r    r   )r0   r   r    r   )r/   r   r    rK   )r/   r   r    r   )r/   r   r    r   )r    r   )r/   r   r0   r   r    r4   )r0   r   r    r   )r0   r   r   r   r    r   ) r'   r(   r)   rf   adaptersDEFAULT_POOLSIZErb   rl   ro   rq   rs   rx   r   r   r   r"   r   r   r   r   r   r~   r   r@   r   propertyr   r   r   r   r^   r   r%   r%   r%   r&   rG   d   sF    
 




U











)rG   zAuthenticator | None_authenticatorr    c                   C  s   t d u rt a t S r5   )r   rG   r%   r%   r%   r&   get_default_authenticator  s   r   )r    rG   )5
__future__r   r*   r\   loggingr   urllib.parser6   os.pathr   pathlibr   typingr   r   rf   requests.adaptersrequests.authrequests.exceptionscachecontrolr   cachecontrol.cachesr   requests_toolbeltr	   poetry.__version__r
   poetry.config.configr   poetry.console.exceptionsr   r   poetry.exceptionsr   poetry.utils.constantsr   r   r   poetry.utils.password_managerr   r   
cleo.io.ior   	getLoggerr'   rm   	dataclassr   r.   rG   r   r,   r   r%   r%   r%   r&   <module>   sN    

   j