<s> """<STR_LIT>""" <EOL> import datetime <EOL> import os <EOL> import subprocess <EOL> import sys <EOL> import time <EOL> import psutil <EOL> from psutil import BSD <EOL> from psutil import FREEBSD <EOL> from psutil import NETBSD <EOL> from psutil import OPENBSD <EOL> from psutil . _compat import PY3 <EOL> from psutil . tests import get_test_subprocess <EOL> from psutil . tests import MEMORY_TOLERANCE <EOL> from psutil . tests import reap_children <EOL> from psutil . tests import retry_before_failing <EOL> from psutil . tests import run_test_module_by_name <EOL> from psutil . tests import sh <EOL> from psutil . tests import unittest <EOL> from psutil . tests import which <EOL> if BSD : <EOL> PAGESIZE = os . sysconf ( "<STR_LIT>" ) <EOL> if os . getuid ( ) == <NUM_LIT:0> : <EOL> MUSE_AVAILABLE = which ( '<STR_LIT>' ) <EOL> else : <EOL> MUSE_AVAILABLE = False <EOL> else : <EOL> MUSE_AVAILABLE = False <EOL> def sysctl ( cmdline ) : <EOL> """<STR_LIT>""" <EOL> result = sh ( "<STR_LIT>" + cmdline ) <EOL> if FREEBSD : <EOL> result = result [ result . find ( "<STR_LIT>" ) + <NUM_LIT:2> : ] <EOL> elif OPENBSD or NETBSD : <EOL> result = result [ result . find ( "<STR_LIT:=>" ) + <NUM_LIT:1> : ] <EOL> try : <EOL> return int ( result ) <EOL> except ValueError : <EOL> return result <EOL> def muse ( field ) : <EOL> """<STR_LIT>""" <EOL> out = sh ( '<STR_LIT>' ) <EOL> for line in out . split ( '<STR_LIT:\n>' ) : <EOL> if line . startswith ( field ) : <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return int ( line . split ( ) [ <NUM_LIT:1> ] ) <EOL> @ unittest . skipUnless ( BSD , "<STR_LIT>" ) <EOL> class BSDSpecificTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . pid = get_test_subprocess ( ) . pid <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> reap_children ( ) <EOL> def test_process_create_time ( self ) : <EOL> cmdline = "<STR_LIT>" % self . pid <EOL> p = subprocess . Popen ( cmdline , shell = <NUM_LIT:1> , stdout = subprocess . PIPE ) <EOL> output = p . communicate ( ) [ <NUM_LIT:0> ] <EOL> if PY3 : <EOL> output = str ( output , sys . stdout . encoding ) <EOL> start_ps = output . replace ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> start_psutil = psutil . Process ( self . pid ) . create_time ( ) <EOL> start_psutil = time . strftime ( "<STR_LIT>" , <EOL> time . localtime ( start_psutil ) ) <EOL> self . assertEqual ( start_ps , start_psutil ) <EOL> def test_disks ( self ) : <EOL> def df ( path ) : <EOL> out = sh ( '<STR_LIT>' % path ) . strip ( ) <EOL> lines = out . split ( '<STR_LIT:\n>' ) <EOL> lines . pop ( <NUM_LIT:0> ) <EOL> line = lines . pop ( <NUM_LIT:0> ) <EOL> dev , total , used , free = line . split ( ) [ : <NUM_LIT:4> ] <EOL> if dev == '<STR_LIT:none>' : <EOL> dev = '<STR_LIT>' <EOL> total = int ( total ) * <NUM_LIT> <EOL> used = int ( used ) * <NUM_LIT> <EOL> free = int ( free ) * <NUM_LIT> <EOL> return dev , total , used , free <EOL> for part in psutil . disk_partitions ( all = False ) : <EOL> usage = psutil . disk_usage ( part . mountpoint ) <EOL> dev , total , used , free = df ( part . mountpoint ) <EOL> self . assertEqual ( part . device , dev ) <EOL> self . assertEqual ( usage . total , total ) <EOL> if abs ( usage . free - free ) > <NUM_LIT:10> * <NUM_LIT> * <NUM_LIT> : <EOL> self . fail ( "<STR_LIT>" % ( usage . free , free ) ) <EOL> if abs ( usage . used - used ) > <NUM_LIT:10> * <NUM_LIT> * <NUM_LIT> : <EOL> self . fail ( "<STR_LIT>" % ( usage . used , used ) ) <EOL> def test_cpu_count_logical ( self ) : <EOL> syst = sysctl ( "<STR_LIT>" ) <EOL> self . assertEqual ( psutil . cpu_count ( logical = True ) , syst ) <EOL> def test_virtual_memory_total ( self ) : <EOL> num = sysctl ( '<STR_LIT>' ) <EOL> self . assertEqual ( num , psutil . virtual_memory ( ) . total ) <EOL> @ unittest . skipUnless ( FREEBSD , "<STR_LIT>" ) <EOL> class FreeBSDSpecificTestCase ( unittest . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . pid = get_test_subprocess ( ) . pid <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> reap_children ( ) <EOL> def test_boot_time ( self ) : <EOL> s = sysctl ( '<STR_LIT>' ) <EOL> s = s [ s . find ( "<STR_LIT>" ) + <NUM_LIT:7> : ] <EOL> s = s [ : s . find ( '<STR_LIT:U+002C>' ) ] <EOL> btime = int ( s ) <EOL> self . assertEqual ( btime , psutil . boot_time ( ) ) <EOL> @ retry_before_failing ( ) <EOL> def test_memory_maps ( self ) : <EOL> out = sh ( '<STR_LIT>' % self . pid ) <EOL> maps = psutil . Process ( self . pid ) . memory_maps ( grouped = False ) <EOL> lines = out . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> : ] <EOL> while lines : <EOL> line = lines . pop ( ) <EOL> fields = line . split ( ) <EOL> _ , start , stop , perms , res = fields [ : <NUM_LIT:5> ] <EOL> map = maps . pop ( ) <EOL> self . assertEqual ( "<STR_LIT>" % ( start , stop ) , map . addr ) <EOL> self . assertEqual ( int ( res ) , map . rss ) <EOL> if not map . path . startswith ( '<STR_LIT:[>' ) : <EOL> self . assertEqual ( fields [ <NUM_LIT:10> ] , map . path ) <EOL> def test_exe ( self ) : <EOL> out = sh ( '<STR_LIT>' % self . pid ) <EOL> self . assertEqual ( psutil . Process ( self . pid ) . exe ( ) , <EOL> out . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> ] . split ( ) [ - <NUM_LIT:1> ] ) <EOL> def test_cmdline ( self ) : <EOL> out = sh ( '<STR_LIT>' % self . pid ) <EOL> self . assertEqual ( '<STR_LIT:U+0020>' . join ( psutil . Process ( self . pid ) . cmdline ( ) ) , <EOL> '<STR_LIT:U+0020>' . join ( out . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> ] . split ( ) [ <NUM_LIT:2> : ] ) ) <EOL> def test_uids_gids ( self ) : <EOL> out = sh ( '<STR_LIT>' % self . pid ) <EOL> euid , ruid , suid , egid , rgid , sgid = out . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> ] . split ( ) [ <NUM_LIT:2> : <NUM_LIT:8> ] <EOL> p = psutil . Process ( self . pid ) <EOL> uids = p . uids ( ) <EOL> gids = p . gids ( ) <EOL> self . assertEqual ( uids . real , int ( ruid ) ) <EOL> self . assertEqual ( uids . effective , int ( euid ) ) <EOL> self . assertEqual ( uids . saved , int ( suid ) ) <EOL> self . assertEqual ( gids . real , int ( rgid ) ) <EOL> self . assertEqual ( gids . effective , int ( egid ) ) <EOL> self . assertEqual ( gids . saved , int ( sgid ) ) <EOL> @ retry_before_failing ( ) <EOL> def test_vmem_active ( self ) : <EOL> syst = sysctl ( "<STR_LIT>" ) * PAGESIZE <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . active , syst , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ retry_before_failing ( ) <EOL> def test_vmem_inactive ( self ) : <EOL> syst = sysctl ( "<STR_LIT>" ) * PAGESIZE <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . inactive , syst , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ retry_before_failing ( ) <EOL> def test_vmem_wired ( self ) : <EOL> syst = sysctl ( "<STR_LIT>" ) * PAGESIZE <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . wired , syst , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ retry_before_failing ( ) <EOL> def test_vmem_cached ( self ) : <EOL> syst = sysctl ( "<STR_LIT>" ) * PAGESIZE <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . cached , syst , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ retry_before_failing ( ) <EOL> def test_vmem_free ( self ) : <EOL> syst = sysctl ( "<STR_LIT>" ) * PAGESIZE <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . free , syst , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ retry_before_failing ( ) <EOL> def test_vmem_buffers ( self ) : <EOL> syst = sysctl ( "<STR_LIT>" ) <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . buffers , syst , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ unittest . skipUnless ( MUSE_AVAILABLE , "<STR_LIT>" ) <EOL> def test_muse_vmem_total ( self ) : <EOL> num = muse ( '<STR_LIT>' ) <EOL> self . assertEqual ( psutil . virtual_memory ( ) . total , num ) <EOL> @ unittest . skipUnless ( MUSE_AVAILABLE , "<STR_LIT>" ) <EOL> @ retry_before_failing ( ) <EOL> def test_muse_vmem_active ( self ) : <EOL> num = muse ( '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . active , num , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ unittest . skipUnless ( MUSE_AVAILABLE , "<STR_LIT>" ) <EOL> @ retry_before_failing ( ) <EOL> def test_muse_vmem_inactive ( self ) : <EOL> num = muse ( '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . inactive , num , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ unittest . skipUnless ( MUSE_AVAILABLE , "<STR_LIT>" ) <EOL> @ retry_before_failing ( ) <EOL> def test_muse_vmem_wired ( self ) : <EOL> num = muse ( '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . wired , num , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ unittest . skipUnless ( MUSE_AVAILABLE , "<STR_LIT>" ) <EOL> @ retry_before_failing ( ) <EOL> def test_muse_vmem_cached ( self ) : <EOL> num = muse ( '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . cached , num , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ unittest . skipUnless ( MUSE_AVAILABLE , "<STR_LIT>" ) <EOL> @ retry_before_failing ( ) <EOL> def test_muse_vmem_free ( self ) : <EOL> num = muse ( '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . free , num , <EOL> delta = MEMORY_TOLERANCE ) <EOL> @ unittest . skipUnless ( MUSE_AVAILABLE , "<STR_LIT>" ) <EOL> @ retry_before_failing ( ) <EOL> def test_muse_vmem_buffers ( self ) : <EOL> num = muse ( '<STR_LIT>' ) <EOL> self . assertAlmostEqual ( psutil . virtual_memory ( ) . buffers , num , <EOL> delta = MEMORY_TOLERANCE ) <EOL> def test_cpu_stats_ctx_switches ( self ) : <EOL> self . assertAlmostEqual ( psutil . cpu_stats ( ) . ctx_switches , <EOL> sysctl ( '<STR_LIT>' ) , delta = <NUM_LIT:1000> ) <EOL> def test_cpu_stats_interrupts ( self ) : <EOL> self . assertAlmostEqual ( psutil . cpu_stats ( ) . interrupts , <EOL> sysctl ( '<STR_LIT>' ) , delta = <NUM_LIT:1000> ) <EOL> def test_cpu_stats_soft_interrupts ( self ) : <EOL> self . assertAlmostEqual ( psutil . cpu_stats ( ) . soft_interrupts , <EOL> sysctl ( '<STR_LIT>' ) , delta = <NUM_LIT:1000> ) <EOL> def test_cpu_stats_syscalls ( self ) : <EOL> self . assertAlmostEqual ( psutil . cpu_stats ( ) . syscalls , <EOL> sysctl ( '<STR_LIT>' ) , delta = <NUM_LIT:1000> ) <EOL> @ unittest . skipUnless ( OPENBSD , "<STR_LIT>" ) <EOL> class OpenBSDSpecificTestCase ( unittest . TestCase ) : <EOL> def test_boot_time ( self ) : <EOL> s = sysctl ( '<STR_LIT>' ) <EOL> sys_bt = datetime . datetime . strptime ( s , "<STR_LIT>" ) <EOL> psutil_bt = datetime . datetime . fromtimestamp ( psutil . boot_time ( ) ) <EOL> self . assertEqual ( sys_bt , psutil_bt ) <EOL> @ unittest . skipUnless ( NETBSD , "<STR_LIT>" ) <EOL> class NetBSDSpecificTestCase ( unittest . TestCase ) : <EOL> def parse_meminfo ( self , look_for ) : <EOL> with open ( '<STR_LIT>' , '<STR_LIT:rb>' ) as f : <EOL> for line in f : <EOL> if line . startswith ( look_for ) : <EOL> return int ( line . split ( ) [ <NUM_LIT:1> ] ) * <NUM_LIT> <EOL> raise ValueError ( "<STR_LIT>" % look_for ) <EOL> def test_vmem_buffers ( self ) : <EOL> self . assertEqual ( <EOL> psutil . virtual_memory ( ) . buffers , self . parse_meminfo ( "<STR_LIT>" ) ) <EOL> def test_vmem_shared ( self ) : <EOL> self . assertEqual ( <EOL> psutil . virtual_memory ( ) . shared , self . parse_meminfo ( "<STR_LIT>" ) ) <EOL> def test_swapmem_total ( self ) : <EOL> self . assertEqual ( <EOL> psutil . swap_memory ( ) . total , self . parse_meminfo ( "<STR_LIT>" ) ) <EOL> def test_swapmem_free ( self ) : <EOL> self . assertEqual ( <EOL> psutil . swap_memory ( ) . free , self . parse_meminfo ( "<STR_LIT>" ) ) <EOL> def test_cpu_stats_interrupts ( self ) : <EOL> with open ( '<STR_LIT>' , '<STR_LIT:rb>' ) as f : <EOL> for line in f : <EOL> if line . startswith ( b'<STR_LIT>' ) : <EOL> interrupts = int ( line . split ( ) [ <NUM_LIT:1> ] ) <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . assertAlmostEqual ( <EOL> psutil . cpu_stats ( ) . interrupts , interrupts , delta = <NUM_LIT:1000> ) <EOL> def test_cpu_stats_ctx_switches ( self ) : <EOL> with open ( '<STR_LIT>' , '<STR_LIT:rb>' ) as f : <EOL> for line in f : <EOL> if line . startswith ( b'<STR_LIT>' ) : <EOL> ctx_switches = int ( line . split ( ) [ <NUM_LIT:1> ] ) <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . assertAlmostEqual ( <EOL> psutil . cpu_stats ( ) . ctx_switches , ctx_switches , delta = <NUM_LIT:1000> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_test_module_by_name ( __file__ ) </s>
