ó
ÐH/\c           @` sÏ   d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l Z d d l m Z e j	 re d „  Z
 n	 d „  Z
 e d „ Z d „  Z e Z y d  d l Z Wn e k
 r¸ e Z e Z n Xd	 „  Z d
 „  Z d S(   i    (   t   absolute_importt   print_functiont   divisionNi   (   t   sysinfoc           C` s   t  j d ƒ ‚ d  S(   Ns   lsof not expected on Windows(   t   unittestt   SkipTest(    (    (    s7   lib/python2.7/site-packages/gevent/testing/openfiles.pyt	   _run_lsof    s    c          C` s£   d d  l  }  t j ƒ  } |  j d ƒ \ } } t j | ƒ d | | f } t j | ƒ rk t j d ƒ ‚ n  t | ƒ  } | j	 ƒ  j
 ƒ  } Wd  QXt j | ƒ | S(   Ni    t   get_open_filess   lsof -p %s > %ss   lsof failed(   t   tempfilet   ost   getpidt   mkstempt   closet   systemR   R   t   opent   readt   stript   remove(   R   t   pidt   fdt   tmpnamet   lsof_commandt   fobjt   data(    (    s7   lib/python2.7/site-packages/gevent/testing/openfiles.pyR   #   s    c   
      C` sR  t  ƒ  } i  } x| j d ƒ D]} | j ƒ  } | s | j d ƒ rM q n  t j d | ƒ } | d  \ } } } } | d  j ƒ  s‘ | j ƒ  r |  r® | d j ƒ  r® q n  | d j ƒ  sÎ t | d  ƒ n	 t | ƒ } | | k r| | | | j | ƒ | f }	 t d |	 ƒ ‚ n  | | | <q q W| sDt d | f ƒ ‚ n  | | d <| S(	   Ns   
t   COMMANDs   \s+i   iÿÿÿÿsU   error when parsing lsof output: duplicate fd=%r
line=%r
split=%r
previous=%r
data:
%ss   failed to parse lsof:
%sR   (	   R   t   splitR   t
   startswitht   ret   isdigitt   intt   gett   AssertionError(
   t   pipesR   t   resultst   lineR   t   _commandt   _pidt   _userR   t   params(    (    s7   lib/python2.7/site-packages/gevent/testing/openfiles.pyt   default_get_open_files1   s(    	,
c          C` su   t  j j d ƒ r5 d t  j ƒ  }  t t  j |  ƒ ƒ Sy t t d t ƒ ƒ d SWn t t	 t
 j f k
 rp d SXd  S(   Ns   /proc/s   /proc/%d/fdR    i   i    (   R	   t   patht   existsR
   t   lent   listdirR   t   Truet   OSErrorR   R   R   (   t   fd_directory(    (    s7   lib/python2.7/site-packages/gevent/testing/openfiles.pyt   default_get_number_open_filesI   s    c          C` sn   t  ƒ  }  t j ƒ  } | j ƒ  | j d ƒ |  d <x |  d D] } | |  | j <q= W|  d c d | g 7<|  S(   s’  
        Return a list of popenfile and pconn objects.

        Note that other than `fd`, they have different attributes.

        .. important:: If you want to find open sockets, on Windows
           and linux, it is important that the socket at least be listening
           (socket.listen(1)). Unlike the lsof implementation, this will only
           return sockets in a state like that.
        t   allR   s   From psutil(   t   dictt   psutilt   Processt
   open_filest   connectionsR   (   R!   t   processt   x(    (    s7   lib/python2.7/site-packages/gevent/testing/openfiles.pyR   d   s    	c          C` s3   t  j ƒ  }  y |  j ƒ  SWn t k
 r. d SXd  S(   Ni    (   R2   R3   t   num_fdst   AttributeError(   R6   (    (    s7   lib/python2.7/site-packages/gevent/testing/openfiles.pyt   get_number_open_filesw   s
    (   t
   __future__R    R   R   R	   R   R   t    R   t   WINR   t   FalseR'   R/   t   lsof_get_open_filesR2   t   ImportErrorR   R:   (    (    (    s7   lib/python2.7/site-packages/gevent/testing/openfiles.pyt   <module>   s"   			
	