Mini Shell

Direktori : /proc/self/root/lib64/python3.6/site-packages/pyanaconda/__pycache__/
Upload File :
Current File : //proc/self/root/lib64/python3.6/site-packages/pyanaconda/__pycache__/ntp.cpython-36.pyc

3

C��e��@s�dZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
mZddlm
Z
dZejd�ZdZGd	d
�d
e�Zdd�Zd
d�Zdd�Zeefdd�Zeedfdd�Zddd�Zddd�ZdS)zO
Module facilitating the work with NTP servers and NTP daemon's configuration

�N)�isys)�	threadMgr�AnacondaThread)�THREAD_SYNC_TIME_BASENAMEz/etc/chrony.confz4^\s*(server|pool)\s*([-a-zA-Z.0-9]+)\s*[a-zA-Z]+\s*$�c@seZdZdZdS)�NTPconfigErrorz(Exception class for NTP related problemsN)�__name__�
__module__�__qualname__�__doc__�rr�/usr/lib64/python3.6/ntp.pyr-srcCsZtj�}y|j|�Wn>tjk
r,dStjk
r@dStjk
rTdSXdS)a
    Tries to do an NTP request to the $server (timeout may take some time).

    :param server: hostname or IP address of an NTP server
    :type server: string
    :return: True if the given server is reachable and working, False otherwise
    :rtype: bool

    FT)�ntplib�	NTPClient�request�NTPException�socket�gaierror�error)�server�clientrrr
�ntp_server_working2srcCs0g}x|D]}|jt|g�q
W|j|�|S)N)�extend�SERVERS_PER_POOL)�pools�servers�ret�poolrrr
�pools_servers_to_internalNs


rcCs�t�}g}g}x |D]}|j|d�d||<qWxb|j�D]V}||tkr||j||t|g�|j||t|g�q:|j|||g�q:W||fS)Nr�)�dict�get�keysrr)Z
pools_serversZserver_numsrr�itemrrr
�internal_to_pools_and_serversWs
r$c	Cs�t�}t�}yft|d��R}xJ|D]B}|j|�}|r |jd�dkrR|j|jd��q |j|jd��q WWdQRXWn8tk
r�}zd||jf}t|��WYdd}~XnX||fS)z�
    Goes through the chronyd's configuration file looking for lines starting
    with 'server'.

    :return: servers found in the chronyd's configuration
    :rtype: list

    �rrr�Nz+Cannot open config file %s for reading (%s))�list�open�match�group�append�IOError�strerrorr)	�conf_file_path�
srv_regexprrZ	conf_file�liner)�ioerr�msgrrr
�get_servers_from_configis

"
r3c,Cs�yt|d�}Wn8tk
rF}zd||jf}t|��WYdd}~XnXy,|rZt|d�}ntj�\}	}
tj|	d�}WnLtk
r�}z0|r�d||jf}nd|
|jf}t|��WYdd}~XnXd}|j|�x|D]}|jd|d	�q�Wx|D]}
|jd
|
d	�q�Wx0|D](}|j	|��r||k�r|j|��qW|j
�|j
�|�s�ytj|
|�tj
|
�Wn6tk
�r�}zd|j}t|��WYdd}~XnXdS)a9
    Replaces the pools and servers defined in the chronyd's configuration file
    with the given ones. If the out_file is not None, then it is used for the
    resulting config.

    :type pools: iterable
    :type servers: iterable
    :param out_file_path: path to the file used for the resulting config

    r%z+Cannot open config file %s for reading (%s)N�wz/Cannot open new config file %s for writing (%s)z.Cannot open temporary file %s for writing (%s)z2# These servers were defined in the installation:
zpool z iburst
zserver z3Cannot replace the old config with the new one (%s))r(r,r-r�tempfileZmkstemp�os�fdopen�writer)�close�shutilZcopyfile�unlink�OSError)rrr.r/Z
out_file_pathZ
old_conf_filer1r2Z
new_conf_fileZfildesZ	temp_pathZheadingrrr0Zoserrrrr
�save_servers_to_config�sJ





r=cCsrtj�}y"|j|�}tjt|j��d}Wn2tjk
rDd}Yntj	k
r\d}YnX|dk	rn||�|S)a�
    Synchronize the system time with a given NTP server. Note that this
    function is blocking and will not return until the time gets synced or
    querying server fails (may take some time before timeouting).

    :param server: NTP server
    :param callback: callback function to run after sync or failure
    :type callback: a function taking one boolean argument (success)
    :return: True if the sync was successful, False otherwise

    TFN)
rrrrZset_system_time�intZtx_timerrr)r�callbackr�results�successrrr
�
one_time_sync�s


rBcCs6dt|f}tj|�rdStjt|t||fd��dS)a�
    Asynchronously synchronize the system time with a given NTP server. This
    function is non-blocking it starts a new thread for synchronization and
    returns. Use callback argument to specify the function called when the
    new thread finishes if needed.

    :param server: NTP server
    :param callback: callback function to run after sync or failure
    :type callback: a function taking one boolean argument (success)

    z%s_%sN)�name�target�args)rrr!�addrrB)rr?Zthread_namerrr
�one_time_sync_async�s



rG)N)N)r�rer6r5r:rrZ
pyanacondarZpyanaconda.threadingrrZpyanaconda.core.constantsrZNTP_CONFIG_FILE�compileZSRV_LINE_REGEXPr�	Exceptionrrrr$r3r=rBrGrrrr
�<module>s,
	F