Skip to content

Commit

Permalink
expose OS constants
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 7, 2016
1 parent e4e16ab commit 907bde6
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 54 deletions.
16 changes: 16 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,22 @@ Popen class
Constants
=========

.. _const-oses:
.. data:: POSIX
WINDOWS
LINUX
OSX
FREEBSD
NETBSD
OPENBSD
BSD
SUNOS

``bool`` constants which define what platform you're on. E.g. if on Windows,
*WINDOWS* constant will be ``True``, all athers will be ``False``.

.. versionadded:: 3.5.0

.. _const-procfs_path:
.. data:: PROCFS_PATH

Expand Down
75 changes: 43 additions & 32 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@
from ._common import NIC_DUPLEX_HALF
from ._common import NIC_DUPLEX_UNKNOWN

from ._common import BSD as _BSD
from ._common import LINUX as _LINUX
from ._common import OPENBSD as _OPENBSD
from ._common import OSX as _OSX
from ._common import POSIX as _POSIX
from ._common import SUNOS as _SUNOS
from ._common import WINDOWS as _WINDOWS


if _LINUX:
from ._common import BSD
from ._common import FREEBSD # NOQA
from ._common import LINUX
from ._common import NETBSD # NOQA
from ._common import OPENBSD # NOQA
from ._common import OSX
from ._common import POSIX # NOQA
from ._common import SUNOS
from ._common import WINDOWS

if LINUX:
# This is public API and it will be retrieved from _pslinux.py
# via sys.modules.
PROCFS_PATH = "/proc"
Expand Down Expand Up @@ -119,7 +120,7 @@
except AttributeError:
pass

elif _WINDOWS:
elif WINDOWS:
from . import _pswindows as _psplatform
from ._psutil_windows import ABOVE_NORMAL_PRIORITY_CLASS # NOQA
from ._psutil_windows import BELOW_NORMAL_PRIORITY_CLASS # NOQA
Expand All @@ -129,13 +130,13 @@
from ._psutil_windows import REALTIME_PRIORITY_CLASS # NOQA
from ._pswindows import CONN_DELETE_TCB # NOQA

elif _OSX:
elif OSX:
from . import _psosx as _psplatform

elif _BSD:
elif BSD:
from . import _psbsd as _psplatform

elif _SUNOS:
elif SUNOS:
from . import _pssunos as _psplatform
from ._pssunos import CONN_BOUND # NOQA
from ._pssunos import CONN_IDLE # NOQA
Expand All @@ -152,18 +153,28 @@
# exceptions
"Error", "NoSuchProcess", "ZombieProcess", "AccessDenied",
"TimeoutExpired",

# constants
"version_info", "__version__",

"STATUS_RUNNING", "STATUS_IDLE", "STATUS_SLEEPING", "STATUS_DISK_SLEEP",
"STATUS_STOPPED", "STATUS_TRACING_STOP", "STATUS_ZOMBIE", "STATUS_DEAD",
"STATUS_WAKING", "STATUS_LOCKED", "STATUS_WAITING", "STATUS_LOCKED",

"CONN_ESTABLISHED", "CONN_SYN_SENT", "CONN_SYN_RECV", "CONN_FIN_WAIT1",
"CONN_FIN_WAIT2", "CONN_TIME_WAIT", "CONN_CLOSE", "CONN_CLOSE_WAIT",
"CONN_LAST_ACK", "CONN_LISTEN", "CONN_CLOSING", "CONN_NONE",

"AF_LINK",

"NIC_DUPLEX_FULL", "NIC_DUPLEX_HALF", "NIC_DUPLEX_UNKNOWN",

"BSD", "FREEBSD", "LINUX", "NETBSD", "OPENBSD", "OSX", "POSIX", "SUNOS",
"WINDOWS",

# classes
"Process", "Popen",

# functions
"pid_exists", "pids", "process_iter", "wait_procs", # proc
"virtual_memory", "swap_memory", # memory
Expand Down Expand Up @@ -530,7 +541,7 @@ def ppid(self):

