U
    ©Ê¹j¬   ã                   @   sþ   d dl 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	m
Z
 d dlmZmZmZmZmZmZ dgZeeeef dœdd„Zeee d	œd
d„Zee eeeef dœdd„Zeedœdd„Zeeeeef f ddœdd„ZG dd„ dƒZdS )é    N)Ú	timedelta)ÚEventÚThread)ÚAnyÚCallableÚDictÚOptionalÚTupleÚUnionÚparse_rendezvous_endpoint)Ú
config_strÚreturnc                 C   s‚   i }|   ¡ } | s|S |  d¡}|D ]Z}| dd¡^}}|  ¡ }|sJtdƒ‚|r\|d   ¡ }nd}|sttd|› d�ƒ‚|||< q"|S )	z¨Extracts key-value pairs from a rendezvous configuration string.

    Args:
        config_str:
            A string in format <key1>=<value1>,...,<keyN>=<valueN>.
    ú,ú=é   zZThe rendezvous configuration string must be in format <key1>=<value1>,...,<keyN>=<valueN>.r   Nz%The rendezvous configuration option 'z' must have a value specified.)ÚstripÚsplitÚ
ValueError)r   ÚconfigÚ
key_valuesÚkvÚkeyÚvaluesÚvalue© r   úi/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/distributed/elastic/rendezvous/utils.pyÚ_parse_rendezvous_config   s*    
ÿ
ÿ
r   )Úport_strr   c                 C   s   | rt  d| ¡rt| ƒS dS )z3Tries to extract the port number from ``port_str``.z^[0-9]{1,5}$N)ÚreÚmatchÚint)r   r   r   r   Ú_try_parse_port9   s    r!   )ÚendpointÚdefault_portr   c                 C   sæ   | dk	r|   ¡ } | sd|fS | d dkrD| d dkrD| fg ˜^}}n|  dd¡^}}t|ƒdkr„|d dkr„|d dkr„|dd… }t|ƒdkr¾t|d ƒ}|dks¬|d	krÂtd
| › d�ƒ‚n|}t d|¡sÞtd| › d�ƒ‚||fS )a5  Extracts the hostname and the port number from a rendezvous endpoint.

    Args:
        endpoint:
            A string in format <hostname>[:<port>].
        default_port:
            The port number to use if the endpoint does not include one.

    Returns:
        A tuple of hostname and port number.
    NÚ	localhostr   ú[éÿÿÿÿú]ú:r   i   z,The port number of the rendezvous endpoint 'z)' must be an integer between 0 and 65536.z^[\w\.:-]+$z)The hostname of the rendezvous endpoint 'zN' must be a dot-separated list of labels, an IPv4 address, or an IPv6 address.)r   ÚrsplitÚlenr!   r   r   r   )r"   r#   ÚhostÚrestÚportr   r   r   r   @   s*    $
ÿ
ÿ)r+   r   c              
   C   s"  | dkrdS zt  | ¡}W n tk
r2   d}Y nX |rB|jrBdS ztj| dtjtjd�}W n, ttjfk
rˆ } zg }W 5 d}~X Y nX dd„ |D ƒ}t 	¡ }| |kr¬dS tj|dtjtjd�}|D ]V}|d rä|d | krä dS |�r|d d	 t
|ƒk�r dS |d d	 |krÆ dS qÆd
S )a=  Indicates whether ``host`` matches the hostname of this machine.

    This function compares ``host`` to the hostname as well as to the IP
    addresses of this machine. Note that it may return a false negative if this
    machine has CNAME records beyond its FQDN or IP addresses assigned to
    secondary NICs.
    r$   TN)ÚprotoÚflagsc                 S   s   g | ]}|d  d ‘qS )é   r   r   )Ú.0Zhost_addr_infor   r   r   Ú
<listcomp>‰   s   ÿz-_matches_machine_hostname.<locals>.<listcomp>é   r0   r   F)Ú	ipaddressÚ
ip_addressr   Úis_loopbackÚsocketÚgetaddrinfoÚIPPROTO_TCPÚAI_CANONNAMEÚgaierrorÚgethostnameÚstr)r+   ÚaddrZhost_addr_listÚ_Zhost_ip_listZ	this_hostZ	addr_listZ	addr_infor   r   r   Ú_matches_machine_hostnameo   sJ    

   ÿ
