3 JZ@sdZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z dd l mZd d d d gZdd Zddd Zdd Zedd ZdS)z%Routines common to all posix systems.N)memoize) sdiskusage) usage_percent)PY3)unicode)TimeoutExpired pid_existswait_pid disk_usageget_terminal_mapcCsl|dkr dSytj|dWnFtk rb}z*|jtjkr>dS|jtjkrNdS|WYdd}~XnXdSdS)z6Check whether pid exists in the current process table.rTFN)oskillOSErrorerrnoZESRCHZEPERM)piderrr /usr/lib64/python3.6/_psposix.pyr s  c sfdd}ttdtjdk rBfdd}n fdd}d}xy|\}}Wnftk r}zJ|jtjkr||}wTn,|jtjkrxtr||}qdSqWnWYdd}~XqTX|d kr||}qTtj|rtj | Stj |rtj |St d |qTWdS) aIWait 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. cs8dk r kr tdtj|t|ddS)N)rnameg{Gz?)rtimeZsleepmin)delay)r proc_namestop_attimeouttimerrr check_timeout?s   zwait_pid..check_timeoutZ monotonicNcstjtjS)N)r waitpidWNOHANGr)rrrwaitcallHszwait_pid..waitcallcs tjdS)Nr)r rr)rrrr!Lsg-C6?rzunknown process exit status %r) getattrrrrZEINTRZECHILDr r WIFSIGNALEDWTERMSIG WIFEXITED WEXITSTATUS ValueError) rrrrr!rZretpidZstatusrr)rrrrrrr 4s8            cCstrtj|}ndytj|}WnTtk rrt|trly|jtj}Wntk r^YnXtj|}nYnX|j |j }|j |j }|j |j }||}||}t ||dd}t||||dS)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)totalusedZfreeZpercent)rr statvfsUnicodeEncodeError isinstancerencodesysgetfilesystemencodingf_blocksf_frsizef_bfreef_bavailrr)pathstr(Z avail_to_rootZ avail_to_userr)Z total_userZusage_percent_userrrrr ws(      cCsi}tjdtjd}xb|D]Z}||ks2t|y||tj|j<Wqtk rv}z|jtjkrfWYdd}~XqXqW|S)zMGet a map of device-id -> path as a dict. Used by Process.terminal() z /dev/tty*z /dev/pts/*N)globAssertionErrorr statst_rdevrrENOENT)retZlsrrrrrr s  )NN)__doc__rr6r r.rZ_commonrrrZ_compatrr _exceptionsr__all__r r r r rrrrs         C1