
    ztfD                     T   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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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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lmZ ddlmZ ddlmZ erddlZddlZddlmZ d$dZ d Z!d Z"d Z#d Z$ ejJ                  e d       G d de             Z& ejJ                  e d       G d de             Z' ejJ                  e d       G d  d!e             Z(e)d"k(  rdd#l*m+Z+  e+e,       yy)%zPOSIX specific tests.    N)AIX)BSD)LINUX)MACOS)OPENBSD)POSIX)SUNOS)HAS_NET_IO_COUNTERS)
PYTHON_EXE)	QEMU_USER)PsutilTestCase)mock)retry_on_failure)sh)skip_on_access_denied)spawn_testproc)	terminate)which)getpagesizec                 ,   dg}t         r|j                  d       ||j                  dt        |      g       n/t        st
        r|j                  d       n|j                  d       t        r| j                  dd      } |j                  d	| g       t        |      }t         r|j                         n|j                         d
d }g }|D ]/  }|j                         }	 t        |      }|j                  |       1 ||S |d   S # t        $ r Y 'w xY w)zwWrapper for calling the ps command with a little bit of cross-platform
    support for a narrow range of features.
    psz--no-headersNz-pz-Aaxstartstimez-o   r   )r   appendextendstrr	   r   replacer   
splitlinesstripint
ValueError)fmtpidcmdoutput
all_outputlines         a/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/psutil/tests/test_posix.pyr   r   .   s   
 &C

>"


D#c(#$CJJtJJtkk'7+JJc{WF$)V v/@/@/B12/FFJ  zz|	t9D 	$  {!}  		s    D	DDc                 |    d}t         rd}t        ||       j                         }t        rd|d   v sJ |d   S |d   S )Ncommandcommz
/bin/qemu-r   r   )r	   r   splitr   )r%   fieldr,   s      r*   ps_namer0   f   sL    En""$Gwqz)))qz1:    c                     d}t         st        rd}t        ||       }t        j                  dd|      }|j                         S )Nr,   argsz\(python.*?\)$ )r   r	   r   resubr!   )r%   r/   outs      r*   ps_argsr8   q   s;    E
e
UC.C
&&"B
,C99;r1   c                 .    d}t         rd}t        ||       S )Nrssrssizer   r   r%   r/   s     r*   ps_rssr>   {   s    E
eS>r1   c                 .    d}t         rd}t        ||       S )Nvszvsizer<   r=   s     r*   ps_vszrB      s    E
eS>r1   z
POSIX onlyc                   z   e Zd ZdZed        Zed        Zd Zd Zd Z	d Z
d Z e        e       d	               Z e        e       d
               Zd Zd Zd Zd Z ej*                  exs ed      d        Zd Z e       d        Z ej*                  ed       ej*                  ed      d               Zy)TestProcesszBCompare psutil results against 'ps' command line utility (mainly).c                 f    t        t        ddgt        j                        j                  | _        y )Nz-Ez-O)stdin)r   r   
subprocessPIPEr%   clss    r*   
setUpClasszTestProcess.setUpClass   s&     t$JOO

# 	r1   c                 .    t        | j                         y N)r   r%   rI   s    r*   tearDownClasszTestProcess.tearDownClass   s    #''r1   c                     t        d| j                        }t        j                  | j                        j	                         }| j                  ||       y )Nppid)r   r%   psutilProcessrP   assertEqual)selfppid_psppid_psutils      r*   	test_ppidzTestProcess.test_ppid   s=    VTXX&nnTXX.335+.r1   c                     t        d| j                        }t        j                  | j                        j	                         j
                  }| j                  ||       y )Nuid)r   r%   rQ   rR   uidsrealrS   )rT   uid_ps
uid_psutils      r*   test_uidzTestProcess.test_uid   sC    E488$^^DHH-22499
,r1   c                     t        d| j                        }t        j                  | j                        j	                         j
                  }| j                  ||       y )Nrgid)r   r%   rQ   rR   gidsr[   rS   )rT   gid_ps
gid_psutils      r*   test_gidzTestProcess.test_gid   sC    FDHH%^^DHH-22499
,r1   c                     t        d| j                        }t        j                  | j                        j	                         }| j                  ||       y )Nuser)r   r%   rQ   rR   usernamerS   )rT   username_psusername_psutils      r*   test_usernamezTestProcess.test_username   s=    * ..2;;=o6r1   c                 ,   t        j                         }t        j                  dt              5 }| j                  |j                         t        |j                         j                               |j                  sJ 	 d d d        y # 1 sw Y   y xY w)Nzpsutil.pwd.getpwuidside_effect)rQ   rR   r   patchKeyErrorrS   rg   r   rZ   r[   called)rT   pfuns      r*   test_username_no_resolutionz'TestProcess.test_username_no_resolution   sg     NNZZ-8D 	QZZ\3qvvx}}+=>:::	 	 	   AB

