B
    *dq              R   @   s  d Z ddlmZ ddl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	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 ydd	l	mZ W n ek
r   dZY nX ydd
l	mZ W n ek
r   dZY nX ejdkrddlZndZejd dkZeedZe Zddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_gRZejd`kZejdakZ ej!"dbZ#ej!"dcZ$e$Z%ej!"ddZ&ej!"deZ'ej!"dfZ(e&p6e'p6e(Z)ej!"dgZ*ej!"dhZ+diZ,djZ-dkZ.dlZ/dmZ0dnZ1doZ2dpZ3dqZ4drZ5dsZ6dtZ7duZ8dvZ9dwZ:dxZ;dyZ<dzZ=d{Z>d|Z?d}Z@d~ZAdZBdZCdZDdZEedkrdZFdZGdZHn G dd dejIZJeK LeJjM edkrdZNdZOn G dd dejIZPeK LePjM eQ ZRes8dZSn2yeT ZSW n$ eUk
rh   er`dndZSY nX edIddddddgZVedEddddgZWedCddddddgZXedDddddddgZYedFddddddddgZZedJdddddgZ[edAdddddddgZ\edGdddddgZ]edHdddddgZ^edBddddgZ_eddddgZ`edddddgZaeddddgZbedddgZced9ddddgZded>ddgZeed?dddgZfed@dddgZged;dddgZhed<ddddgZied=ddgZjed:ddgZked8ddddddgZledddgZmeeegeegfeegegfegegfeegegfegegfeegeegfegeegfegeegfdݜZnedk	renLegegfegegfdޜ edk	renLdegeegfi G dd deoZpG dd depZqG dd deqZrG dd depZsG dd depZterTeuevewr@evd Zxn
eyevdZxexd ndd ZzdddQZ{ddN Z|dd Z}ddM Z~ddP Ze|ddR ZddO ZddS ZddT Zddd[ZddL ZG dd dZddU Ze Zeje_eje_dZddW ZddV Zeefd dXZefddYZdddZZdd ZerLdd Zndd Ze|ejfd	d^Zddd]Zdd
ejfdd_Zdd\ ZdS (  z9Common objects shared by __init__.py and _ps*.py modules.    )division)print_functionN)
namedtuple)AF_INET)
SOCK_DGRAM)SOCK_STREAM)AF_INET6)AF_UNIX)      r
   PSUTIL_DEBUGFREEBSDBSDLINUXNETBSDOPENBSDMACOSOSXPOSIXSUNOSWINDOWS
CONN_CLOSECONN_CLOSE_WAITCONN_CLOSINGCONN_ESTABLISHEDCONN_FIN_WAIT1CONN_FIN_WAIT2CONN_LAST_ACKCONN_LISTEN	CONN_NONECONN_SYN_RECVCONN_SYN_SENTCONN_TIME_WAITNIC_DUPLEX_FULLNIC_DUPLEX_HALFNIC_DUPLEX_UNKNOWNSTATUS_DEADSTATUS_DISK_SLEEPSTATUS_IDLESTATUS_LOCKEDSTATUS_RUNNINGSTATUS_SLEEPINGSTATUS_STOPPEDSTATUS_SUSPENDEDSTATUS_TRACING_STOPSTATUS_WAITINGSTATUS_WAKE_KILLSTATUS_WAKINGSTATUS_ZOMBIESTATUS_PARKEDENCODINGENCODING_ERRSr   pconn	pcputimespctxswpgidspiopionice	popenfilepthreadpuidssconn	scpustatssdiskio	sdiskpart
sdiskusagesnetiosnicaddr	snicstatssswapsuser	conn_tmapdeprecated_methodisfile_strictmemoizeparse_environ_blockpath_exists_strictusage_percentsupports_ipv6sockfam_to_enumsocktype_to_enumwrap_numbers	open_textopen_binarycatbcatbytes2humanconn_to_ntupledebughiliteterm_supports_colorsprint_colorposixntlinuxdarwin)ZfreebsdZmidnightbsdZopenbsdZnetbsd)sunossolarisaixrunningsleepingz
disk-sleepstoppedztracing-stopzombieZdeadz	wake-killZwakingidlelockedZwaitingZ	suspendedZparkedZESTABLISHEDZSYN_SENTZSYN_RECVZ	FIN_WAIT1Z	FIN_WAIT2Z	TIME_WAITZCLOSEZ
CLOSE_WAITZLAST_ACKZLISTENZCLOSINGNONE      c               @   s   e Zd ZdZdZdZdS )	NicDuplexrl   rm   r   N)__name__
__module____qualname__r#   r$   r%    rr   rr   [/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/psutil/_common.pyrn      s   rn   c               @   s   e Zd ZdZdZdS )BatteryTimert   ru   N)ro   rp   rq   POWER_TIME_UNKNOWNPOWER_TIME_UNLIMITEDrr   rr   rr   rs   rv      s   rv   replacesurrogateescapetotalusedfreepercentsinZsoutZ
read_countZwrite_count
read_byteswrite_bytesZ	read_timeZ
write_timeZdeviceZ
mountpointZfstypeoptsZmaxfileZmaxpathZ
bytes_sentZ
bytes_recvZpackets_sentZpackets_recvZerrinZerroutZdropinZdropoutnameZterminalhoststartedpidfdfamilytypeladdrraddrstatusaddressnetmask	broadcastZptpZisupZduplexspeedZmtuflagsZctx_switchesZ
interruptsZsoft_interruptsZsyscallsscpufreqcurrentminmaxshwtemplabelhighcriticalsbatteryZsecsleftZpower_pluggedsfanusersystemchildren_userchildren_systempathidZ	user_timeZsystem_timerealZ	effectiveZsavedZioclassvalueZ	voluntaryZinvoluntaryaddripport)allZtcpZtcp4ZudpZudp4ZinetZinet4Zinet6)Ztcp6Zudp6unixc               @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
ErrorzQBase exception class. All other psutil exceptions inherit
    from this one.
    psutilc             C   sL   t  }x>|D ]6}t| |d }|r,|||< q|dkr|dkr|||< qW |S )Nr   r   )collectionsOrderedDictgetattr)selfattrsinfor   r   rr   rr   rs   	_infodict  s    

zError._infodictc             C   sP   |  d}|r,dddd | D  }nd }ddd t| dd	|fD S )
N)r   ppidr   z(%s)z, c             S   s   g | ]\}}d ||f qS )z%s=%rrr   ).0kvrr   rr   rs   
<listcomp>+  s    z!Error.__str__.<locals>.<listcomp> c             S   s   g | ]}|r|qS rr   rr   )r   xrr   rr   rs   r   .  s    msg )r   joinitemsr   )r   r   detailsrr   rr   rs   __str__&  s    
zError.__str__c             C   s2   |  d}ddd | D }d| jj|f S )N)r   r   r   secondsr   z, c             S   s   g | ]\}}d ||f qS )z%s=%rrr   )r   r   r   rr   rr   rs   r   3  s    z"Error.__repr__.<locals>.<listcomp>zpsutil.%s(%s))r   r   r   	__class__ro   )r   r   r   rr   rr   rs   __repr__0  s    
zError.__repr__N)ro   rp   rq   __doc__r   r   r   rr   rr   rr   rs   r     s
   

r   c               @   s   e Zd ZdZdZdddZdS )NoSuchProcesszXException raised when a process with a certain PID doesn't
    or no longer exists.
    r   Nc             C   s$   t |  || _|| _|pd| _d S )Nzprocess no longer exists)r   __init__r   r   r   )r   r   r   r   rr   rr   rs   r   =  s    
zNoSuchProcess.__init__)NN)ro   rp   rq   r   r   rr   rr   rr   rs   r   7  s   r   c               @   s   e Zd ZdZdZdddZdS )ZombieProcessa1  Exception raised when querying a zombie process. This is
    raised on macOS, BSD and Solaris only, and not always: depending
    on the query the OS may be able to succeed anyway.
    On Linux all zombie processes are querable (hence this is never
    raised). Windows doesn't have zombie processes.
    r   Nc             C   s$   t | ||| || _|pd| _d S )Nz"PID still exists but it's a zombie)r   r   r   r   )r   r   r   r   r   rr   rr   rs   r   M  s    zZombieProcess.__init__)NNN)ro   rp   rq   r   r   rr   rr   rr   rs   r   D  s   r   c               @   s   e Zd ZdZdZdddZdS )AccessDeniedz@Exception raised when permission to perform an action is denied.r   Nc             C   s$   t |  || _|| _|pd| _d S )Nr   )r   r   r   r   r   )r   r   r   r   rr   rr   rs   r   W  s    
zAccessDenied.__init__)NNN)ro   rp   rq   r   r   rr   rr   rr   rs   r   S  s   r   c               @   s   e Zd ZdZdZdddZdS )TimeoutExpiredzWRaised on Process.wait(timeout) if timeout expires and process
    is still alive.
    r   Nc             C   s*   t |  || _|| _|| _d| | _d S )Nztimeout after %s seconds)r   r   r   r   r   r   )r   r   r   r   rr   rr   rs   r   d  s
    
zTimeoutExpired.__init__)NN)ro   rp   rq   r   r   rr   rr   rr   rs   r   ^  s   r   execzvdef raise_from(value, from_value):
    try:
        raise value from from_value
    finally:
        value = None
    c             C   s   | d S )Nrr   )r   
from_valuerr   rr   rs   
raise_from  s    r   c             C   sD   yt | | d }W n tk
r(   dS X |dk	r<t||}|S dS )z5Calculate percentage usage of 'used' against 'total'.d   g        N)floatZeroDivisionErrorround)r|   r{   Zround_retrr   rr   rs   rO     s    
c                s2   t  fdd} fdd}i  ||_|S )a  A simple memoize decorator for functions supporting (hashable)
    positional arguments.
    It also provides a cache_clear() function for clearing the cache:

    >>> @memoize
    ... def foo()
    ...     return 1
        ...
    >>> foo()
    1
    >>> foo.cache_clear()
    >>>

    It supports:
     - functions
     - classes (acts as a @singleton)
     - staticmethods
     - classmethods

    It does NOT support:
     - methods
    c                 sz   | t t| f}y | S  tk
rt   y| | } |< W n, tk
rn } zt|d W d d }~X Y nX |S X d S )N)	frozensetsortedr   KeyError	Exceptionr   )argskwargskeyr   err)cachefunrr   rs   wrapper  s    zmemoize.<locals>.wrapperc                  s       dS )zClear cache.N)clearrr   )r   rr   rs   cache_clear  s    zmemoize.<locals>.cache_clear)	functoolswrapsr   )r   r   r   rr   )r   r   rs   rL     s
    c                s6   t   fdd}dd }dd }||_||_|S )a  A memoize decorator which is disabled by default. It can be
    activated and deactivated on request.
    For efficiency reasons it can be used only against class methods
    accepting no arguments.

    >>> class Foo:
    ...     @memoize
    ...     def foo()
    ...         print(1)
    ...
    >>> f = Foo()
    >>> # deactivated (default)
    >>> foo()
    1
    >>> foo()
    1
    >>>
    >>> # activated
    >>> foo.cache_activate(self)
    >>> foo()
    1
    >>> foo()
    >>> foo()
    >>>
    c                s   y| j   }W n tk
rX   y | S  tk
rR } zt|d W d d }~X Y nX Y nr tk
r   y | }W n, tk
r } zt|d W d d }~X Y nX y|| j  < W n tk
r   Y nX Y nX |S )N)_cacheAttributeErrorr   r   r   )r   r   r   )r   rr   rs   r     s"     z'memoize_when_activated.<locals>.wrapperc             S   s
   i | _ dS )zjActivate cache. Expects a Process instance. Cache will be
        stored as a "_cache" instance attribute.N)r   )procrr   rr   rs   cache_activate  s    z.memoize_when_activated.<locals>.cache_activatec             S   s"   y| ` W n tk
