B
    چ\5                 @   s   d 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
ddgZdZdd	 Zddd
Zdd Zedd ZdS )z%Routines common to all posix systems.    N   )memoize)
sdiskusage)usage_percent)PY3)unicode
pid_existswait_pid
disk_usageget_terminal_mapc          
   C   sl   | dkrdS yt | d W nF tk
rb } z(|jtjkr>dS |jtjkrNdS |W dd}~X Y nX dS dS )z6Check whether pid exists in the current process table.r   TFN)oskillOSErrorerrnoZESRCHZEPERM)piderr r   .lib/python3.7/site-packages/psutil/_psposix.pyr      s    c       	   
      s   fdd}t tdtjdk	rB fdd}  n fdd}d}xy| \}}W nf tk
r } zH|jtjkr||}wTn,|jtjkrxt r||}qdS qW n W dd}~X Y qTX |d	kr||}qTt|rt	| S t
|rt|S td
| qTW dS )aI  Wait for process with pid 'pid' to terminate and return its
    exit status code as an integer.

    If pid is not a children of os.getpid() (current process) just
    waits until the process disappears and return None.

    If pid does not exist at all return None immediately.

    Raise TimeoutExpired on timeout expired.
    c                s8   d k	r  kr t  dt|  t| d dS )N)r   name   g{Gz?)TimeoutExpiredtimeZsleepmin)delay)r   	proc_namestop_attimeouttimerr   r   check_timeoutC   s
    

zwait_pid.<locals>.check_timeoutZ	monotonicNc                  s   t  t jS )N)r   waitpidWNOHANGr   )r   r   r   waitcallL   s    zwait_pid.<locals>.waitcallc                  s   t  dS )Nr   )r   r   r   )r   r   r   r!   P   s    g-C6?r   zunknown process exit status %r)getattrr   r   r   ZEINTRZECHILDr   r   WIFSIGNALEDWTERMSIG	WIFEXITEDWEXITSTATUS
ValueError)	r   r   r   r   r!   r   ZretpidZstatusr   r   )r   r   r   r   r   r   r	   8   s8    



c             C   s   t rt| }ndyt| }W nT tk
rr   t| trly| t } W n tk
r^   Y nX t| }n Y nX |j	|j
 }|j|j
 }|j|j
 }|| }|| }t||dd}t||||dS )a.  Return disk usage associated with path.
    Note: UNIX usually reserves 5% disk space which is not accessible
    by user. In this function "total" and "used" values reflect the
    total and used disk space whereas "free" and "percent" represent
    the "free" and "used percent" user disk space.
    r   )Zround_)totalusedZfreeZpercent)r   r   statvfsUnicodeEncodeError
isinstancer   encodesysgetfilesystemencodingf_blocksf_frsizef_bfreef_bavailr   r   )pathstr(   Zavail_to_rootZavail_to_userr)   Z
total_userZusage_percent_userr   r   r   r
   {   s(    
c              C   s   i } t  dt  d }xb|D ]Z}|| ks2t|y|| t|j< W q tk
rv } z|jtjkrf W dd}~X Y qX qW | S )zMGet a map of device-id -> path as a dict.
    Used by Process.terminal()
    z	/dev/tty*z
/dev/pts/*N)globAssertionErrorr   statst_rdevr   r   ZENOENT)ZretZlsr   r   r   r   r   r      s    
)NN)__doc__r   r6   r   r.   r   Z_commonr   r   r   Z_compatr   r   __all__r   r   r	   r
   r   r   r   r   r   <module>   s    
C1