Bc                     t        j                  d       t        | j                        }t	        j
                  | j                        j                         d   dz  }| j                  ||       y )N皙?r      )timesleepr>   r%   rQ   rR   memory_inforS   )rT   rss_ps
rss_psutils      r*   test_rss_memoryzTestProcess.test_rss_memory   R    
 	

3!^^DHH-99;A>E
,r1   c                     t        j                  d       t        | j                        }t	        j
                  | j                        j                         d   dz  }| j                  ||       y )Nrv   r   rw   )rx   ry   rB   r%   rQ   rR   rz   rS   )rT   vsz_ps
vsz_psutils      r*   test_vsz_memoryzTestProcess.test_vsz_memory   r~   r1   c                    t        | j                        }t        j                  j	                  |      j                         }t        j                  | j                        j                         j                         }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }| j                  ||       y )Nz\d.\dr4   z\d)r0   r%   ospathbasenamelowerrQ   rR   namer5   r6   rS   )rT   name_psname_psutils      r*   	test_namezTestProcess.test_name   s    $((#''""7+113nnTXX.335;;= &&2w/ffXr;7&&G,ffUB4+.r1   c                 (   d}g d}t        j                  d|      5  t        j                  d|      5  t        j                         }| j	                  |j                         d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nlong-program-name)long-program-name-extendedfoobarpsutil._psplatform.Process.namereturn_value"psutil._psplatform.Process.cmdliner   )r   rn   rQ   rR   rS   r   )rT   r   cmdlinerq   s       r*   test_name_longzTestProcess.test_name_long   s     #>ZZ9M 	I47 I NN$  +GH	I	I 	II I	I 	Is"   B5A<+B<B	BBc                 H   d}t        j                  d|      5  t        j                  dt        j                  dd            5  t        j                         }| j                  |j                         d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wNr   r   r   r   r   r4   rl   )r   rn   rQ   AccessDeniedrR   rS   r   rT   r   rq   s      r*   test_name_long_cmdline_ad_excz)TestProcess.test_name_long_cmdline_ad_exc   s     #ZZ9M 	@4"//26 @ NN$  +>?@	@ 	@@ @	@ 	@s#   ,B5B;BB	BB!c                 \   d}t        j                  d|      5  t        j                  dt        j                  dd            5  t        j                         }| j                  t        j                  |j                         d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr   )r   rn   rQ   NoSuchProcessrR   assertRaisesr   r   s      r*   test_name_long_cmdline_nsp_excz*TestProcess.test_name_long_cmdline_nsp_exc   s     #ZZ9M 	@4"00B7 @ NN$!!&"6"6?@	@ 	@@ @	@ 	@s#   ,B"?BB"B	B""B+zps -o start not availablec                    t        d| j                        }t        j                  | j                        j	                         }t
        j
                  j                  |      j                  d      }t        |      }t
        j
                  j                  |      j                  d      }| j                  |||g       y )Nr   z%H:%M:%S)
r   r%   rQ   rR   create_timedatetimefromtimestampstrftimeroundassertIn)rT   time_pstime_psutiltime_psutil_tstampround_time_psutilround_time_psutil_tstamps         r*   test_create_timezTestProcess.test_create_time   s    Wdhh'nnTXX.::<%..<<

(:
 	
 "+.#+#4#4#B#B$

(:
 	! 	g 24LMNr1   c                    t        | j                        }t        j                  | j                        j	                         }	 | j                  ||       y # t        $ r# |d t        |       }| j                  ||       Y y w xY wrM   )r0   r%   rQ   rR   exerS   AssertionErrorlen)rT   ps_pathnamepsutil_pathnameadjusted_ps_pathnames       r*   test_exezTestProcess.test_exe  sx    dhh' ..2668
	@[/: 	@ $//A[1A#B [*>?	@s   A )BBc                     t        | j                        }dj                  t        j                  | j                        j                               }| j                  ||       y )N )r8   r%   joinrQ   rR   r   rS   )rT   
ps_cmdlinepsutil_cmdlines      r*   test_cmdlinezTestProcess.test_cmdline  sD    TXX&
&..":"B"B"DE^4r1   znot reliable on SUNOSznot reliable on AIXc                     t        d| j                        }t        j                         j	                         }| j                  ||       y )Nnice)r   r%   rQ   rR   r   rS   )rT   ps_nicepsutil_nices      r*   	test_nicezTestProcess.test_nice*  s9     VTXX&nn&++-+.r1   N)__name__
__module____qualname____doc__classmethodrK   rN   rW   r^   rd   rj   rs   r   r   r}   r   r   r   r   r   unittestskipIfr   r   r   r   r   r	   r   r    r1   r*   rD   rD      s   L 
  /