r   Y nX dS )zDeactivate and clear cache.N)r   r   )r   rr   rr   rs   cache_deactivate  s    z0memoize_when_activated.<locals>.cache_deactivate)r   r   r   r   )r   r   r   r   rr   )r   rs   memoize_when_activated  s    r   c          
   C   sV   yt | }W n6 tk
rD } z|jtjtjfkr4 dS d}~X Y nX t|jS dS )zSame as os.path.isfile() but does not swallow EACCES / EPERM
    exceptions, see:
    http://mail.python.org/pipermail/python-dev/2012-June/120787.html
    FN)osstatOSErrorerrnoEPERMEACCESS_ISREGst_mode)r   str   rr   rr   rs   rK     s    c          
   C   sN   yt |  W n6 tk
rD } z|jtjtjfkr4 dS d}~X Y nX dS dS )zSame as os.path.exists() but does not swallow EACCES / EPERM
    exceptions, see:
    http://mail.python.org/pipermail/python-dev/2012-June/120787.html
    FNT)r   r   r   r   r   r   )r   r   rr   rr   rs   rN     s    c           	   C   s`   t jrtdkrdS y2t  tt j} t|  | d W dQ R X dS  t jk
rZ   dS X dS )z2Return True if IPv6 is supported on this platform.NF)z::1r   T)sockethas_ipv6r   r   
contextlibclosingbinderror)sockrr   rr   rs   rP     s    c             C   sz   i }d}t }xh| d|}||kr$P | d||}||krj| || }| |d | }|rb| }|||< |d }qW |S )zCParse a C environ block of environment variables into a dictionary.r    =rm   )r   findupper)datar   posZWINDOWS_Znext_posZ	equal_posr   r   rr   rr   rs   rM   -  s     c             C   s0   t dkr| S y
t| S  tk
r*   | S X dS )zConvert a numeric socket family value to an IntEnum member.
    If it's not a known member, return the numeric value itself.
    N)enumr   AddressFamily
ValueError)numrr   rr   rs   rQ   I  s    
c             C   s0   t dkr| S y
t| S  tk
r*   | S X dS )zConvert a numeric socket type value to an IntEnum member.
    If it's not a known member, return the numeric value itself.
    N)r   r   
SocketKindr   )r   rr   rr   rs   rR   V  s    
c             C   s   |t jtfkr&|rt| }|r&t| }|t jkrJ|ttfkrJ||t}nt}t|}t|}|dkrxt	| |||||S t
| ||||||S dS )z2Convert a raw connection tuple to a proper ntuple.N)r   r   r   r   r   getr   rQ   rR   r6   r?   )r   famtype_r   r   r   Z
status_mapr   rr   rr   rs   rY   c  s    c                s    fdd}|S )zA decorator which can be used to mark a method as deprecated
    'replcement' is the method name which will be called instead.
    c                s:   d| j f  | jd kr | _t|  fdd}|S )Nz8%s() is deprecated and will be removed; use %s() insteadc                s    t j tdd t| ||S )Nrl   )category
stacklevel)warningswarnDeprecationWarningr   )r   r   r   )r   replacementrr   rs   inner  s    z/deprecated_method.<locals>.outer.<locals>.inner)ro   r   r   r   )r   r	  )r  )r   rs   outerz  s    
z deprecated_method.<locals>.outerrr   )r  r
  rr   )r  rs   rJ   v  s    c               @   sB   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	d
S )_WrapNumberszNWatches numbers so that they don't overflow and wrap
    (reset to zero).
    c             C   s    t  | _i | _i | _i | _d S )N)	threadingLocklockr   	remindersreminder_keys)r   rr   rr   rs   r     s    
z_WrapNumbers.__init__c             C   sX   || j kst|| jkst|| jks*t|| j |< tt| j|< tt| j|< d S )N)r   AssertionErrorr  r  r   defaultdictintset)r   
input_dictr   rr   rr   rs   	_add_dict  s    
z_WrapNumbers._add_dictc             C   sd   | j | }t| t|  }x<|D ]4}x"| j| | D ]}| j| |= q<W | j| |= q(W dS )zIn case the number of keys changed between calls (e.g. a
        disk disappears) this removes the entry from self.reminders.
        N)r   r  keysr  r  )r   r  r   old_dictZ	gone_keysZgone_keyremkeyrr   rr   rs   _remove_dead_reminders  s    

z#_WrapNumbers._remove_dead_remindersc          	   C   s  || j kr| || |S | || | j | }i }x| D ]}|| }y|| }W n tk
rt   |||< w>Y nX g }xvtt|D ]f}	||	 }
||	 }||	f}|
|k r| j| |  |7  < | j| | 	| |
|
| j| |   qW t|||< q>W || j |< |S )zkCache dict and sum numbers which overflow and wrap.
        Return an updated copy of `input_dict`
        )r   r  r  r  r   rangelenr  r  addappendtuple)r   r  r   r  Znew_dictr   Zinput_tupleZ	old_tuplebitsiZinput_value	old_valuer  rr   rr   rs   run  s2    


z_WrapNumbers.runNc          	   C   sh   | j X |dkr0| j  | j  | j  n*| j|d | j|d | j|d W dQ R X dS )z>Clear the internal cache, optionally only for function 'name'.N)r  r   r   r  r  pop)r   r   rr   rr   rs   r     s    

z_WrapNumbers.cache_clearc          	   C   s"   | j  | j| j| jfS Q R X dS )z5Return internal cache dicts as a tuple of 3 elements.N)r  r   r  r  )r   rr   rr   rs   
cache_info  s    z_WrapNumbers.cache_info)N)
ro   rp   rq   r   r   r  r  r#  r   r%  rr   rr   rr   rs   r    s   '
r  c          	   C   s   t j t | |S Q R X dS )zGiven an `input_dict` and a function `name`, adjust the numbers
    which "wrap" (restart from zero) across different calls by adding
    "old value" to "new value" and return an updated dict.
    N)_wnr  r#  )r  r   rr   rr   rs   rS     s    i   c             C   s   t | dtdS )Nrb)	buffering)openFILE_READ_BUFFER_SIZE)fnamerr   rr   rs   rU     s    c             C   sd   t st| dtdS t| dtttd}y
t|_W n0 tk
rB   Y n tk
r^   |   Y nX |S )zOn Python 3 opens a file in text mode by using fs encoding and
    a proper en/decoding errors handler.
    On Python 2 this is just an alias for open(name, 'rt').
    rt)r(  )r(  encodingerrors)	PY3r)  r*  r4   r5   _CHUNK_SIZEr   r   close)r+  Zfobjrr   rr   rs   rT      s    

c          	   C   s\   |t kr"|| 
}| S Q R X n6y|| 
}| S Q R X W n ttfk
rV   |S X dS )zRead entire file content and return it as a string. File is
    opened in text mode. If specified, `fallback` is the value
    returned in case of error, either if the file does not exist or
    it can't be read().
    N)_DEFAULTreadIOErrorr   )r+  fallback_openfrr   rr   rs   rV     s    

c             C   s   t | |tdS )z,Same as above but opens file in binary mode.)r5  r6  )rV   rU   )r+  r5  rr   rr   rs   rW   +  s    %(value).1f%(symbol)sc             C   s   d}i }x0t |dd D ]\}}d|d d > ||< qW x>t|dd D ]*}| || krLt| ||  }|t  S qLW |t|d | d S )zUsed by various scripts. See:
    http://goo.gl/zeJZl

    >>> bytes2human(10000)
    '9.8K'
    >>> bytes2human(100001221)
    '95.4M'
    )	BKMGTPEZYrm   N
   r   )symbolr   )	enumeratereversedr   localsdict)nformatsymbolsprefixr!  srC  r   rr   rr   rs   rX   0  s    	c               C   s   t jd jS )z+Return updated psutil.PROCFS_PATH constant.r   )sysmodulesZPROCFS_PATHrr   rr   rr   rs   get_procfs_pathD  s    rO  c             C   s   | j ttdS )N)r-  r.  )decoder4   r5   )rL  rr   rr   rs   rP  J  s    rP  c             C   s   | S )Nrr   )rL  rr   rr   rs   rP  M  s    c             C   s^   t jdkrdS y2dd l}|  s$t|  |ddks>tW n tk
rT   dS X dS d S )Nr_   Tr   colorsF)r   r   cursesisattyr  Z	setuptermZtigetnumr   )filerR  rr   rr   rs   r\   V  s    
Fc             C   s   t  s
| S g }tddddddddd	d
	}d|d< y|| }W n( tk
rd   tdt|  Y nX || |r~|d dd|| f S )z*Return an highlighted version of 'string'.Z32Z91Z33Z93Z34Z35Z36Z37Z30)	greenredbrownyellowbluevioletZ	lightblueZgreyZdarkgreyZ29Nz#invalid color %r; choose between %s1z[%sm%s[0m;)r\   rG  r   r   listr  r  r   )rL  colorboldattrrQ  rr   rr   rs   r[   e  s    

c          	   C   s  t  st| |d ntr.tt| |||d nddl}d}|jjj}|jjj}t	ddddd}||d< y|| }W n, t
k
r   td	|t| f Y nX |r|dkr|d
7 }|tjkrdnd}	|j|_||	}
||
| zt| |d W d||
| X dS )z$Print a colorized version of string.)rT  r   N   rl   r      )rU  rV  rW  rX  z#invalid color %r; choose between %r   ii)r\   printr   r[   ctypeswindllZKernel32GetStdHandleSetConsoleTextAttributerG  r   r   r]  r  rM  stderrZc_ulongrestype)rL  r^  r_  rT  re  ZDEFAULT_COLORrg  rh  rQ  Z	handle_idhandlerr   rr   rs   r]   x  s0    


c             C   sn   t rjddl}|| j\}}}}}t| trRt| ttt	frJd|  } nd|  } t
d||| f tjd dS )z@If PSUTIL_DEBUG env var is set, print a debug message to stderr.r   Nzignoring %szignoring %rzpsutil-debug [%s:%s]> %s)rT  )r   inspectgetframeinfocurrentframef_back
isinstancer   r   r4  EnvironmentErrorrd  rM  ri  )r   rl  r+  lineno_linesindexrr   rr   rs   rZ     s    

)N)N)r8  )NF)r   
__future__r   r   r   r   r   r   r   r   r   rM  r  r  r   r   r   r   r   ImportErrorr	   version_infor   r/  boolgetenvr   objectr2  __all__r   r   r   platform
startswithr   r   r   r   r   r   r   r   ZAIXr*   r+   r'   r,   r.   r2   r&   r0   r1   r(   r)   r/   r-   r3   r   r!   r    r   r   r"   r   r   r   r   r   r   r#   r$   r%   IntEnumrn   globalsupdate__members__rw   rx   rv   getfilesystemencodingr4   r5   getfilesystemencodeerrorsr   rG   rC   rA   rB   rD   rH   r?   rE   rF   r@   r   r   r   r   r7   r<   r=   r>   r9   r:   r;   r8   r6   r   rI   r   r   r   r   r   r   rp  __builtins__rG  exec_r   r   rO   rL   r   rK   rN   rP   rM   rQ   rR   rY   rJ   r  rS   r&  r   r%  r*  rU   rT   rV   rW   rX   rO  rP  stdoutr\   r[   r]   rZ   rr   rr   rr   rs   <module>   s  



	

	







	




	!

,EW		"