diff options
| author | Jay Loden <jloden@gmail.com> | 2009-03-13 03:22:55 +0000 |
|---|---|---|
| committer | Jay Loden <jloden@gmail.com> | 2009-03-13 03:22:55 +0000 |
| commit | bdfd71e2fdd4c52be99835909755cc4c8f2bfcd5 (patch) | |
| tree | 1b369721dc1f5a18acf86a0bcdc701cfb137576a | |
| parent | 956b0afbecfccad2fc1152ea095c8a74934e4d0d (diff) | |
| download | psutil-bdfd71e2fdd4c52be99835909755cc4c8f2bfcd5.tar.gz | |
Removed need for import of psutil module in platform specific modules by:
1) split exceptions out into error.py so they can be imported by multiple modules
within the package and overriden in the global psutil namespace by
platform-specific C module versions as needed.
2) Impl.get_process_info() now returns just the tuple, and deproxy() handles
passing it to the constructor of a ProcessInfo() object and creating the
instance for Process._procinfo
I've updated all the files for all platforms but only tested on OS X, will
verify nothing broke on Linux, BSD, and Windows and commit any necessary
changes in the next revision(s).
| -rw-r--r-- | psutil/__init__.py | 1 | ||||
| -rw-r--r-- | psutil/_psbsd.py | 18 | ||||
| -rwxr-xr-x | psutil/_pslinux.py | 14 | ||||
| -rw-r--r-- | psutil/_psmswindows.py | 17 | ||||
| -rw-r--r-- | psutil/_psosx.py | 27 | ||||
| -rw-r--r-- | psutil/_psutil.py | 22 | ||||
| -rw-r--r-- | psutil/error.py | 9 |
7 files changed, 54 insertions, 54 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py index 514b6dd2..9dbc9d3e 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -1 +1,2 @@ from _psutil import * + diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 69005ee7..aa5f8fff 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -11,24 +11,24 @@ import grp import _psutil_bsd -NoSuchProcess = _psutil_bsd.NoSuchProcess +# import psutil exceptions we can override with our own +from error import * +# module level constants (gets pushed up to psutil module) +NoSuchProcess = _psutil_bsd.NoSuchProcess NUM_CPUS = _psutil_osx.get_num_cpus() - def wrap_privileges(callable): """Call callable into a try/except clause so that if an OSError EPERM exception is raised we translate it into psutil.AccessDenied. """ def wrapper(*args, **kwargs): - # XXX - figure out why it can't be imported globally - import psutil try: return callable(*args, **kwargs) except OSError, err: if err.errno == errno.EPERM: - raise psutil.AccessDenied + raise AccessDenied raise return wrapper @@ -40,23 +40,19 @@ class Impl(object): """Returns a tuple that can be passed to the psutil.ProcessInfo class constructor. """ - # XXX - figure out why it can't be imported globally (see r54) - import psutil infoTuple = _psutil_bsd.get_process_info(pid) - return psutil.ProcessInfo(*infoTuple) + return infoTuple @wrap_privileges def kill_process(self, pid, sig=signal.SIGKILL): """Terminates the process with the given PID.""" - # XXX - figure out why it can't be imported globally (see r54) - import psutil if sig is None: sig = signal.SIGKILL try: os.kill(pid, sig) except OSError, err: if err.errno == errno.ESRCH: - raise psutil.NoSuchProcess(pid) + raise NoSuchProcess(pid) raise @wrap_privileges diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index eb8936a1..61f939ae 100755 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -9,8 +9,8 @@ import errno import pwd import grp -import psutil - +# import psutil exceptions we can override with our own +from error import * def _get_uptime(): """Return system boot time (epoch in seconds)""" @@ -48,7 +48,7 @@ def prevent_zombie(method): except IOError, err: if err.errno == errno.ENOENT: # no such file or directory if not self.pid_exists(pid): - raise psutil.NoSuchProcess(pid) + raise NoSuchProcess(pid) raise return wrapper @@ -63,7 +63,7 @@ def wrap_privileges(callable): return callable(*args, **kwargs) except OSError, err: if err.errno == errno.EPERM: - raise psutil.AccessDenied + raise AccessDenied raise return wrapper @@ -76,7 +76,7 @@ class Impl(object): """Returns a process info class.""" if pid == 0: # special case for 0 (kernel process) PID - return psutil.ProcessInfo(pid, 0, 'sched', '', [], 0, 0) + return (pid, 0, 'sched', '', [], 0, 0) # determine executable try: @@ -103,7 +103,7 @@ class Impl(object): finally: f.close() - return psutil.ProcessInfo(pid, self._get_ppid(pid), name, path, cmdline, + return (pid, self._get_ppid(pid), name, path, cmdline, self._get_process_uid(pid), self._get_process_gid(pid)) @@ -116,7 +116,7 @@ class Impl(object): os.kill(pid, sig) except OSError, err: if err.errno == errno.ESRCH: - raise psutil.NoSuchProcess(pid) + raise NoSuchProcess(pid) raise def get_pid_list(self): diff --git a/psutil/_psmswindows.py b/psutil/_psmswindows.py index fbdcc3b3..5431f606 100644 --- a/psutil/_psmswindows.py +++ b/psutil/_psmswindows.py @@ -7,25 +7,24 @@ import errno import _psutil_mswindows +# import psutil exceptions we can override with our own +from error import * +# module level constants (gets pushed up to psutil module) NoSuchProcess = _psutil_mswindows.NoSuchProcess - NUM_CPUS = _psutil_mswindows.get_num_cpus() - def wrap_privileges(callable): """Call callable into a try/except clause so that if a WindowsError 5 AccessDenied exception is raised we translate it into psutil.AccessDenied """ def wrapper(*args, **kwargs): - # XXX - figure out why it can't be imported globally - import psutil try: return callable(*args, **kwargs) except OSError, err: if err.errno == errno.EACCES: - raise psutil.AccessDenied + raise AccessDenied raise return wrapper @@ -37,22 +36,18 @@ class Impl(object): """Returns a tuple that can be passed to the psutil.ProcessInfo class constructor. """ - # XXX - figure out why it can't be imported globally - import psutil infoTuple = _psutil_mswindows.get_process_info(pid) - return psutil.ProcessInfo(*infoTuple) + return infoTuple @wrap_privileges def kill_process(self, pid, sig=None): """Terminates the process with the given PID.""" - # XXX - figure out why it can't be imported globally - import psutil try: return _psutil_mswindows.kill_process(pid) except OSError, err: # work around issue #24 if (pid == 0) and (err.errno == errno.EINVAL): - raise psutil.AccessDenied + raise AccessDenied raise def get_pid_list(self): diff --git a/psutil/_psosx.py b/psutil/_psosx.py index fc4fc5cc..4b00f6b9 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -9,24 +9,24 @@ import errno import _psutil_osx -NoSuchProcess = _psutil_osx.NoSuchProcess +# import psutil exceptions we can override with our own +from error import * +# module level constants (gets pushed up to psutil module) +NoSuchProcess = _psutil_osx.NoSuchProcess NUM_CPUS = _psutil_osx.get_num_cpus() - def wrap_privileges(callable): """Call callable into a try/except clause so that if an OSError EPERM exception is raised we translate it into psutil.AccessDenied. """ def wrapper(*args, **kwargs): - # XXX - figure out why it can't be imported globally - import psutil try: return callable(*args, **kwargs) except OSError, err: if err.errno == errno.EPERM: - raise psutil.AccessDenied + raise AccessDenied raise return wrapper @@ -38,25 +38,30 @@ class Impl(object): """Returns a tuple that can be passed to the psutil.ProcessInfo class constructor. """ - # XXX - figure out why it can't be imported globally (see r54) - import psutil infoTuple = _psutil_osx.get_process_info(pid) - return psutil.ProcessInfo(*infoTuple) + return infoTuple @wrap_privileges def kill_process(self, pid, sig=signal.SIGKILL): """Terminates the process with the given PID.""" - # XXX - figure out why it can't be imported globally (see r54) - import psutil if sig is None: sig = signal.SIGKILL try: os.kill(pid, sig) except OSError, err: if err.errno == errno.ESRCH: - raise psutil.NoSuchProcess(pid) + raise NoSuchProcess(pid) raise + def get_cpu_times(self, pid): + #FIXME: write a real function + return (0.0, 0.0) + + def get_process_create_time(self, pid): + #FIXME: write a real function + return 0.0 + + def get_pid_list(self): """Returns a list of PIDs currently running on the system.""" return _psutil_osx.get_pid_list() diff --git a/psutil/_psutil.py b/psutil/_psutil.py index 6149f094..f7652a5d 100644 --- a/psutil/_psutil.py +++ b/psutil/_psutil.py @@ -25,34 +25,26 @@ try: except ImportError: pwd = grp = None +# exceptions are imported here, but may be overriden by platform +# module implementation later +from error import * -# this exception get overriden by the platform specific modules if necessary -class NoSuchProcess(Exception): - """No process was found for the given parameters.""" - -class AccessDenied(Exception): - """Exception raised when permission to perform an action is denied.""" - - -# the linux implementation has the majority of it's functionality -# implemented in python via /proc +# import the appropriate module for our platform only if sys.platform.lower().startswith("linux"): from _pslinux import * -# the windows implementation requires the _psutil_mswindows C module elif sys.platform.lower().startswith("win32"): from _psmswindows import * -# OS X implementation requires _psutil_osx C module elif sys.platform.lower().startswith("darwin"): from _psosx import * -# BSD implementation requires _psutil_bsd C module elif sys.platform.lower().startswith("freebsd"): from _psbsd import * else: raise ImportError('no os specific module found') +# platform-specific modules define an Impl implementation class _platform_impl = Impl() @@ -138,7 +130,9 @@ class Process(object): get_process_info(). """ if self.is_proxy: - self._procinfo = _platform_impl.get_process_info(self._procinfo.pid) + # get_process_info returns a tuple we use as the arguments + # to the ProcessInfo constructor + self._procinfo = ProcessInfo(*_platform_impl.get_process_info(self._procinfo.pid)) self.is_proxy = False @property diff --git a/psutil/error.py b/psutil/error.py new file mode 100644 index 00000000..d52362e1 --- /dev/null +++ b/psutil/error.py @@ -0,0 +1,9 @@ +import sys + +# this exception get overriden by the platform specific modules if necessary +class NoSuchProcess(Exception): + """No process was found for the given parameters.""" + +class AccessDenied(Exception): + """Exception raised when permission to perform an action is denied.""" + |