-
-
7
 -  - -  -/I@
@ X__U\c#>?O @O@( 5 5 X__U34X__S/0/ 1 5/r1   rD   c                   V   e Zd ZdZ e       d        Z ej                  ed       ej                   e	d       d       ej                  e
 d      d                      Z e       d        Z e       d	        Zd
 Zd Zd Zd Z ej                  ed       e       d               Zy)TestSystemAPIszTest some system APIs.c                 p   t        t        d            }t        j                         }t        s
t
        rd|vr|j                  dd       t        |      t        |      z
  dkD  rH|D cg c]	  }||vs| c}|D cg c]	  }||vs| c}z   }| j                  dt        |      z         y c c}w c c}w )Nr%   r   r   zdifference: )
sortedr   rQ   pidsr   r   insertr   failr   )rT   pids_pspids_psutilx
differences        r*   	test_pidszTestSystemAPIs.test_pids6  s     E#kkm G 0NN1a  w<#k**Q.%0EAW4D!E"Ia{&:I J ))NS_<==	 /E Is   )	B.3B.=	B3B3zunreliable on SUNOSifconfigzno ifconfig cmdznot supportedc                     t        d      }t        j                  d      D ]@  }|j                         D ]  }|j	                  |      s * | j                  d|d|       y )Nzifconfig -aT)perniczcouldn't find z nic in 'ifconfig -a' output
)r   rQ   net_io_countersr.   
startswithr   )rT   r'   nicr)   s       r*   test_nic_nameszTestSystemAPIs.test_nic_namesJ  si     M"))6 	C ??3' iiF$ 	r1   c                    t        d      }|j                         st        j                  d      |j	                  d      }|D cg c]  }|j	                         d    }}|D cg c]  }|j	                         d    }}| j                  t        |      t        t        j                                      | j                  t        j                         |      5  t        t        j                               D ]o  \  }}| j                  |j                  ||          | j                  |j                  ||          |j                  Qt        j                  |j                         q 	 d d d        y c c}w c c}w # 1 sw Y   y xY w)Nwho -uno users on this system
r   r   rQ   who)r   r!   r   SkipTestr.   rS   r   rQ   userssubTest	enumerater   terminalr%   rR   )rT   r7   linesr   r   	terminalsidxus           r*   
test_userszTestSystemAPIs.test_usersZ  s,   lyy{##$=>>		$',-!1--+01aQWWYq\1	1US%89\\S\9 	*#FLLN3 *Q  s4  Ys^<55$NN155)	*	* 	* .1	* 	*s   E7&E<A.F!FF
c                    t        d      }|j                         st        j                  d      d }t	        j
                  d|      }|rd}nmt	        j
                  d|      }|rd}nRt	        j
                  d|      }|rd}n7t	        j
                  d	|      }|rd}|D cg c]  }|j                          }}|st        j                  d
|z        | j                  t        j                         |      5  t        t        j                               D ]R  \  }}t        j                  j                  |j                        j                  |      }| j                  |||          T 	 d d d        y c c}w # 1 sw Y   y xY w)Nr   r   z\d\d\d\d-\d\d-\d\d \d\d:\d\dz%Y-%m-%d %H:%Mz[A-Z][a-z][a-z] \d\d \d\d:\d\dz%b %d %H:%Mz[A-Z][a-z][a-z] \d\dz%b %dz[a-z][a-z][a-z] \d\dz(cannot interpret tstamp in who output
%sr   )r   r!   r   r   r5   findall
capitalizer   rQ   r   r   r   r   startedr   rS   )rT   r7   tstampr   r   r   r   psutil_values           r*   test_users_startedz!TestSystemAPIs.test_users_startedj  sb   lyy{##$=>>**<cB%F jj!BCHG& **%<cB$F !jj)@#FG!(;B"Ca1<<>"C"C##;sC  \\S\9 	=#FLLN3 =Q'00>>II (6"    ws|<	=	= 	= #D	= 	=s   "E7:A3E<<Fc                 ,   t        j                  dt        t        j                  d            5 }| j                  t        t        j                  j                  t        j                                |j                  sJ 	 d d d        y # 1 sw Y   y xY w)Nzpsutil._psposix.os.killr4   rl   )r   rn   OSErrorerrnoEBADFr   rQ   _psposix
pid_existsr   getpidrp   rT   ms     r*   test_pid_exists_let_raisez(TestSystemAPIs.test_pid_exists_let_raise  se     ZZ%75;;3K
 	gv'A'A299;O88O8		 	 	rt   c                 ,   t        j                  dt        t        j                  d            5 }| j                  t        t        j                  j                  t        j                                |j                  sJ 	 d d d        y # 1 sw Y   y xY w)Npsutil._psposix.os.waitpidr4   rl   )r   rn   r   r   r   r   rQ   r   wait_pidr   r   rp   r   s     r*   test_os_waitpid_let_raisez(TestSystemAPIs.test_os_waitpid_let_raise  se     ZZ(gekk26N
 	gv'?'?M88O8		 	 	rt   c                 X   t        j                  dt        t        j                  d            5 }| j                  t        j                  j                  t        j                  j                  t        j                         d       |j                  sJ 	 d d d        y # 1 sw Y   y xY w)Nr   r4   rl   g{Gz?)timeout)r   rn   r   r   EINTRr   rQ   r   TimeoutExpiredr   r   r   rp   r   s     r*   test_os_waitpid_eintrz$TestSystemAPIs.test_os_waitpid_eintr  s|    ZZ(gekk26N
 		..((			   88O8		 		 		s   A&B  B)c                     t        j                  dd      5 }| j                  t        t        j
                  j                  t        j                                |j                  sJ 	 d d d        y # 1 sw Y   y xY w)Nr   )r   r   )
