Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Prototype for Cygwin support module (take 2) #1483

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ syntax: glob
*.so
*.swp
.failed-tests.txt
*.dll
.cache/
.idea/
.tox/
Expand Down
33 changes: 29 additions & 4 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- NetBSD
- Sun Solaris
- AIX
- Cygwin (experimental)

Works with Python versions from 2.6 to 3.4+.
"""
Expand Down Expand Up @@ -77,6 +78,7 @@

from ._common import AIX
from ._common import BSD
from ._common import CYGWIN
from ._common import FREEBSD # NOQA
from ._common import LINUX
from ._common import MACOS
Expand Down Expand Up @@ -173,6 +175,10 @@
# via sys.modules.
PROCFS_PATH = "/proc"

elif CYGWIN:
PROCFS_PATH = "/proc"
from . import _pscygwin as _psplatform

else: # pragma: no cover
raise NotImplementedError('platform %s is not supported' % sys.platform)

Expand Down Expand Up @@ -201,7 +207,7 @@
"POWER_TIME_UNKNOWN", "POWER_TIME_UNLIMITED",

"BSD", "FREEBSD", "LINUX", "NETBSD", "OPENBSD", "MACOS", "OSX", "POSIX",
"SUNOS", "WINDOWS", "AIX",
"SUNOS", "WINDOWS", "AIX", "CYGWIN",

# classes
"Process", "Popen",
Expand Down Expand Up @@ -357,6 +363,12 @@ def __init__(self, seconds, pid=None, name=None):
if POSIX:
from . import _psposix
_psposix.TimeoutExpired = TimeoutExpired
if CYGWIN:
from . import _pslinux
_pslinux.NoSuchProcess = NoSuchProcess
_pslinux.ZombieProcess = ZombieProcess
_pslinux.AccessDenied = AccessDenied
_pslinux.TimeoutExpired = TimeoutExpired
Copy link
Contributor Author

@embray embray Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. Didn't there used to be a psutil._exceptions module? Why did that go away? Having something like that would seem a bit less crazy than stuff like this...



# =====================================================================
Expand Down Expand Up @@ -657,11 +669,16 @@ def parent(self):
if self.pid == lowest_pid:
return None
ppid = self.ppid()
if CYGWIN:
# See note for Process.create_time in _pscygwin.py
rounding_error = 1
else:
rounding_error = 0
if ppid is not None:
ctime = self.create_time()
try:
parent = Process(ppid)
if parent.create_time() <= ctime:
if parent.create_time() <= (ctime + rounding_error):
return parent
# ...else ppid has been reused by another process
except NoSuchProcess:
Expand Down Expand Up @@ -1490,7 +1507,15 @@ def pids():
"""Return a list of current running PIDs."""
global _LOWEST_PID
ret = sorted(_psplatform.pids())
_LOWEST_PID = ret[0]

if CYGWIN:
# Note: The _LOWEST_PID concept is not very meaningful since every
# "top-level" process has a non-1 PID; they do have PPID of 1 but that
# does not correspend to an actual process that shows up in the pids()
# list
_LOWEST_PID = 1
else:
_LOWEST_PID = ret[0]
return ret


Expand Down Expand Up @@ -2462,7 +2487,7 @@ def test(): # pragma: no cover
else:
cputime = ''

user = p.info['username']
user = p.info['username'] or ''
if not user and POSIX:
try:
user = p.uids()[0]
Expand Down
72 changes: 69 additions & 3 deletions psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
enum = None

# can't take it from _common.py as this script is imported by setup.py
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3

__all__ = [
# constants
'FREEBSD', 'BSD', 'LINUX', 'NETBSD', 'OPENBSD', 'MACOS', 'OSX', 'POSIX',
'SUNOS', 'WINDOWS',
'SUNOS', 'WINDOWS', 'CYGWIN',
'ENCODING', 'ENCODING_ERRS', 'AF_INET6',
# connection constants
'CONN_CLOSE', 'CONN_CLOSE_WAIT', 'CONN_CLOSING', 'CONN_ESTABLISHED',
Expand All @@ -61,8 +62,9 @@
'pthread', 'puids', 'sconn', 'scpustats', 'sdiskio', 'sdiskpart',
'sdiskusage', 'snetio', 'snicaddr', 'snicstats', 'sswap', 'suser',
# utility functions
'conn_tmap', 'deprecated_method', 'isfile_strict', 'memoize',
'parse_environ_block', 'path_exists_strict', 'usage_percent',
'str2bytes', 'bytes2str', 'unicode2str',
'conn_tmap', 'deprecated_method', 'get_procfs_path', 'isfile_strict',
'memoize', 'parse_environ_block', 'path_exists_strict', 'usage_percent',
'supports_ipv6', 'sockfam_to_enum', 'socktype_to_enum', "wrap_numbers",
'bytes2human',
]
Expand All @@ -84,6 +86,7 @@
BSD = FREEBSD or OPENBSD or NETBSD
SUNOS = sys.platform.startswith(("sunos", "solaris"))
AIX = sys.platform.startswith("aix")
CYGWIN = sys.platform.startswith("cygwin")


# ===================================================================
Expand Down Expand Up @@ -264,6 +267,61 @@ class BatteryTime(enum.IntEnum):
# --- utils
# ===================================================================

if PY2:
def bytes2str(s, encoding=ENCODING, errors=ENCODING_ERRS):
"""Given bytes, return a str.

