σ
ίΘ[c           @` s3  d  d l  m Z m Z m Z m Z 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 d d l m Z m Z d   Z d   Z d d d d d  Z d   Z  d   Z! d   Z" d   Z# d S(   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t
   _find_home(   t   urlparse(   t   xmlrpc_client(   t   six(   t   log(   t   suppress(   t   get_readable_fileobji   (   t   SAMPHubErrort   SAMPWarningc         C` sm   i  } t  |   U } xK | D]C } | j d  s | j d  \ } } | j   | | j   <q q WWd QX| S(   uK   
    Read in the lockfile given by ``lockfilename`` into a dictionary.
    u   #u   =N(   R
   t
   startswitht   splitt   strip(   t   lockfilenamet   lockfiledictt   ft   linet   kwt   val(    (    s<   lib/python2.7/site-packages/astropy/samp/lockfile_helpers.pyt   read_lockfile   s    #c         C` sΑ   t  |  d  } | j   t j |  t j t j  t  |  d  } t j j   j	   } | j
 d j |   | j
 d  x6 t j |  D]% \ } } | j
 d j | |   q W| j   d  S(   Nu   wu   # SAMP lockfile written on {}
u!   # Standard Profile required keys
u   {0}={1}
(   t   opent   closet   ost   chmodt   statt   S_IREADt   S_IWRITEt   datetimet   nowt	   isoformatt   writet   formatR   t	   iteritems(   R   R   t   lockfilet   now_isot   keyt   value(    (    s<   lib/python2.7/site-packages/astropy/samp/lockfile_helpers.pyt   write_lockfile,   s    
c         C` sΈ  t    d } d t j k r t j d j d  rjt j d t d  }  t |   } | d d k r t j d j |   t	  t
 S| d }  qjnΨ |  d  k r]t j d |  | d	 k rΦ t j j t   d
  }  qjt j j t   d  } z) y t j |  Wn t k
 rn XWd  t j | t j t j t j  Xt j j | d j |   }  n t j d  t |   \ } } | rt j d t	  t
 St j d |   t |  |  |  S(   Nu    u   SAMP_HUBu   std-lockurl:i    u   fileuA   Unable to start a Hub with lockfile {}. Start-up process aborted.i   u   Running mode: u   singleu   .sampu   .samp-1u   samp-hub-{}u   Running mode: multipleu>   Another SAMP Hub is already running. Start-up process aborted.u   Lock-file: (   t   remove_garbage_lock_filesR   t   environR   t   lenR   t   warningst   warnR"   R   t   Falset   NoneR   t   debugt   patht   joinR   t   mkdirt   OSErrorR   R   R   R   t   S_IEXECt   check_running_hubR(   (   R   t   modet   hub_idt
   hub_paramst   lockfiledirt   lockfile_parsedt   hub_is_runningR   (    (    s<   lib/python2.7/site-packages/astropy/samp/lockfile_helpers.pyt   create_lock_file;   sF     		c          C` s   t    }  |  s t d   n  d t j k rl t j d j d  r] t j d t d  } q t d   n t j j t   d  } |  | S(   u   
    Get either the hub given by the environment variable SAMP_HUB, or the one
    given by the lockfile .samp in the user home directory.
    u"   Unable to find a running SAMP Hub.u   SAMP_HUBu   std-lockurl:u   SAMP Hub profile not supported.u   .samp(	   t   get_running_hubsR   R   R*   R   R+   R1   R2   R   (   t   hubsR   (    (    s<   lib/python2.7/site-packages/astropy/samp/lockfile_helpers.pyt   get_main_running_hub|   s    	c          C` s(  i  }  d } d t  j k rN t  j d j d  rf t  j d t d  } qf n t  j j t   d  } t |  \ } } | r | |  | <n  d } t  j j t   d  } t  j j |  r$xf t  j	 |  D]R } | j d  rΛ t  j j | |  } t |  \ } } | r| |  | <qqΛ qΛ Wn  |  S(   uδ  
    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.
    u    u   SAMP_HUBu   std-lockurl:u   .sampu   .samp-1u   samp-hub(
   R   R*   R   R+   R1   R2   R   R6   t   isdirt   listdir(   R?   R   R<   R   R:   t   filename(    (    s<   lib/python2.7/site-packages/astropy/samp/lockfile_helpers.pyR>      s&    c         C` sΏ   t  } i  } y t |   } Wn t k
 r6 | | f SXd | k r΅ y? t j | d j d d  d d } | j j j   t	 } Wq΅ t j
 k
 r t	 } q΅ t j k
 r± q΅ Xn  | | f S(   u|  
    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
    u   samp.hub.xmlrpc.urlu   \u    t
   allow_nonei   (   R.   R   t   IOErrort   xmlrpct   ServerProxyt   replacet   sampt   hubt   pingt   Truet   ProtocolErrort   sockett   error(   R   t
   is_runningR   t   proxy(    (    s<   lib/python2.7/site-packages/astropy/samp/lockfile_helpers.pyR6   Θ   s"    
	c          C` s0  d }  t  j j t   d  }  t |   \ } } | sn t  j j |   rn t t   t  j |   Wd  QXqn n  t  j j t   d  } t  j j	 |  r,x t  j
 |  D]} } | j d  r¨ t  j j | |  }  t |   \ } } | s%t  j j |   r"t t   t  j |   Wd  QXq"q%q¨ q¨ Wn  d  S(   Nu    u   .sampu   .samp-1u   samp-hub(   R   R1   R2   R   R6   t   isfileR	   R4   t   removeRA   RB   R   (   R   R<   R   R:   RC   (    (    s<   lib/python2.7/site-packages/astropy/samp/lockfile_helpers.pyR)   ς   s"    ($   t
   __future__R    R   R   R   R   R   RN   R   R,   t   config.pathsR   t   extern.six.moves.urllib.parseR   t   extern.six.movesR   RF   t   externR   t    R   t   utils.compatR	   t
   utils.dataR
   t   errorsR   R   R   R(   R/   R=   R@   R>   R6   R)   (    (    (    s<   lib/python2.7/site-packages/astropy/samp/lockfile_helpers.pyt   <module>   s*   "			@		5	*