U
    !ºÖh0  ã                   @   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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j ed¡Zej ed¡Zejjeo(ej e¡ ddG dd„ deƒƒZejjeoXej e¡ ddG dd„ deƒƒZ ejjeoˆej e¡ ddG dd„ deƒƒZ!dS )zTest various scripts.é    N)ÚPOSIX)ÚWINDOWS)Ú
CI_TESTING)ÚHAS_BATTERY)ÚHAS_MEMORY_MAPS)ÚHAS_SENSORS_BATTERY)ÚHAS_SENSORS_FANS)ÚHAS_SENSORS_TEMPERATURES)Ú
PYTHON_EXE)ÚPYTHON_EXE_ENV)ÚROOT_DIR)ÚSCRIPTS_DIR)ÚPsutilTestCase)Úimport_module_by_path)Úpsutil)ÚshZinternalzsetup.pyzcan't find scripts/ directory©Úreasonc                   @   s   e Zd Zedd„ ƒZedd„ ƒZdd„ Zejj	e
 ddd	d
„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zejj	eote ¡  dddd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zejj	e dddd „ ƒZd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zejj	e  d-dd.d/„ ƒZ!d0d1„ Z"ejj	e# ddd2d3„ ƒZ$ejj	e% ddd4d5„ ƒZ&ejj	e' ddejj	e( d6dd7d8„ ƒƒZ)ejj	e' ddejj	e( d6dd9d:„ ƒƒZ*d;S )<ÚTestExampleScriptsc              
   O   sœ   |  dt¡ tj t| ¡} t| g}|D ]}| |¡ q&zt|f|Ž 	¡ }W n@ t
k
rŠ } z"dt|ƒkrxt|ƒ W Y ¢S ‚ W 5 d }~X Y nX |s˜t|ƒ‚|S )NÚenvZAccessDenied)Ú
setdefaultr   ÚosÚpathÚjoinr   r
   Úappendr   ÚstripÚRuntimeErrorÚstrÚAssertionError)ÚexeÚargsÚkwargsÚcmdÚargÚoutÚerr© r&   ú?./venv/lib/python3.8/site-packages/psutil/tests/test_scripts.pyÚassert_stdout0   s    z TestExampleScripts.assert_stdoutc              	   C   s<   t j t| ¡} t| dd}| ¡ }W 5 Q R X t |¡ d S ©NÚutf8)Úencoding)r   r   r   r   ÚopenÚreadÚastÚparse)r   ÚfÚsrcr&   r&   r'   Úassert_syntaxA   s    z TestExampleScripts.assert_syntaxc                 C   sZ   t | ƒ}t t¡D ]B}| d¡rdtj |¡d  |krt dtj 	t|¡›d¡‚qd S )Nú.pyZtest_r   zno test defined for z script)