# XXX should we check creation time here rather than in
# Process.parent()?
if _POSIX:
if POSIX:
return self._proc.ppid()
else:
self._ppid = self._ppid or self._proc.ppid()
Expand All @@ -541,10 +552,10 @@ def name(self):
# Process name is only cached on Windows as on POSIX it may
# change, see:
# https://github.com/giampaolo/psutil/issues/692
if _WINDOWS and self._name is not None:
if WINDOWS and self._name is not None:
return self._name
name = self._proc.name()
if _POSIX and len(name) >= 15:
if POSIX and len(name) >= 15:
# On UNIX the name gets truncated to the first 15 characters.
# If it matches the first part of the cmdline we return that
# one instead because it's usually more explicative.
Expand Down Expand Up @@ -616,7 +627,7 @@ def username(self):
"""The name of the user that owns the process.
On UNIX this is calculated by using *real* process uid.
"""
if _POSIX:
if POSIX:
if pwd is None:
# might happen if python was installed from sources
raise ImportError(
Expand Down Expand Up @@ -652,7 +663,7 @@ def nice(self, value=None):
raise NoSuchProcess(self.pid, self._name)
self._proc.nice_set(value)

if _POSIX:
if POSIX:

def uids(self):
"""Return process UIDs as a (real, effective, saved)
Expand Down Expand Up @@ -755,7 +766,7 @@ def environ(self):
might not reflect changes made after the process started. """
return self._proc.environ()

if _WINDOWS:
if WINDOWS:

def num_handles(self):
"""Return the number of handles opened by this process
Expand Down Expand Up @@ -906,7 +917,7 @@ def cpu_percent(self, interval=None):
"""
blocking = interval is not None and interval > 0.0
num_cpus = cpu_count()
if _POSIX:
if POSIX:
def timer():
return _timer() * num_cpus
else:
Expand Down Expand Up @@ -1074,7 +1085,7 @@ def connections(self, kind='inet'):
"""
return self._proc.connections(kind)

if _POSIX:
if POSIX:
def _send_signal(self, sig):
if self.pid == 0:
# see "man 2 kill"
Expand All @@ -1086,7 +1097,7 @@ def _send_signal(self, sig):
os.kill(self.pid, sig)
except OSError as err:
if err.errno == errno.ESRCH:
if _OPENBSD and pid_exists(self.pid):
if OPENBSD and pid_exists(self.pid):
# We do this because os.kill() lies in case of
# zombie processes.
raise ZombieProcess(self.pid, self._name, self._ppid)
Expand All @@ -1104,7 +1115,7 @@ def send_signal(self, sig):
On Windows only SIGTERM is valid and is treated as an alias
for kill().
"""
if _POSIX:
if POSIX:
self._send_signal(sig)
else:
if sig == signal.SIGTERM:
Expand All @@ -1124,7 +1135,7 @@ def suspend(self):
whether PID has been reused.
On Windows this has the effect ot suspending all process threads.
"""
if _POSIX:
if POSIX:
self._send_signal(signal.SIGSTOP)
else:
self._proc.suspend()
Expand All @@ -1135,7 +1146,7 @@ def resume(self):
whether PID has been reused.
On Windows this has the effect of resuming all process threads.
"""
if _POSIX:
if POSIX:
self._send_signal(signal.SIGCONT)
else:
self._proc.resume()
Expand All @@ -1146,7 +1157,7 @@ def terminate(self):
whether PID has been reused.
On Windows this is an alias for kill().
"""
if _POSIX:
if POSIX:
self._send_signal(signal.SIGTERM)
else:
self._proc.kill()
Expand All @@ -1156,7 +1167,7 @@ def kill(self):
"""Kill the current process with SIGKILL pre-emptively checking
whether PID has been reused.
"""
if _POSIX:
if POSIX:
self._send_signal(signal.SIGKILL)
else:
self._proc.kill()
Expand Down Expand Up @@ -1263,7 +1274,7 @@ def pid_exists(pid):
"""
if pid < 0:
return False
elif pid == 0 and _POSIX:
elif pid == 0 and POSIX:
# On POSIX we use os.kill() to determine PID existence.
# According to "man 2 kill" PID 0 has a special meaning
# though: it refers to <<every process in the process
Expand Down Expand Up @@ -1891,7 +1902,7 @@ def net_if_addrs():
try:
fam = socket.AddressFamily(fam)
except ValueError:
if _WINDOWS and fam == -1:
if WINDOWS and fam == -1:
fam = _psplatform.AF_LINK
elif (hasattr(_psplatform, "AF_LINK") and
_psplatform.AF_LINK == fam):
Expand Down Expand Up @@ -1953,7 +1964,7 @@ def test(): # pragma: no cover
templ = "%-10s %5s %4s %4s %7s %7s %-13s %5s %7s %s"
attrs = ['pid', 'cpu_percent', 'memory_percent', 'name', 'cpu_times',
'create_time', 'memory_info']
if _POSIX:
if POSIX:
attrs.append('uids')
attrs.append('terminal')
print(templ % ("USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY",
Expand All @@ -1978,7 +1989,7 @@ def test(): # pragma: no cover
user = p.username()
except Error:
user = ''
if _WINDOWS and '\\' in user:
if WINDOWS and '\\' in user:
user = user.split('\\')[1]
vms = pinfo['memory_info'] and \
int(pinfo['memory_info'].vms / 1024) or '?'
Expand Down
8 changes: 4 additions & 4 deletions test/_bsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import time

import psutil
from psutil._common import BSD
from psutil._common import FREEBSD
from psutil._common import NETBSD
from psutil._common import OPENBSD
from psutil import BSD
from psutil import FREEBSD
from psutil import NETBSD
from psutil import OPENBSD
from psutil._compat import PY3
from test_psutil import get_test_subprocess
from test_psutil import MEMORY_TOLERANCE
Expand Down
2 changes: 1 addition & 1 deletion test/_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import psutil
import psutil._pslinux
from psutil._common import LINUX
from psutil import LINUX
from psutil._compat import PY3
from psutil._compat import u
from test_psutil import call_until
Expand Down
2 changes: 1 addition & 1 deletion test/_osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import time

import psutil
from psutil._common import OSX
from psutil import OSX
from psutil._compat import PY3
from test_psutil import get_test_subprocess
from test_psutil import MEMORY_TOLERANCE
Expand Down
10 changes: 5 additions & 5 deletions test/_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import time

import psutil
from psutil._common import BSD
from psutil._common import LINUX
from psutil._common import OSX
from psutil._common import POSIX
from psutil._common import SUNOS
from psutil import BSD
from psutil import LINUX
from psutil import OSX
from psutil import POSIX
from psutil import SUNOS
from psutil._compat import callable
from psutil._compat import PY3
from test_psutil import get_kernel_version
Expand Down
2 changes: 1 addition & 1 deletion test/_sunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os

import psutil
from psutil._common import SUNOS
from psutil import SUNOS
from test_psutil import sh
from test_psutil import unittest

Expand Down
2 changes: 1 addition & 1 deletion test/_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
win32api = win32con = None

import psutil
from psutil._common import WINDOWS
from psutil import WINDOWS
from psutil._compat import callable
from psutil._compat import long
from psutil._compat import PY3
Expand Down
18 changes: 9 additions & 9 deletions test/test_psutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@
import mock # requires "pip install mock"

import psutil
from psutil._common import BSD
from psutil._common import FREEBSD
from psutil._common import LINUX
from psutil._common import NETBSD
from psutil._common import OPENBSD
from psutil._common import OSX
from psutil._common import POSIX
from psutil._common import SUNOS
from psutil import BSD
from psutil import FREEBSD
from psutil import LINUX
from psutil import NETBSD
from psutil import OPENBSD
from psutil import OSX
from psutil import POSIX
from psutil import SUNOS
from psutil import WINDOWS
from psutil._common import supports_ipv6
from psutil._common import WINDOWS
from psutil._compat import callable
from psutil._compat import long
from psutil._compat import PY3
Expand Down

0 comments on commit 907bde6

Please sign in to comment.