§
    ÎÑßjX  ã                   óD  — d Z 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Zddlm	Z	 ddl
ZddlmZ d„ Z G d„ de¦  «        Z G d„ d	e¦  «        Z G d
„ de¦  «        Z ed¦  «        fd„Z ed¦  «        fd„Zd„ Z G d„ de¦  «        Zdd„Zedk    o e¦   «          dS  dS )z:
A simple library for managing the availability of ports.
é    N)Úabc)Útimingc                 ó"   — | dk    rdS | dv rdS | S )z×
    Return the host on which a client can connect to the given listener.

    >>> client_host('192.168.0.1')
    '192.168.0.1'
    >>> client_host('0.0.0.0')
    '127.0.0.1'
    >>> client_host('::')
    '::1'
    z0.0.0.0z	127.0.0.1)z::z::0z	::0.0.0.0z::1© )Úserver_hosts    ú;/opt/saltstack/salt/lib/python3.11/site-packages/portend.pyÚclient_hostr	      s-   € ð �iÒÐàˆ{ØÐ0Ð0Ð0ð ˆuØÐó    c                   ó$   — e Zd Zdd„Zdd„Zd„ ZdS )ÚCheckerç      ð?c                 ó   — || _         d S ©N©Útimeout)Úselfr   s     r   Ú__init__zChecker.__init__)   s   € ØˆŒˆˆr
   Nc                 óH  — |€'t          |t          j        ¦  «        r|dd…         \  }}t          j        ¦   «         dk    rt          |¦  «        }t          j        ||t          j        t          j	        ¦  «        }t          t          j        | j        |¦  «        ¦  «         dS )až  
        Assert that the given addr is free
        in that all attempts to connect fail within the timeout
        or raise a PortNotFree exception.

        >>> free_port = find_available_local_port()

        >>> Checker().assert_free('localhost', free_port)
        >>> Checker().assert_free('127.0.0.1', free_port)
        >>> Checker().assert_free('::1', free_port)

        Also accepts an addr tuple

        >>> addr = '::1', free_port, 0, 0
        >>> Checker().assert_free(addr)

        Host might refer to a server bind address like '::', which
        should use localhost to perform the check.

        >>> Checker().assert_free('::', free_port)
        Né   ÚWindows)Ú
isinstancer   ÚSequenceÚplatformÚsystemr	   ÚsocketÚgetaddrinfoÚ	AF_UNSPECÚSOCK_STREAMÚlistÚ	itertoolsÚstarmapÚ_connect)r   ÚhostÚportÚinfos       r   Úassert_freezChecker.assert_free,   s‰   € ð, ˆ<�J t­S¬\Ñ:Ô:ˆ<Ø˜b˜q˜bœ‰JˆD�$ÝŒ?ÑÔ 	Ò)Ð)Ý˜tÑ$Ô$ˆDÝÔ! $¨­fÔ.>ÅÔ@RÑSÔSˆÝ�YÔ˜tœ}¨dÑ3Ô3Ñ4Ô4Ð4Ð4Ð4r
   c                 ó–  — t          j         |||¦  «        }|                     | j        ¦  «         t          j        |¦  «        5  	 |                     |¦  «         n!# t           j        $ r Y d d d ¦  «         d S w xY w	 d d d ¦  «         n# 1 swxY w Y   |d d…         \  }}d}	t           |	j        di t          ¦   «         ¤Ž¦  «        ‚)Nr   z Port {port} is in use on {host}.r   )
r   Ú
settimeoutr   Ú
contextlibÚclosingÚconnectÚerrorÚPortNotFreeÚformatÚlocals)
r   ÚafÚsocktypeÚprotoÚ	canonnameÚsaÚsr#   r$   Útmpls
             r   r"   zChecker._connectI   s.  € ÝŒM˜"˜h¨Ñ.Ô.ˆà	�Š�T”\Ñ"Ô"Ð"åÔ Ñ"Ô"ð 	ð 	ðØ—	’	˜"‘”��øÝ”<ð ð ð Øð		ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ðøøøð ð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð ˜˜˜”V‰
