<s> """<STR_LIT>""" <EOL> import errno <EOL> import os <EOL> import sys <EOL> import platform <EOL> import _psutil_mswindows <EOL> from _psutil_mswindows import ERROR_ACCESS_DENIED <EOL> from psutil . error import AccessDenied , NoSuchProcess , TimeoutExpired <EOL> from psutil . _common import * <EOL> from psutil . _compat import PY3 , xrange , long <EOL> __extra__all__ = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> NUM_CPUS = _psutil_mswindows . get_num_cpus ( ) <EOL> BOOT_TIME = _psutil_mswindows . get_system_uptime ( ) <EOL> TOTAL_PHYMEM = _psutil_mswindows . get_virtual_mem ( ) [ <NUM_LIT:0> ] <EOL> WAIT_TIMEOUT = <NUM_LIT> <EOL> ACCESS_DENIED_SET = frozenset ( [ errno . EPERM , errno . EACCES , ERROR_ACCESS_DENIED ] ) <EOL> from _psutil_mswindows import ( ABOVE_NORMAL_PRIORITY_CLASS , <EOL> BELOW_NORMAL_PRIORITY_CLASS , <EOL> HIGH_PRIORITY_CLASS , <EOL> IDLE_PRIORITY_CLASS , <EOL> NORMAL_PRIORITY_CLASS , <EOL> REALTIME_PRIORITY_CLASS , <EOL> INFINITE ) <EOL> @ memoize <EOL> def _win32_QueryDosDevice ( s ) : <EOL> return _psutil_mswindows . win32_QueryDosDevice ( s ) <EOL> def _convert_raw_path ( s ) : <EOL> if PY3 and not isinstance ( s , str ) : <EOL> s = s . decode ( '<STR_LIT:utf8>' ) <EOL> rawdrive = '<STR_LIT:\\>' . join ( s . split ( '<STR_LIT:\\>' ) [ : <NUM_LIT:3> ] ) <EOL> driveletter = _win32_QueryDosDevice ( rawdrive ) <EOL> return os . path . join ( driveletter , s [ len ( rawdrive ) : ] ) <EOL> nt_virtmem_info = namedtuple ( '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def virtual_memory ( ) : <EOL> """<STR_LIT>""" <EOL> mem = _psutil_mswindows . get_virtual_mem ( ) <EOL> totphys , availphys , totpagef , availpagef , totvirt , freevirt = mem <EOL> total = totphys <EOL> avail = availphys <EOL> free = availphys <EOL> used = total - avail <EOL> percent = usage_percent ( ( total - avail ) , total , _round = <NUM_LIT:1> ) <EOL> return nt_virtmem_info ( total , avail , percent , used , free ) <EOL> def swap_memory ( ) : <EOL> """<STR_LIT>""" <EOL> mem = _psutil_mswindows . get_virtual_mem ( ) <EOL> total = mem [ <NUM_LIT:2> ] <EOL> free = mem [ <NUM_LIT:3> ] <EOL> used = total - free <EOL> percent = usage_percent ( used , total , _round = <NUM_LIT:1> ) <EOL> return nt_swapmeminfo ( total , used , free , percent , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def get_disk_usage ( path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> total , free = _psutil_mswindows . get_disk_usage ( path ) <EOL> except WindowsError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if not os . path . exists ( path ) : <EOL> raise OSError ( errno . ENOENT , "<STR_LIT>" % path ) <EOL> raise <EOL> used = total - free <EOL> percent = usage_percent ( used , total , _round = <NUM_LIT:1> ) <EOL> return nt_diskinfo ( total , used , free , percent ) <EOL> def disk_partitions ( all ) : <EOL> """<STR_LIT>""" <EOL> rawlist = _psutil_mswindows . get_disk_partitions ( all ) <EOL> return [ nt_partition ( * x ) for x in rawlist ] <EOL> _cputimes_ntuple = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_system_cpu_times ( ) : <EOL> """<STR_LIT>""" <EOL> user , system , idle = <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> for cpu_time in _psutil_mswindows . get_system_cpu_times ( ) : <EOL> user += cpu_time [ <NUM_LIT:0> ] <EOL> system += cpu_time [ <NUM_LIT:1> ] <EOL> idle += cpu_time [ <NUM_LIT:2> ] <EOL> return _cputimes_ntuple ( user , system , idle ) <EOL> def get_system_per_cpu_times ( ) : <EOL> """<STR_LIT>""" <EOL> ret = [ ] <EOL> for cpu_t in _psutil_mswindows . get_system_cpu_times ( ) : <EOL> user , system , idle = cpu_t <EOL> item = _cputimes_ntuple ( user , system , idle ) <EOL> ret . append ( item ) <EOL> return ret <EOL> def get_system_users ( ) : <EOL> """<STR_LIT>""" <EOL> retlist = [ ] <EOL> rawlist = _psutil_mswindows . get_system_users ( ) <EOL> for item in rawlist : <EOL> user , hostname , tstamp = item <EOL> nt = nt_user ( user , None , hostname , tstamp ) <EOL> retlist . append ( nt ) <EOL> return retlist <EOL> get_pid_list = _psutil_mswindows . get_pid_list <EOL> pid_exists = _psutil_mswindows . pid_exists <EOL> network_io_counters = _psutil_mswindows . get_network_io_counters <EOL> disk_io_counters = _psutil_mswindows . get_disk_io_counters <EOL> def wrap_exceptions ( callable ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> try : <EOL> return callable ( self , * args , ** kwargs ) <EOL> except OSError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if err . errno in ACCESS_DENIED_SET : <EOL> raise AccessDenied ( self . pid , self . _process_name ) <EOL> if err . errno == errno . ESRCH : <EOL> raise NoSuchProcess ( self . pid , self . _process_name ) <EOL> raise <EOL> return wrapper <EOL> class Process ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self , pid ) : <EOL> self . pid = pid <EOL> self . _process_name = None <EOL> @ wrap_exceptions <EOL> def get_process_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return _psutil_mswindows . get_process_name ( self . pid ) <EOL> @ wrap_exceptions <EOL> def get_process_exe ( self ) : <EOL> return _convert_raw_path ( _psutil_mswindows . get_process_exe ( self . pid ) ) <EOL> @ wrap_exceptions <EOL> def get_process_cmdline ( self ) : <EOL> """<STR_LIT>""" <EOL> return _psutil_mswindows . get_process_cmdline ( self . pid ) <EOL> @ wrap_exceptions <EOL> def get_process_ppid ( self ) : <EOL> """<STR_LIT>""" <EOL> return _psutil_mswindows . get_process_ppid ( self . pid ) <EOL> def _get_raw_meminfo ( self ) : <EOL> try : <EOL> return _psutil_mswindows . get_process_memory_info ( self . pid ) <EOL> except OSError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if err . errno in ACCESS_DENIED_SET : <EOL> return _psutil_mswindows . get_process_memory_info_2 ( self . pid ) <EOL> raise <EOL> @ wrap_exceptions <EOL> def get_memory_info ( self ) : <EOL> """<STR_LIT>""" <EOL> t = self . _get_raw_meminfo ( ) <EOL> return nt_meminfo ( t [ <NUM_LIT:2> ] , t [ <NUM_LIT:7> ] ) <EOL> _nt_ext_mem = namedtuple ( '<STR_LIT>' , <EOL> '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ] ) ) <EOL> @ wrap_exceptions <EOL> def get_ext_memory_info ( self ) : <EOL> return self . _nt_ext_mem ( * self . _get_raw_meminfo ( ) ) <EOL> nt_mmap_grouped = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> nt_mmap_ext = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_memory_maps ( self ) : <EOL> try : <EOL> raw = _psutil_mswindows . get_process_memory_maps ( self . pid ) <EOL> except OSError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if err . errno in ( errno . EPERM , errno . EACCES , ERROR_ACCESS_DENIED ) : <EOL> raise AccessDenied ( self . pid , self . _process_name ) <EOL> if err . errno == errno . ESRCH : <EOL> raise NoSuchProcess ( self . pid , self . _process_name ) <EOL> raise <EOL> else : <EOL> for addr , perm , path , rss in raw : <EOL> path = _convert_raw_path ( path ) <EOL> addr = hex ( addr ) <EOL> yield ( addr , perm , path , rss ) <EOL> @ wrap_exceptions <EOL> def kill_process ( self ) : <EOL> """<STR_LIT>""" <EOL> return _psutil_mswindows . kill_process ( self . pid ) <EOL> @ wrap_exceptions <EOL> def process_wait ( self , timeout = None ) : <EOL> if timeout is None : <EOL> timeout = INFINITE <EOL> else : <EOL> timeout = int ( timeout * <NUM_LIT:1000> ) <EOL> ret = _psutil_mswindows . process_wait ( self . pid , timeout ) <EOL> if ret == WAIT_TIMEOUT : <EOL> raise TimeoutExpired ( self . pid , self . _process_name ) <EOL> return ret <EOL> @ wrap_exceptions <EOL> def get_process_username ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . pid in ( <NUM_LIT:0> , <NUM_LIT:4> ) : <EOL> return '<STR_LIT>' <EOL> return _psutil_mswindows . get_process_username ( self . pid ) <EOL> @ wrap_exceptions <EOL> def get_process_create_time ( self ) : <EOL> if self . pid in ( <NUM_LIT:0> , <NUM_LIT:4> ) : <EOL> return BOOT_TIME <EOL> try : <EOL> return _psutil_mswindows . get_process_create_time ( self . pid ) <EOL> except OSError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if err . errno in ACCESS_DENIED_SET : <EOL> return _psutil_mswindows . get_process_create_time_2 ( self . pid ) <EOL> raise <EOL> @ wrap_exceptions <EOL> def get_process_num_threads ( self ) : <EOL> return _psutil_mswindows . get_process_num_threads ( self . pid ) <EOL> @ wrap_exceptions <EOL> def get_process_threads ( self ) : <EOL> rawlist = _psutil_mswindows . get_process_threads ( self . pid ) <EOL> retlist = [ ] <EOL> for thread_id , utime , stime in rawlist : <EOL> ntuple = nt_thread ( thread_id , utime , stime ) <EOL> retlist . append ( ntuple ) <EOL> return retlist <EOL> @ wrap_exceptions <EOL> def get_cpu_times ( self ) : <EOL> try : <EOL> ret = _psutil_mswindows . get_process_cpu_times ( self . pid ) <EOL> except OSError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if err . errno in ACCESS_DENIED_SET : <EOL> ret = _psutil_mswindows . get_process_cpu_times_2 ( self . pid ) <EOL> else : <EOL> raise <EOL> return nt_cputimes ( * ret ) <EOL> @ wrap_exceptions <EOL> def suspend_process ( self ) : <EOL> return _psutil_mswindows . suspend_process ( self . pid ) <EOL> @ wrap_exceptions <EOL> def resume_process ( self ) : <EOL> return _psutil_mswindows . resume_process ( self . pid ) <EOL> @ wrap_exceptions <EOL> def get_process_cwd ( self ) : <EOL> if self . pid in ( <NUM_LIT:0> , <NUM_LIT:4> ) : <EOL> raise AccessDenied ( self . pid , self . _process_name ) <EOL> path = _psutil_mswindows . get_process_cwd ( self . pid ) <EOL> return os . path . normpath ( path ) <EOL> @ wrap_exceptions <EOL> def get_open_files ( self ) : <EOL> if self . pid in ( <NUM_LIT:0> , <NUM_LIT:4> ) : <EOL> return [ ] <EOL> retlist = [ ] <EOL> raw_file_names = _psutil_mswindows . get_process_open_files ( self . pid ) <EOL> for file in raw_file_names : <EOL> file = _convert_raw_path ( file ) <EOL> if isfile_strict ( file ) and file not in retlist : <EOL> ntuple = nt_openfile ( file , - <NUM_LIT:1> ) <EOL> retlist . append ( ntuple ) <EOL> return retlist <EOL> @ wrap_exceptions <EOL> def get_connections ( self , kind = '<STR_LIT>' ) : <EOL> if kind not in conn_tmap : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % ( kind , '<STR_LIT:U+002CU+0020>' . join ( [ repr ( x ) for x in conn_tmap ] ) ) ) <EOL> families , types = conn_tmap [ kind ] <EOL> ret = _psutil_mswindows . get_process_connections ( self . pid , families , types ) <EOL> return [ nt_connection ( * conn ) for conn in ret ] <EOL> @ wrap_exceptions <EOL> def get_process_nice ( self ) : <EOL> return _psutil_mswindows . get_process_priority ( self . pid ) <EOL> @ wrap_exceptions <EOL> def set_process_nice ( self , value ) : <EOL> return _psutil_mswindows . set_process_priority ( self . pid , value ) <EOL> @ wrap_exceptions <EOL> def get_process_io_counters ( self ) : <EOL> try : <EOL> ret = _psutil_mswindows . get_process_io_counters ( self . pid ) <EOL> except OSError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if err . errno in ACCESS_DENIED_SET : <EOL> ret = _psutil_mswindows . get_process_io_counters_2 ( self . pid ) <EOL> else : <EOL> raise <EOL> return nt_io ( * ret ) <EOL> @ wrap_exceptions <EOL> def get_process_status ( self ) : <EOL> suspended = _psutil_mswindows . is_process_suspended ( self . pid ) <EOL> if suspended : <EOL> return STATUS_STOPPED <EOL> else : <EOL> return STATUS_RUNNING <EOL> @ wrap_exceptions <EOL> def get_process_cpu_affinity ( self ) : <EOL> from_bitmask = lambda x : [ i for i in xrange ( <NUM_LIT:64> ) if ( <NUM_LIT:1> << i ) & x ] <EOL> bitmask = _psutil_mswindows . get_process_cpu_affinity ( self . pid ) <EOL> return from_bitmask ( bitmask ) <EOL> @ wrap_exceptions <EOL> def set_process_cpu_affinity ( self , value ) : <EOL> def to_bitmask ( l ) : <EOL> if not l : <EOL> raise ValueError ( "<STR_LIT>" % l ) <EOL> out = <NUM_LIT:0> <EOL> for b in l : <EOL> if not isinstance ( b , ( int , long ) ) or b < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % b ) <EOL> out |= <NUM_LIT:2> ** b <EOL> return out <EOL> allcpus = list ( range ( len ( get_system_per_cpu_times ( ) ) ) ) <EOL> for cpu in value : <EOL> if cpu not in allcpus : <EOL> raise ValueError ( "<STR_LIT>" % cpu ) <EOL> bitmask = to_bitmask ( value ) <EOL> _psutil_mswindows . set_process_cpu_affinity ( self . pid , bitmask ) <EOL> @ wrap_exceptions <EOL> def get_num_handles ( self ) : <EOL> try : <EOL> return _psutil_mswindows . get_process_num_handles ( self . pid ) <EOL> except OSError : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if err . errno in ACCESS_DENIED_SET : <EOL> return _psutil_mswindows . get_process_num_handles_2 ( self . pid ) <EOL> raise <EOL> @ wrap_exceptions <EOL> def get_num_ctx_switches ( self ) : <EOL> return nt_ctxsw ( * _psutil_mswindows . get_process_num_ctx_switches ( self . pid ) ) </s>
