a
    ߙfb                     @   s   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mZ d dlmZ ddlmZmZ d	d
 Zdd ZdddZdd Zdd Zdd Zdd ZdS )    N)suppress)urlparse)
_find_home)log)get_readable_fileobj   )SAMPHubErrorSAMPWarningc                 C   sb   i }t | B}|D ],}|ds|d\}}| || < qW d   n1 sT0    Y  |S )zK
    Read in the lockfile given by ``lockfilename`` into a dictionary.
    #=N)r   
startswithsplitstrip)lockfilenamelockfiledictflinekwval r   <lib/python3.9/site-packages/astropy/samp/lockfile_helpers.pyread_lockfile   s    

0r   c                 C   s   t | d}|  t| tjtj  t | d}tj 	 }|
d| d |
d | D ]\}}|
| d| d qb|  d S )Nwz# SAMP lockfile written on 
z!# Standard Profile required keys
r   )opencloseoschmodstatS_IREADS_IWRITEdatetimeZnowZ	isoformatwriteitems)r   r   ZlockfileZnow_isokeyvaluer   r   r   write_lockfile)   s    


r&   c                 C   sl  t   d}dtjv rntjd drltjd tdd  } t| }|d dkrdtd| t	 dS |d } n| d u r$t
d	|  |d
krtjt d} ntjt d}z@zt| W n ty   Y n0 W t|tjtj tj  nt|tjtj tj  0 tj|d| } n
t
d t| \}}|rPtdt	 dS t
d|   t| | | S )N SAMP_HUBstd-lockurl:r   filezAUnable to start a Hub with lockfile {}. Start-up process aborted.F   zRunning mode: Zsingle.samp.samp-1z	samp-hub-zRunning mode: multiplez>Another SAMP Hub is already running. Start-up process aborted.zLock-file: )remove_garbage_lock_filesr   environr   lenr   warningswarnformatr	   r   debugpathjoinr   mkdirOSErrorr   r   r   r    S_IEXECcheck_running_hubr&   )r   modeZhub_idZ
hub_paramslockfiledirZlockfile_parsedhub_is_runningr   r   r   r   create_lock_file8   sR    




r>   c                  C   sf   t  } | stddtjv rNtjd drDtjd tdd }q^tdntjt d}| | S )z
    Get either the hub given by the environment variable SAMP_HUB, or the one
    given by the lockfile .samp in the user home directory.
    z"Unable to find a running SAMP Hub.r(   r)   NzSAMP Hub profile not supported.r,   )	get_running_hubsr   r   r/   r   r0   r5   r6   r   )hubsr   r   r   r   get_main_running_huby   s    

rA   c                  C   s   i } d}dt jv r:t jd drJt jd tdd }nt jt d}t|\}}|rb|| |< d}t jt d}t j|rt 	|D ]4}|drt j||}t|\}}|r|| |< q| S )a  
    Return a dictionary containing the lock-file contents of all the currently
    running hubs (single and/or multiple mode).

    The dictionary format is:

    ``{<lock-file>: {<token-name>: <token-string>, ...}, ...}``

    where ``{<lock-file>}`` is the lock-file name, ``{<token-name>}`` and
    ``{<token-string>}`` are the lock-file tokens (name and content).

    Returns
    -------
    running_hubs : dict
        Lock-file contents of all the currently running hubs.
    r'   r(   r)   Nr,   r-   samp-hub)
r   r/   r   r0   r5   r6   r   r:   isdirlistdir)r@   r   r=   r   r<   filenamer   r   r   r?      s&    


r?   c                 C   s   d}i }zt | }W n ty.   ||f Y S 0 d|v rz.tj|d dddd}|jj  d}W n* tjy~   d}Y n t	j
y   Y n0 ||fS )a|  
    Test whether a hub identified by ``lockfilename`` is running or not.

    Parameters
    ----------
    lockfilename : str
        Lock-file name (path + file name) of the Hub to be tested.

    Returns
    -------
    is_running : bool
        Whether the hub is running
    hub_params : dict
        If the hub is running this contains the parameters from the lockfile
    Fzsamp.hub.xmlrpc.url\r'   r   )Z
allow_noneT)r   r8   xmlrpcZServerProxyreplaceZsampZhubZpingZProtocolErrorsocketerror)r   Z
is_runningr   proxyr   r   r   r:      s(    r:   c               	   C   s   d} t jt d} t| \}}|sbt j| rbtt t |  W d    n1 sX0    Y  t jt d}t j	|rt 
|D ]j}|drt j||} t| \}}|st j| rtt t |  W d    q1 s0    Y  qd S )Nr'   r,   r-   rB   )r   r5   r6   r   r:   isfiler   r8   removerC   rD   r   )r   r=   r   r<   rE   r   r   r   r.      s"    
(

r.   )NNNN)r!   r   rI   r   r1   
contextlibr   Zurllib.parser   Zxmlrpc.clientZclientrG   Zastropy.config.pathsr   Zastropyr   Zastropy.utils.datar   errorsr   r	   r   r&   r>   rA   r?   r:   r.   r   r   r   r   <module>   s(     
A5*