þ   ÿr@   )Úsecondsr   c                 C   s*   t | tƒrtj| Ž } | dkr&t | ¡ dS )zØSuspends the current thread for ``seconds``.

    Args:
        seconds:
            Either the delay, in seconds, or a tuple of a lower and an upper
            bound within which a random delay will be picked.
    g{®Gáz„?N)Ú
isinstanceÚtupleÚrandomÚuniformÚtimeÚsleep)rA   r   r   r   Ú_delay¦   s    

rH   c                   @   sÈ   e Zd ZU dZG dd„ dƒZee ed< ee ed< ee	j
 ed< eed< eed eed	d
œdd„Zeee dœdd„ƒZed	dœdd„Zd	dœdd„Zd	dœdd„Zed	dœdd„ƒZedd„ ƒZd	S )Ú_PeriodicTimerzÌRepresents a timer that periodically runs a specified function.

    Args:
        interval:
            The interval, in seconds, between each run.
        function:
            The function to run.
    c                   @   sJ   e Zd ZU eed< ed ed< eedf ed< ee	ef ed< e
ed< dS )	z_PeriodicTimer._ContextÚinterval©.NÚfunction.ÚargsÚkwargsÚ
stop_eventN)Ú__name__Ú
__module__Ú__qualname__ÚfloatÚ__annotations__r   r	   r   r   r=   r   r   r   r   r   Ú_ContextÁ   s
   
rU   Ú_nameÚ_threadÚ
_finalizerÚ_ctxrK   N)rJ   rL   rM   rN   r   c                 O   sV   d | _ |  ¡ | _| ¡ | j_|| j_|p*d| j_|p6i | j_tƒ | j_	d | _
d | _d S )Nr   )rV   rU   rY   Útotal_secondsrJ   rL   rM   rN   r   rO   rW   rX   )ÚselfrJ   rL   rM   rN   r   r   r   Ú__init__Ï   s    

z_PeriodicTimer.__init__)r   c                 C   s   | j S )zGets the name of the timer.)rV   ©r[   r   r   r   Únameâ   s    z_PeriodicTimer.name)r^   r   c                 C   s   | j rtdƒ‚|| _dS )z©Sets the name of the timer.

        The specified name will be assigned to the background thread and serves
        for debugging and troubleshooting purposes.
        úThe timer has already started.N)rW   ÚRuntimeErrorrV   )r[   r^   r   r   r   Úset_nameç   s    z_PeriodicTimer.set_namec                 C   s\   | j rtdƒ‚t| j| jpd| jfdd�| _ t | | j| j | jj	¡| _
d| j
_| j  ¡  dS )zStart the timer.r_   ZPeriodicTimerT)Útargetr^   rM   ÚdaemonFN)rW   r`   r   Ú_runrV   rY   ÚweakrefÚfinalizeÚ_stop_threadrO   rX   ÚatexitÚstartr]   r   r   r   ri   ò   s        ÿ   ÿz_PeriodicTimer.startc                 C   s   | j r|   ¡  dS )z'Stop the timer at the next opportunity.N)rX   r]   r   r   r   Úcancel	  s    z_PeriodicTimer.cancelc                 C   s$   | j  | j¡s | j| j| jŽ q d S ©N)rO   ÚwaitrJ   rL   rM   rN   )Úctxr   r   r   rd     s    z_PeriodicTimer._runc                 C   s   |  ¡  |  ¡  d S rk   )ÚsetÚjoin)ÚthreadrO   r   r   r   rg     s    z_PeriodicTimer._stop_thread)rP   rQ   rR   Ú__doc__rU   r   r=   rT   r   re   rf   r   r   r   r\   Úpropertyr^   ra   ri   rj   Ústaticmethodrd   rg   r   r   r   r   rI   µ   s*   
úrI   )r4   rD   r   r7   rF   re   Údatetimer   Ú	threadingr   r   Útypingr   r   r   r   r	   r
   Ú__all__r=   r   r    r!   r   Úboolr@   rS   rH   rI   r   r   r   r   Ú<module>   s    &/7 