On Python 2 this is a no-op since bytes is str; on Python 3 the bytes
are decoded using the optional encoding/errors arguments, or the
constants ENCODING/ENCODING_ERRS by default.
"""
return s

def str2bytes(s, encoding=ENCODING, errors=ENCODING_ERRS):
"""Given a str, return bytes.

On Python 2 this is a no-op since str is bytes; on Python 3 the
bytes are encoding using the optional encoding/errors arguments, or
the constants ENCODING/ENCODING_ERRS by default.
"""
return s

def unicode2str(s, encoding=ENCODING, errors=ENCODING_ERRS):
"""Given a Python 3 str or Python 2 unicode, return a str.

On Python 3 this is a no-op since str is unicode, but on Python 2
the unicode is encoded using the optional encoding/errors arguments,
or the constants ENCODING/ENCODING_ERRS by default.
"""
return s.encode(encoding, errors)
else:
def bytes2str(s, encoding=ENCODING, errors=ENCODING_ERRS):
"""Given bytes, return a str.

On Python 2 this is a no-op since bytes is str; on Python 3 the bytes
are decoded using the optional encoding/errors arguments, or the
constants ENCODING/ENCODING_ERRS by default.
"""
return s.decode(encoding, errors)

def str2bytes(s, encoding=ENCODING, errors=ENCODING_ERRS):
"""Given a str, return bytes.

On Python 2 this is a no-op since str is bytes; on Python 3 the
bytes are encoding using the optional encoding/errors arguments, or
the constants ENCODING/ENCODING_ERRS by default.
"""
return s.encode(encoding, errors)

def unicode2str(s, encoding=ENCODING, errors=ENCODING_ERRS):
"""Given a Python 3 str or Python 2 unicode, return a str.

On Python 3 this is a no-op since str is unicode, but on Python 2
the unicode is encoded using the optional encoding/errors arguments,
or the constants ENCODING/ENCODING_ERRS by default.
"""
return s


def usage_percent(used, total, round_=None):
"""Calculate percentage usage of 'used' against 'total'."""
Expand Down Expand Up @@ -366,6 +424,14 @@ def cache_deactivate(proc):
return wrapper


def get_procfs_path():
"""Return updted psutil.PROCFS_PATH constant."""
try:
return sys.modules['psutil'].PROCFS_PATH
except AttributeError:
raise RuntimeError("This platform does not have a proc filesystem.")


def isfile_strict(path):
"""Same as os.path.isfile() but does not swallow EACCES / EPERM
exceptions, see:
Expand Down
11 changes: 1 addition & 10 deletions psutil/_psaix.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from . import _psutil_aix as cext
from . import _psutil_posix as cext_posix
from ._common import AF_INET6
from ._common import get_procfs_path
from ._common import memoize_when_activated
from ._common import NIC_DUPLEX_FULL
from ._common import NIC_DUPLEX_HALF
Expand Down Expand Up @@ -101,16 +102,6 @@
svmem = namedtuple('svmem', ['total', 'available', 'percent', 'used', 'free'])


# =====================================================================
# --- utils
# =====================================================================


def get_procfs_path():
"""Return updated psutil.PROCFS_PATH constant."""
return sys.modules['psutil'].PROCFS_PATH


# =====================================================================
# --- memory
# =====================================================================
Expand Down
Loading