ˆˆdØ1ˆÝ˜+˜$œ+Ð1Ð1­©¬Ð1Ð1Ñ2Ô2Ð2s6   ÁBÁAÁBÁA;Á,BÁ:A;Á;BÂBÂB)r   r   )Ú__name__Ú
__module__Ú__qualname__r   r&   r"   r   r
   r   r   r   (   sK   € € € € € ðð ð ð ð5ð 5ð 5ð 5ð:3ð 3ð 3ð 3ð 3r
   r   c                   ó   — e Zd ZdS )ÚTimeoutN©r7   r8   r9   r   r
   r   r;   r;   Z   ó   € € € € € Ø€Dr
   r;   c                   ó   — e Zd ZdS )r-   Nr<   r   r
   r   r-   r-   ^   r=   r
   r-   ÚInfc           	      óZ  — | st          d¦  «        ‚t          j        |¦  «        }	 	 t          d¬¦  «                             | |¦  «         dS # t
          $ rQ |                     ¦   «         r&t           dj        di t          ¦   «         ¤Ž¦  «        ‚t          j        d¦  «         Y nw xY wŒ†)uà  
    Wait for the specified port to become free (dropping or rejecting
    requests). Return when the port is free or raise a Timeout if timeout has
    elapsed.

    Timeout may be specified in seconds or as a timedelta.
    If timeout is None or âˆž, the routine will run indefinitely.

    >>> free('localhost', find_available_local_port())

    >>> free(None, None)
    Traceback (most recent call last):
    ...
    ValueError: Host values of '' or None are not allowed.
    ú*Host values of '' or None are not allowed.Tçš™™™™™¹?r   NzPort {port} not free on {host}.r   )Ú
ValueErrorr   ÚTimerr   r&   r-   Úexpiredr;   r.   r/   ÚtimeÚsleep©r#   r$   r   Útimers       r   ÚfreerJ   b   sÄ   € ð  ð GÝÐEÑFÔFÐFåŒL˜Ñ!Ô!€Eð	ð	å˜CÐ Ñ Ô ×,Ò,¨T°4Ñ8Ô8Ð8ØˆFøÝð 	ð 	ð 	Ø�}Š}‰Œð TÝÐFÐ?ÔFÐRÐRÍÉÌÐRÐRÑSÔSÐSåŒJ�s‰OŒOˆOˆOˆOð		øøøð	s   ¨$A ÁAB)Â(B)c                 óZ  — | st          d¦  «        ‚t          j        |¦  «        }	 	 t          d¬¦  «                             | |¦  «         |                     ¦   «         r&t           dj        di t          ¦   «         ¤Ž¦  «        ‚t          j
        d¦  «         n# t          $ r Y dS w xY wŒ†)	uE  
    Wait for the specified port to become occupied (accepting requests).
    Return when the port is occupied or raise a Timeout if timeout has
    elapsed.

    Timeout may be specified in seconds or as a timedelta.
    If timeout is None or âˆž, the routine will run indefinitely.

    >>> occupied('localhost', find_available_local_port(), .1)
    Traceback (most recent call last):
    ...
    Timeout: Port ... not bound on localhost.

    >>> occupied(None, None)
    Traceback (most recent call last):
    ...
    ValueError: Host values of '' or None are not allowed.
    rA   Tg      à?r   z Port {port} not bound on {host}.rB   Nr   )rC   r   rD   r   r&   rE   r;   r.   r/   rF   rG   r-   rH   s       r   ÚoccupiedrL   ƒ   sÄ   € ð& ð GÝÐEÑFÔFÐFåŒL˜Ñ!Ô!€Eð	ð	Ý˜CÐ Ñ Ô ×,Ò,¨T°4Ñ8Ô8Ð8Ø�}Š}‰Œð UÝÐGÐ@ÔGÐSÐSÍ&É(Ì(ÐSÐSÑTÔTÐTåŒJ�s‰OŒOˆOˆOøÝð 	ð 	ð 	àˆFˆFð	øøøð	s   ¨A2B Â
B)Â(B)c                  óZ  — t          j        ddt           j        t           j        ¦  «        } t	          t          | ¦  «        ¦  «        \  }}}}}t          j         ||¦  «        }|                     |¦  «         |                     ¦   «         dd…         \  }}|                     ¦   «          |S )zb
    Find a free port on localhost.

    >>> 0 < find_available_local_port() < 65536
    True
    Nr   r   )	r   r   r   r   ÚnextÚiterÚbindÚgetsocknameÚclose)ÚinfosÚfamilyr2   Ú_ÚaddrÚsockr$   s          r   Úfind_available_local_portrX   §   sŽ   € õ Ô˜t Q­Ô(8½&Ô:LÑMÔM€EÝ $¥T¨%¡[¤[Ñ 1Ô 1Ñ€FˆE�1�a˜ÝŒ=˜ Ñ'Ô'€DØ‡I‚Iˆd�O„O€OØ×!Ò!Ñ#Ô# B Q BÔ'�J€Dˆ$Ø‡J‚J�L„L€LØ€Kr
   c                   óT   — e Zd ZdZed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         ZdS )ÚHostPorta  
    A simple representation of a host/port pair as a string

    >>> hp = HostPort('localhost:32768')

    >>> hp.host
    'localhost'

    >>> hp.port
    32768

    >>> len(hp)
    15

    >>> hp = HostPort('[::1]:32768')

    >>> hp.host
    '::1'

    >>> hp.port
    32768
    c                 óP   — t           j                             d| › �¦  «        j        S ©Nz//)ÚurllibÚparseÚurlparseÚhostname©r   s    r   r#   zHostPort.hostÏ   s!   € åŒ|×$Ò$ [¨$ [ [Ñ1Ô1Ô:Ð:r
   c                 óP   — t           j                             d| › �¦  «        j        S r\   )r]   r^   r_   r$   ra   s    r   r$   zHostPort.portÓ   s!   € åŒ|×$Ò$ [¨$ [ [Ñ1Ô1Ô6Ð6r
   c                 ó¬   — |d d…         \  }}t          |¦  «        }d|v rd|› d�n|} | d                     |t          |¦  «        g¦  «        ¦  «        S )Nr   ú:ú[ú])r	   ÚjoinÚstr)ÚclsrV   Úlisten_hostr$   Ú
