U
    ºO.eŸ  ã                   @   sî   d Z ddlZddlZddlZddlZzddlZW n ek
rH   dZY nX ddlZddlZddl	Z	ddl
mZmZmZ e e¡Zedœdd„Zejdeedœd	d
„ƒZedœdd„Zejeedœdd„ƒZee dœdd„Zedœdd„ZdS )aL  This module contains several helper functions which can be used to
find an address of the submitting system, for example to use as the
address parameter for HighThroughputExecutor.

The helper to use depends on the network environment around the submitter,
so some experimentation will probably be needed to choose the correct one.
é    N)ÚSetÚListÚCallable)Úreturnc                  C   sT   t  d¡ ddl} |  | j| j¡}| d¡ | ¡ d }| ¡  t  d |¡¡ |S )zâFinds an address for the local host by querying the local routing table
       for the route to Google DNS.

       This will return an unusable value when the internet-facing address is
       not reachable from workers.
    z/Finding address by querying local routing tabler   N)z8.8.8.8éP   úAddress found: {})	ÚloggerÚdebugÚsocketÚAF_INETÚ
SOCK_DGRAMÚconnectÚgetsocknameÚcloseÚformat)r
   ÚsÚaddr© r   úm/mounts/lovelace/software/anaconda3/envs/qiime2-shotgun-2023.9/lib/python3.8/site-packages/parsl/addresses.pyÚaddress_by_route   s    

r   é   )Útimeoutr   c                 C   sP   t  d¡ tjd| d}|jdkr<|j}t  d |¡¡ |S td |j¡ƒ‚dS )aD  Finds an address for the local host by querying ipify. This may
       return an unusable value when the host is behind NAT, or when the
       internet-facing address is not reachable from workers.
       Parameters:
       -----------

       timeout : float
          Timeout for the request in seconds. Default: 30s
    z*Finding address by querying remote servicezhttps://api.ipify.org)r   éÈ   r   z6Remote service returned unexpected HTTP status code {}N)r   r	   ÚrequestsÚgetZstatus_codeÚtextr   ÚRuntimeError)r   Úresponser   r   r   r   Úaddress_by_query-   s    

r   c                  C   s&   t  d¡ t ¡ } t  d | ¡¡ | S )z‘Returns the hostname of the local host.

       This will return an unusable value when the hostname cannot be
       resolved from workers.
    z'Finding address by using local hostnamer   )r   r	   ÚplatformÚnoder   )r   r   r   r   Úaddress_by_hostnameC   s    
r!   )Úifnamer   c                 C   sV   t dk	stdƒ‚t tjtj¡}t t  | ¡ dt 	dt
| dd… dƒ¡¡dd… ¡S )	aT  Returns the IP address of the given interface name, e.g. 'eth0'

    This is taken from a Stack Overflow answer: https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python#24196955

    Parameters
    ----------
    ifname : str
        Name of the interface whose address is to be returned. Required.

    Nz*This function is not supported on your OS.i‰  Z256sé   zutf-8é   é   )ÚfcntlÚAssertionErrorr
   r   r   Ú	inet_ntoaÚioctlÚfilenoÚstructÚpackÚbytes)r"   r   r   r   r   Úaddress_by_interfaceO   s    ý ür.   c               	   C   s˜   t  ¡ } tƒ }| D ]<}z| t|ƒ¡ W q tk
rL   t d |¡¡ Y qX qt	t
tg}|D ]4}z| |ƒ ¡ W q^ tk
r   t d¡ Y q^X q^|S )zx Uses a combination of methods to determine possible addresses.

    Returns:
         list of addresses as strings
    ú3Ignoring failure to fetch address from interface {}ú$Ignoring an address finder exception)ÚpsutilÚnet_if_addrsÚsetÚaddr.   Ú	Exceptionr   Úinfor   r!   r   r   )Únet_interfacesZs_addressesÚ	interfaceÚresolution_functionsÚfr   r   r   Úget_all_addressesd   s    
r;   c               	   C   s¦   t  ¡ } d}| D ]<}zt|ƒ}|W   S  tk
rJ   t d |¡¡ Y qX qttt	g}|D ]4}z|ƒ }|W   S  tk
rŽ   t d¡ Y q\X q\|dkr¢tdƒ‚|S )zy Uses a combination of methods to find any address of the local machine.

    Returns:
        one address in string
    Ú r/   r0   z)Cannot find address of the local machine.)
r1   r2   r.   r5   r   r6   r   r!   r   r   )r7   r   r8   r9   r:   r   r   r   Úget_any_address~   s$    


r=   )r   )Ú__doc__Úloggingr   r   r
   r&   ÚImportErrorr+   Ú	typeguardr1   Útypingr   r   r   Ú	getLoggerÚ__name__r   Ústrr   ÚtypecheckedÚfloatr   r!   r.   r;   r=   r   r   r   r   Ú<module>   s*   

