
    IR-em                         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	 Zd
 ZddZd Zd Zd Zd ZdS )    N)suppress)urlparse)log)
_find_home)get_readable_fileobj   )SAMPHubErrorSAMPWarningc                    i }t          |           5 }|D ]X}|                    d          sA|                    d          \  }}|                                ||                                <   Y	 ddd           n# 1 swxY w Y   |S )zK
    Read in the lockfile given by ``lockfilename`` into a dictionary.
    #=N)r   
startswithsplitstrip)lockfilenamelockfiledictflinekwvals         =lib/python3.11/site-packages/astropy/samp/lockfile_helpers.pyread_lockfiler      s     L	l	+	+ 7q 	7 	7D??3'' 7**S//C+.99;;RXXZZ(	77 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 s   AA;;A?A?c                    t          | d          }|                                 t          j        | t          j        t          j        z              t          | d          }t          j                                        	                                }|
                    d| d           |
                    d           |                                D ] \  }}|
                    | d| d           !|                                 d S )Nwz# SAMP lockfile written on 
z!# Standard Profile required keys
r   )opencloseoschmodstatS_IREADS_IWRITEdatetimenow	isoformatwriteitems)r   r   lockfilenow_isokeyvalues         r   write_lockfiler,   %   s    L#&&HNNH\4<$-7888L#&&H##%%//11GNN<<<<===NN7888"((** + +
U#)))))****NN    c                    t                       d}dt          j        v rt          j        d                             d          ryt          j        d         t	          d          d          } t          |           }|d         dk    r/t          j        d                    |           t                     dS |d         } nP| 9t          j        d	|z              |d
k    r.t          j                            t                      d          } nt          j                            t                      d          }	 t          j        |           n# t           $ r Y nw xY wt          j        |t$          j        t$          j        z   t$          j        z              n># t          j        |t$          j        t$          j        z   t$          j        z              w xY wt          j                            |d|           } nt          j        d           t-          |           \  }}|rt          j        dt                     dS t          j        d| z              t/          | |           | S )N SAMP_HUBstd-lockurl:r   filezAUnable to start a Hub with lockfile {}. Start-up process aborted.F   zRunning mode: 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   modehub_id
hub_paramslockfiledirlockfile_parsedhub_is_runningr   s           r   create_lock_filerJ   3   s4   K RZ:j!,,^<< 	2:j1#n2E2E2G2GHL&|44Oq!V++006|0D0D  
 u.q1 I&-...x!w||JLL'BB gll:<<CCWH[))))   D H[$,*F*UVVVVBH[$,*F*UVVVV!w||K9MV9M9MNN I.///#4\#B#B NL L	
 	
 	
 uIml*+++<,,,s*   :E F 
EF EF ;Gc                  |   t                      } | st          d          dt          j        v r\t          j        d                             d          r(t          j        d         t          d          d         }n;t          d          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.r0   r1   NzSAMP Hub profile not supported.r5   )	get_running_hubsr	   r   r8   r   r9   r>   r?   r   )hubsr   s     r   get_main_running_hubrN   p   s    
 D A?@@@ RZ:j!,,^<< 	B:j1#n2E2E2G2GHLL@AAAw||JLL'::r-   c                     i } d}dt           j        v rMt           j        d                             d          r't           j        d         t          d          d         }n,t           j                            t                      d          }t          |          \  }}|r|| |<   d}t           j                            t                      d          }t           j                            |          ret          j	        |          D ]P}|                    d          r9t           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/   r0   r1   Nr5   r6   samp-hub)
r   r8   r   r9   r>   r?   r   rC   isdirlistdir)rM   r   rI   r   rG   filenames         r   rL   rL      s<   " DL
 RZ:j!,,^<< 	I:j1#n2E2E2G2GHLw||JLL'::#4\#B#B NL *)\ K',,z||Y77K	w}}[!! 6
;// 	6 	6H"":.. 6!w||KBB/@/N/N,! 6)5D&Kr-   c                 L   d}i }	 t          |           }n# t          $ r ||fcY S w xY wd|v rt	 t          j        |d                             dd          d          }|j        j                                         d}n"# t          j        $ r d}Y nt          $ r Y nw xY w||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   )
allow_noneT)	r   rA   xmlrpcServerProxyreplacesamphubpingProtocolError)r   
is_runningr   proxys       r   rC   rC      s      JL($\22 ( ( (<''''( ,,	&23;;D"EERS  E JN!!!JJ# 	 	 	 JJJ 	 	 	D	 |##s$    ''AB   B	BBc                  `   d} t           j                            t                      d          } t	          |           \  }}|s_t           j                            |           r@t          t                    5  t          j        |            d d d            n# 1 swxY w Y   t           j                            t                      d          }t           j        	                    |          rt          j
        |          D ]}|                    d          rt           j                            ||          } t	          |           \  }}|s_t           j                            |           r@t          t                    5  t          j        |            d d d            n# 1 swxY w Y   d S d S )Nr/   r5   r6   rP   )r   r>   r?   r   rC   isfiler   rA   removerQ   rR   r   )r   rI   r   rG   rS   s        r   r7   r7      s   L 7<<
g66L#4\#B#B NL (7>>,'' 	('"" ( (	,'''( ( ( ( ( ( ( ( ( ( ( ( ( ( (
 ',,z||Y77K	w}}[!! 	4
;// 	4 	4H"":.. 4!w||KBB/@/N/N,% 4w~~l33 4%g.. 4 4Il3334 4 4 4 4 4 4 4 4 4 4 4 4 4 4	4 	4	4 	4s$   6BBB?F  F$	'F$	)NNNN)r#   r   r    r:   xmlrpc.clientclientrW   
contextlibr   urllib.parser   astropyr   astropy.config.pathsr   astropy.utils.datar   errorsr	   r
   r   r,   rJ   rN   rL   rC   r7    r-   r   <module>rl      s*    				               ! ! ! ! ! !       + + + + + + 3 3 3 3 3 3 - - - - - - - -    : : : :z  .1 1 1h'$ '$ '$T4 4 4 4 4r-   