r   rn   r   r#   rQ   r   r   r   r   rp   r   s     r*   test_os_waitpid_bad_ret_statusz-TestSystemAPIs.test_os_waitpid_bad_ret_status  s]    ZZ(w
 	FOO44biik 88O8	 	 	s   AA22A;zunreliable on AIXc                    d }d}t        j                  d      D ]  }t        j                  |j                        }	  ||j                        \  }}}}| j                  |j                  ||       | j                  |j                  ||       | j                  |j                  ||       | j                  |j                  |d        y # t        $ r0}	t        |	      j                         }	d|	v sd|	v sd	|	v rY d }	~	 d }	~	ww xY w)
Nc                    	 t        d| z        j                         }|j                  d      d   }|j                         }t        |d         dz  }t        |d         dz  }t        |d         dz  }t        |d	   j                  d
d            }||||fS # t        $ r6}dt        |      j	                         v rt        j                  d       d }~ww xY w)Nzdf -k %szdevice busyzdf returned EBUSYr   r   rw            %r4   )r   r!   RuntimeErrorr   r   r   r   r.   r"   floatr   )	devicer7   errr)   fieldstotalusedfreepercents	            r*   dfz*TestSystemAPIs.test_disk_usage.<locals>.df  s    f,-335
 99T?1%DZZ\Fq	NT)Evay>D(Dvay>D(DF1I--c267G4w//    CHNN$44"++,?@@s   B 	C"1CCi  @ F)all)deltar   zno such file or directoryzraw devices not supportedzpermission denied)rQ   disk_partitions
disk_usage
mountpointr  assertAlmostEqualr  r  r  r  r  r   r   )
rT   r  	tolerancepartusager  r  r  r  r  s
             r*   test_disk_usagezTestSystemAPIs.test_disk_usage  s   	0 $	**u5 	HD%%doo6EH-/_*tT7 &&u{{E&K&&uzz4y&I&&uzz4y&I&&u}}gQ&G)	H    #hnn&/362c9*c1s   C	D
%DDD
N)r   r   r   r   r   r   r   r   r	   r   r
   r   r   r   r   r   r  r  r   r!  r   r1   r*   r   r   2  s     > >& X__U12X__z**,=>X__,,o>
 ? ? 3
 * * $= $=L X__S-.%H  /%Hr1   r   c                       e Zd Zd Zy)TestMiscc                     t               }| j                  |d       | j                  |t        j                                 | j                  |t        j
                         y )Nr   )r   assertGreaterrS   resourcemmapPAGESIZE)rT   pagesizes     r*   test_getpagesizezTestMisc.test_getpagesize  sF    =8Q'8#7#7#9:4==1r1   N)r   r   r   r*  r   r1   r*   r#  r#    s    2r1   r#  __main__)run_from_namerM   )-r   r   r   r   r5   rG   rx   r   rQ   r   r   r   r   r   r   r	   psutil.testsr
   r   r   r   r   r   r   r   r   r   r   r'  r&  psutil._psutil_posixr   r   r0   r8   r>   rB   r   rD   r   r#  r   psutil.tests.runnerr,  __file__r   r1   r*   <module>r1     s#      	 	            , # " '  )  . ' "  	0)p UL)e/. e/ *e/P UL)pH^ pH *pHf UL)2~ 2 *2 z1( r1   