Údirr   Úlistdirr   Úendswithr   ÚsplitextÚpytestÚfailr   )ÚselfZmethsÚnamer&   r&   r'   Útest_coverageH   s    
ÿz TestExampleScripts.test_coveragez
POSIX onlyr   c                 C   sb   t  t¡D ]R\}}}|D ]B}| d¡rt j ||¡}tjt  |¡tj @ st	 
|›d¡‚qq
d S )Nr3   z is not executable)r   Úwalkr   r6   r   r   ÚstatÚS_IXUSRÚST_MODEr8   r9   )r:   ÚrootÚdirsÚfilesÚfiler   r&   r&   r'   Útest_executableT   s    
z"TestExampleScripts.test_executablec                 C   s   |   d¡ d S )Nzdisk_usage.py©r(   ©r:   r&   r&   r'   Útest_disk_usage]   s    z"TestExampleScripts.test_disk_usagec                 C   s   |   d¡ d S )Nzfree.pyrF   rG   r&   r&   r'   Ú	test_free`   s    zTestExampleScripts.test_freec                 C   s   |   d¡ d S )Nz
meminfo.pyrF   rG   r&   r&   r'   Útest_meminfoc   s    zTestExampleScripts.test_meminfoc                 C   s   |   dtt ¡ ƒ¡ d S )Nzprocinfo.py©r(   r   r   ÚgetpidrG   r&   r&   r'   Útest_procinfof   s    z TestExampleScripts.test_procinfozno usersc                 C   s   |   d¡ d S )Nzwho.pyrF   rG   r&   r&   r'   Útest_whoi   s    zTestExampleScripts.test_whoc                 C   s   |   d¡ d S )Nzps.pyrF   rG   r&   r&   r'   Útest_psm   s    zTestExampleScripts.test_psc                 C   s   |   d¡ d S )Nz	pstree.pyrF   rG   r&   r&   r'   Útest_pstreep   s    zTestExampleScripts.test_pstreec                 C   s   |   d¡ d S )Nz
netstat.pyrF   rG   r&   r&   r'   Útest_netstats   s    zTestExampleScripts.test_netstatc                 C   s   |   d¡ d S )Nzifconfig.pyrF   rG   r&   r&   r'   Útest_ifconfigv   s    z TestExampleScripts.test_ifconfigúnot supportedc                 C   s   |   dtt ¡ ƒ¡ d S )Nzpmap.pyrK   rG   r&   r&   r'   Ú	test_pmapy   s    zTestExampleScripts.test_pmapc                 C   s*   dt  ¡  ¡ jkrt d¡‚|  d¡ d S )NZussrS   zprocsmem.py)r   ÚProcessZmemory_full_infoÚ_fieldsr8   Úskipr(   rG   r&   r&   r'   Útest_procsmem}   s    
z TestExampleScripts.test_procsmemc                 C   s   |   d¡ d S )Nz
killall.py©r2   rG   r&   r&   r'   Útest_killall‚   s    zTestExampleScripts.test_killallc                 C   s   |   d¡ d S )Nz	nettop.pyrY   rG   r&   r&   r'   Útest_nettop…   s    zTestExampleScripts.test_nettopc                 C   s   |   d¡ d S )Nztop.pyrY   rG   r&   r&   r'   Útest_topˆ   s    zTestExampleScripts.test_topc                 C   s   |   d¡ d S )Nziotop.pyrY   rG   r&   r&   r'   Ú
test_iotop‹   s    zTestExampleScripts.test_iotopc                 C   s,   |   dt ¡  ¡ ¡}tt ¡ ƒ|ks(t‚d S )Nzpidof.py)r(   r   rU   r;   r   r   rL   r   )r:   Úoutputr&   r&   r'   Ú
test_pidofŽ   s    zTestExampleScripts.test_pidofzWINDOWS onlyc                 C   s   |   d¡ d S )Nzwinservices.pyrF   rG   r&   r&   r'   Útest_winservices’   s    z#TestExampleScripts.test_winservicesc                 C   s   |   d¡ d S )Nzcpu_distribution.pyrY   rG   r&   r&   r'   Útest_cpu_distribution–   s    z(TestExampleScripts.test_cpu_distributionc                 C   s    t  ¡ st d¡‚|  d¡ d S )Nzno temperaturesztemperatures.py)r   Zsensors_temperaturesr8   rW   r(   rG   r&   r&   r'   Útest_temperatures™   s    
z$TestExampleScripts.test_temperaturesc                 C   s    t  ¡ st d¡‚|  d¡ d S )Nzno fanszfans.py)r   Zsensors_fansr8   rW   r(   rG   r&   r&   r'   Ú	test_fansŸ   s    
zTestExampleScripts.test_fansz
no batteryc                 C   s   |   d¡ d S )Nz
battery.pyrF   rG   r&   r&   r'   Útest_battery¥   s    zTestExampleScripts.test_batteryc                 C   s   |   d¡ d S )Nz
sensors.pyrF   rG   r&   r&   r'   Útest_sensorsª   s    zTestExampleScripts.test_sensorsN)+Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr(   r2   r<   r8   ÚmarkÚskipifr   rE   rH   rI   rJ   rM   r   r   ZusersrN   rO   rP   rQ   rR   r   rT   rX   rZ   r[   r\   r]   r_   r   r`   ra   r	   rb   r   rc   r   r   rd   re   r&   r&   r&   r'   r   +   sL   







r   z&can't find scripts/internal/ directoryc                   @   s8   e Zd Zedd„ ƒZdd„ Zejje	dddd„ ƒZ
d	S )
ÚTestInternalScriptsc                  c   s.   t  t¡D ]} |  d¡r
t j t| ¡V  q
d S )Nr3   )r   r5   ÚINTERNAL_SCRIPTS_DIRr6   r   r   )r;   r&   r&   r'   Úlsº   s    
zTestInternalScripts.lsc              
   C   s<   |   ¡ D ].}t|dd}| ¡ }W 5 Q R X t |¡ qd S r)   )rn   r,   r-   r.   r/   )r:   r   r0   Údatar&   r&   r'   Útest_syntax_allÀ   s    z#TestInternalScripts.test_syntax_allz	not on CIr   c              	   C   s4   |   ¡ D ]&}zt|ƒ W q tk
r,   Y qX qd S ©N)rn   r   Ú
SystemExit)r:   r   r&   r&   r'   Útest_import_allÆ   s
    z#TestInternalScripts.test_import_allN)rf   rg   rh   ri   rn   rp   r8   rj   rk   r   rs   r&   r&   r&   r'   rl   µ   s
   
rl   zcan't find setup.pyc                   @   s4   e Zd Zdd„ Zejje d¡ dddd„ ƒZ	dS )	ÚTestSetupScriptc              	   C   s<   t tƒ}t t¡ | ¡  W 5 Q R X | ¡ tjks8t	‚d S rq   )
r   ÚSETUP_PYr8   Zraisesrr   ZsetupZget_versionr   Ú__version__r   )r:   Úmoduler&   r&   r'   Útest_invocationØ   s    zTestSetupScript.test_invocationú	python2.7zpython2.7 not installedr   c                 C   s`   t jt d¡tgt jt jdd}| ¡ \}}| ¡ dks<t‚|rDt‚d|ksPt‚d|ks\t‚d S )Nry   T)ÚstdoutÚstderrZuniversal_newlinesé   z$psutil no longer supports Python 2.7z'Latest version supporting Python 2.7 is)	Ú
subprocessÚPopenÚshutilÚwhichru   ÚPIPEZcommunicateÚwaitr   )r:   Úprz   r{   r&   r&   r'   Útest_python2Þ   s    üzTestSetupScript.test_python2N)
rf   rg   rh   rx   r8   rj   rk   r   r€   r„   r&   r&   r&   r'   rt   Ô   s   
 ÿrt   )"Ú__doc__r.   r   r   r>   r}   r8   r   r   r   Zpsutil.testsr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   rm   ru   rj   rk   Úexistsr   rl   rt   r&   r&   r&   r'   Ú<module>   sP   þ þ ÿ