B
    îq\ð  ã               @   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   sR   i }t | ƒ<}x4|D ],}| d¡s| d¡\}}| ¡ || ¡ < qW W dQ R X |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.7/site-packages/astropy/samp/lockfile_helpers.pyÚread_lockfile   s    


r   c             C   sŒ   t | dƒ}| ¡  t | tjtj ¡ t | dƒ}tj ¡  	¡ }| 
d |¡¡ | 
d¡ x&| ¡ D ]\}}| 
d ||¡¡ qbW | ¡  d S )NÚwz# SAMP lockfile written on {}
z!# Standard Profile required keys
z{0}={1}
)ÚopenÚcloseÚosÚchmodÚstatÚS_IREADÚS_IWRITEÚdatetimeZnowZ	isoformatÚwriteÚformatÚitems)r   r   ZlockfileZnow_isoÚkeyÚvaluer   r   r   Úwrite_lockfile)   s    


r&   c             C   sT  t ƒ  d}dtjkrntjd  d¡rltjd tdƒd … } t| ƒ}|d dkrdt d | ¡t	¡ dS |d } n¨| d krt
 d	| ¡ |d
kr tj tƒ d¡} njtj tƒ d¡}z(yt |¡ W n tk
rÔ   Y nX W d t |tjtj tj ¡ X tj |d |¡¡} n
t
 d¡ t| ƒ\}}|r8t dt	¡ dS t
 d|  ¡ t| |ƒ | S )NÚ ÚSAMP_HUBzstd-lockurl:r   ÚfilezAUnable to start a Hub with lockfile {}. Start-up process aborted.Fé   zRunning mode: Zsinglez.sampz.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Úwarnr"   r	   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   sD    





r:   c              C   sf   t ƒ } | stdƒ‚dtjk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(   zstd-lockurl:NzSAMP Hub profile not supported.z.samp)	Úget_running_hubsr   r   r,   r   r-   r1   r2   r   )Úhubsr   r   r   r   Úget_main_running_huby   s    

r=   c              C   sÊ   i } d}dt jk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 |¡rÆxBt  	|¡D ]4}| d¡rŽt j ||¡}t|ƒ\}}|rŽ|| |< qŽW | 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(   zstd-lockurl:Nz.sampz.samp-1zsamp-hub)
r   r,   r   r-   r1   r2   r   r6   ÚisdirÚlistdir)r<   r   r9   r   r8   Úfilenamer   r   r   r;      s&    

r;   c             C   sœ   d}i }yt | ƒ}W n tk
r,   ||fS X d|kr”y.tj|d  dd¡dd}|jj ¡  d}W n. tjk
r~   d}Y n t	j
k
r’   Y nX ||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   r4   ÚxmlrpcZServerProxyÚreplaceZsampZhubZpingZProtocolErrorÚsocketÚerror)r   Z
is_runningr   Úproxyr   r   r   r6   Å   s"    
r6   c           
   C   sÔ   d} t j tƒ d¡} t| ƒ\}}|sNt j | ¡rNttƒ t  | ¡ W d Q R X t j tƒ d¡}t j 	|¡rÐxdt  
|¡D ]V}| d¡rvt j ||¡} t| ƒ\}}|svt j | ¡rvttƒ t  | ¡ W d Q R X qvW d S )Nr'   z.sampz.samp-1zsamp-hub)r   r1   r2   r   r6   Úisfiler   r4   Úremover>   r?   r   )r   r9   r   r8   r@   r   r   r   r+   ï   s"    


r+   )NNNN)r    r   rD   r   r.   Ú
contextlibr   Zurllib.parser   Zxmlrpc.clientZclientrB   Zastropy.config.pathsr   Zastropyr   Zastropy.utils.datar   Úerrorsr   r	   r   r&   r:   r=   r;   r6   r+   r   r   r   r   Ú<module>   s&    
@5*