ó
½-'Nc           @   sr   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 e	 f d „  ƒ  YZ
 d S(   iÿÿÿÿN(   t   get_mount_info(   t   CoverageMonitor(   t   Accumulator(   t   MonitorPlugint	   MountInfoc           B   s­   e  Z d  Z d Z d d d e j d d d d „ Z d „  Z d „  Z d	 „  Z	 d
 „  Z
 e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s
   mount-infoiÈ   i,  i<   s   /proc/mountss	   /etc/mtabc   
      C   s   | |  _  | |  _ | |  _ | |  _ | |  _ | d  k rE t j } n  | |  _ | |  _ g  |  _	 g  |  _
 d  |  _ y d d l m } Wn t k
 r¢ | |  _ n X| p¯ | ƒ  |  _ y d d l m }	 Wn t k
 rå d  |  _ n7 Xy |	 j j g  ƒ |  _ Wn t k
 rd  |  _ n Xd  S(   Niÿÿÿÿ(   t
   HALManager(   t   GUdev(   t   run_intervalt   _monitor_intervalt   _create_timet   _mounts_filet
   _mtab_filet   Nonet   ost   statvfst   _statvfst   _free_spacet   _mount_infot   _mount_info_to_persistt   landscape.halR   t   ImportErrort   _hal_managert   gi.repositoryR   t   _gudev_clientt   Clientt   newt   AttributeError(
   t   selft   intervalt   monitor_intervalt   mounts_filet   create_timeR   t   hal_managert	   mtab_fileR   R   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyt   __init__   s2    										c         C   s­   t  t |  ƒ j | ƒ t |  j |  j j ƒ |  _ t |  j	 d d d |  j
 ƒ|  _ |  j j j |  j |  j j ƒ |  j j j d |  j j d d ƒ|  j d |  j t ƒ d  S(   Ngš™™™™™é?s   mount info snapshotR   t   stopt   priorityiÐ  s
   mount-info(   t   superR   t   registerR   t   _persistt   registryt	   step_sizet   _accumulateR   R   R	   t   _monitort   reactort
   call_everyR   t   logt   call_ont   call_on_acceptedt   send_messagest   True(   R   R(   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyR&   0   s    "c         C   s   t  d  |  j ƒ  |  j ƒ  g ƒ S(   N(   t   filterR   t   create_mount_info_messaget   create_free_space_message(   R   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyt   create_messages;   s    c         C   s>   |  j  r: i d d 6|  j  d 6} |  j  |  _ g  |  _  | Sd  S(   Ns
   mount-infot   type(   R   R   R   (   R   t   message(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyR4   ?   s    		c         C   sH   |  j  rD |  j  |  j  } i d d 6| d 6} |  j  |  j |  _  | Sd  S(   Ns
   free-spaceR7   (   R   t    max_free_space_items_to_exchangeR   (   R   t   items_to_exchangeR8   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyR5   G   s    	


c            s_   xX ˆ  j  ƒ  D]J } ˆ  j j j | d | ƒ} | d d k r | j ‡  f d †  ƒ q q Wd  S(   Nt   urgentR7   s
   mount-infoc            s
   ˆ  j  ƒ  S(   N(   t   persist_mount_info(   t   x(   R   (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyt   <lambda>V   s    (   R6   R(   t   brokert   send_messaget   addCallback(   R   R;   R8   t   d(    (   R   s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyR1   R   s    c         C   s   |  j  j j d |  j ƒ d  S(   Ns
   mount-info(   R(   R?   t   call_if_acceptedR1   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyt   exchangeX   s    c         C   sW   x: |  j  D]/ \ } } | d } |  j j d | f | ƒ q
 Wd  |  _  |  j j ƒ  d  S(   Ns   mount-points
   mount-info(   R   R'   t   setR   R(   t   flush(   R   t	   timestampt
   mount_infot   mount_point(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyR<   \   s
    
	c         C   s5  |  j  j ƒ  t |  j ƒ  ƒ } t ƒ  } x|  j ƒ  D]ø } | d } | j d ƒ } d | f } |  j | | | ƒ } | r± | d } t | d ƒ } |  j j	 | | | f ƒ n  |  j
 j d | f ƒ }	 |	 sÜ |	 | k r | g  |  j D] \ }
 } | ^ qé k r |  j j	 | | f ƒ q n  | j | ƒ q5 Wd  S(   Ns   mount-points
   free-spaces   accumulate-free-spacei    i   s
   mount-info(   R+   t   pingt   intR	   RE   t   _get_mount_infot   popR*   R   t   appendR'   t   getR   t   add(   R   t   nowt   current_mount_pointsRH   RI   t
   free_spacet   keyt	   step_dataRG   t   prev_mount_infot   tt   m(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyt   runf   s"    	

(c         C   s=   |  j  d  k	 r |  j ƒ  S|  j d  k	 r2 |  j ƒ  St ƒ  Sd  S(   N(   R   R   t   _get_hal_removable_devicesR   t   _get_udev_removable_devicesRE   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyt   _get_removable_devices|   s
    

c            s#   d t  f ‡  f d †  ƒ  Y} | ƒ  S(   Nt   is_removablec              s   e  Z ‡  f d  †  Z RS(   c            s)   ˆ  j  j | ƒ } | r% | j d ƒ St S(   Nt	   removable(   R   t   query_by_device_filet   get_sysfs_attr_as_booleant   False(   t   oselft   device_namet   device(   R   (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyt   __contains__†   s    (   t   __name__t
   __module__Re   (    (   R   (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyR]   …   s   (   t   object(   R   R]   (    (   R   s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyR[   „   s    c         C   s­  i  } i  } t  ƒ  } |  j j ƒ  } xç | D]ß } | j j d ƒ } | r+ | j j d ƒ rn | j | j ƒ n  y | | j j | ƒ Wn! t k
 r© | g | | j <n X| j j d ƒ } | d  k	 r
y | | j | j ƒ Wqt k
 r| j g | | <qXq
q+ q+ Wt
 } xg | r}t } xT | D]L } | | k r*x7 | | D]( }	 |	 | k rG| j |	 ƒ t
 } qGqGWq*q*WqWt  ƒ  }
 x | D] } |
 j | | ƒ qŽW|
 S(   Ns   block.devices   storage.removables   info.parent(   RE   R   t   get_devicest
   propertiesRO   RP   t   udiRN   t   KeyErrorR   R2   Ra   t   update(   R   t   block_devicest   childrenR^   t   devicesRd   t   block_devicet
   parent_udit   updatedt	   child_udit   removable_devicesRk   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyRZ      s@    			c         c   s   |  j  ƒ  } |  j ƒ  } xp t |  j |  j ƒ D]Y } | d } | d } | j d ƒ r. | j d ƒ r. | | k r. | | k r. | Vq. q. Wd S(   s=   Generator yields local mount points worth recording data for.Rd   s   mount-points   /dev/N(   R\   t   _get_bound_mount_pointsR    R
   R   t
   startswith(   R   Ru   t   bound_mount_pointst   infoRd   RI   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyRL   Ä   s    

c         C   s¿   t  ƒ  } |  j s) t j j |  j ƒ r- | St |  j d ƒ } xy | D]q } y/ | j ƒ  d  \ } } } } | j d ƒ } Wn t k
 r‘ qF n Xd | j d ƒ k rF | j	 | ƒ qF qF W| S(   si   
        Returns a set of mount points that have the "bind" option
        by parsing /etc/mtab.
        t   ri   s   string-escapet   bindt   ,(
   RE   R   R   t   patht   isfilet   opent   splitt   decodet
   ValueErrorRP   (   R   t   bound_pointst   filet   lineRd   RI   t
   filesystemt   options(    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyRv   Ò   s    	 i  N(   Rf   Rg   t   persist_nameR9   t   timeR   R"   R&   R6   R4   R5   Ra   R1   RD   R<   RY   R\   R[   RZ   RL   Rv   (    (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyR   
   s$   							
					7	(   R‰   R   t   landscape.lib.diskR    t   landscape.lib.monitorR   t   landscape.accumulateR   t   landscape.monitor.pluginR   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/landscape/monitor/mountinfo.pyt   <module>   s   