plain_hostr#   s         r   Ú	from_addrzHostPort.from_addr×   sh   € à   ! œHÑˆ�TÝ  Ñ-Ô-ˆ
Ø$'¨:Ð$5Ð$5Ð �:Ð Ð Ð Ð ¸:ˆØˆs�3—8’8˜T¥3 t¡9¤9Ð-Ñ.Ô.Ñ/Ô/Ð/r
   N)	r7   r8   r9   Ú__doc__Úpropertyr#   r$   Úclassmethodrl   r   r
   r   rZ   rZ   ·   so   € € € € € ðð ð. ð;ð ;ñ „Xð;ð ð7ð 7ñ „Xð7ð ð0ð 0ñ „[ð0ð 0ð 0r
   rZ   c                 óÞ  — t          j        ¦   «         }d„ }|                     ddt          ¬¦  «         |                     dd|¬¦  «         |                     ddd t          ¬	¦  «         |                     | ¦  «        } 	 |                      | j        j        | j        j	        | j
        ¬
¦  «         d S # t          $ r/}t          |t          j        ¬¦  «         t          d¦  «        ‚d }~ww xY w)Nc                 ó*   — t          ¦   «         |          S r   )Úglobals)Úkeys    r   Úglobal_lookupz_main.<locals>.global_lookupâ   s   € Ý‰yŒy˜Œ~Ðr
   Útargetz	host:port)ÚmetavarÚtypeÚfuncÚstatez-tz	--timeout)Údefaultrw   r   )Úfileé   )ÚargparseÚArgumentParserÚadd_argumentrZ   ÚfloatÚ
parse_argsrx   ru   r#   r$   r   r;   ÚprintÚsysÚstderrÚ
SystemExit)ÚargsÚparserrt   r   s       r   Ú_mainrˆ   ß   sö   € ÝÔ$Ñ&Ô&€Fðð ð ð ×Ò˜¨+½HÐÑEÔEÐEØ
×Ò˜¨°mÐÑDÔDÐDØ
×Ò˜˜k°4½eÐÑDÔDÐDØ×Ò˜TÑ"Ô"€DðØ�	Š	�$”+Ô" D¤KÔ$4¸d¼lˆ	ÑKÔKÐKÐKÐKøÝð ð ð Ýˆg�CœJÐ'Ñ'Ô'Ð'Ý˜‰mŒmÐøøøøðøøøs   Â 1B3 Â3
C,Â=*C'Ã'C,Ú__main__r   )rm   rF   r   r}   rƒ   r    r)   r   Úcollectionsr   Úurllib.parser]   Útemporar   r	   Úobjectr   ÚIOErrorr;   r-   r€   rJ   rL   rX   rh   rZ   rˆ   r7   r   r
   r   ú<module>r�      sÊ  ððð ð €€€Ø €€€Ø €€€Ø 
€
€
€
Ø Ð Ð Ð Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð à Ð Ð Ð Ð Ð ðð ð ð,/3ð /3ð /3ð /3ð /3ˆfñ /3ô /3ð /3ðd	ð 	ð 	ð 	ð 	ˆgñ 	ô 	ð 	ð	ð 	ð 	ð 	ð 	�'ñ 	ô 	ð 	ð #˜U 5™\œ\ð ð ð ð ðB "'  u¡¤ð !ð !ð !ð !ðHð ð ð %0ð %0ð %0ð %0ð %0ˆsñ %0ô %0ð %0ðPð ð ð ð" 	ˆJÒ Ð "˜5˜5™7œ7˜7˜7˜7Ð "Ð "Ð "r
   