ostools.py :  » IDE » PIDA » pida-0.6beta3 » pida » utils » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » IDE » PIDA 
PIDA » pida 0.6beta3 » pida » utils » ostools.py
# -*- coding: utf-8 -*-
"""
    :copyright: 2009 by The PIDA Project
    :license: GPL 2 or later (see README/COPYING/LICENSE)

Abstraction to get informations about processes etc...

"""
import os, sys

try:
    import psutil
    NoSuchProcess = psutil.NoSuchProcess
    AccessDenied = psutil.AccessDenied
except ImportError:
    psutil = None
    class NoSuchProcess(Exception):
        """
        No process was found for the given parameters.
        """
        pass
    class AccessDenied(Exception):
        """
        No process was found for the given parameters.
        """
        pass



if sys.platform != 'win32':
    def get_default_system_shell():
        """
        Returns the default shell for the user
        """
        import pwd
        return os.environ.get(
            'SHELL', # try shell from env
            pwd.getpwuid(os.getuid())[-1] # fallback to login shell
        )
    
    PATH_MATCHES = \
             ((r'((\.\.\/|[-\.~a-zA-Z0-9_/\-\\])*(\.[a-zA-Z0-9]+)*(\:[0-9]+)?)',
               r'((\.\.\/|[-\.~a-zA-Z0-9_/\-\\])*(\.[a-zA-Z0-9]+)*(\:[0-9]+)?)'),
             )
# old versions
#            (r'((\.\./|[-\.~a-zA-Z0-9_/\-\\])*\(.[a-zA-Z0-9]+){0,1})',
#              r'((\.\./|[-\.~a-zA-Z0-9_/\-\\])*\.([a-zA-Z0-9]+){0,1})')
#            ((r'((\.\./|[-\.~a-zA-Z0-9_/\-\\])*\.[a-zA-Z0-9]+(\:[0-9]+)?)',
#              r'((\.\./|[-\.~a-zA-Z0-9_/\-\\])*\.[a-zA-Z0-9]+(\:[0-9]+)?)'),

else:
    #FIXME: win32 port
    def get_default_system_shell():
        """
        Returns the default shell for the user
        """
        return ""

    PATH_MATCHES = ()



if psutil and hasattr(psutil.Process, 'getcwd'):
    def get_cwd(pid):
        """
        Returns the working path for a process
    
        @pid: process id
        """
        return psutil.Process(pid).getcwd()

    def get_absolute_path(path, pid):
        """
        Returns the absolut path for a path relative for the process pid
    
        @path: path to add
        @pid: process id
        """
        if os.path.isabs(path):
            return path
        base = psutil.Process(pid).getcwd()
        return os.path.abspath(os.path.join(base, path))

elif sys.platform in ('linux2', 'bsd'):
    # linux fallbacks
    def get_cwd(pid):
        """
        Returns the working path for a process
    
        @pid: process id
        """
        try:
            return os.readlink('/proc/%s/cwd'%pid)
        except OSError:
            raise NoSuchProcess("pid %s does not exist" %pid)

    def get_absolute_path(path, pid):
        """
        Returns the absolut path for a path relative for the process pid
    
        @path: path to add
        @pid: process id
        """
        #XXX: works on bsd and linux only
        #     solaris needs /proc/%s/path/cwd
        if os.path.isabs(path):
            return path
        try:
            base = os.readlink('/proc/%s/cwd'%pid)
            return os.path.abspath(os.path.join(base, path))
        except OSError:
            raise NoSuchProcess("pid %s does not exist" %pid)

else:
    def get_cwd(dummy):
        """
        Returns the working path for a process

        !!! NOOP FALLBACK !!!

        @pid: process id
        """
        return ''

    def get_absolute_path(path, dummy):
        """
        Returns the absolut path for a path relative for the process pid

        !!! NOOP FALLBACK !!!

        @path: path to add
        @pid: process id
        """
        if os.path.isabs(path):
            return path

        return None

if psutil:
    def pid_exist(pid):
        """
        Check whether the given PID exists in the current process list
        """
        return psutil.pid_exists(pid)

elif sys.platform in ('linux2', 'bsd'):
    def pid_exist(pid):
        """
        Check whether the given PID exists in the current process list
        """
        return os.path.exists("/proc/%s" %pid)
else:
    def pid_exist(dummy):
        """
        Check whether the given PID exists in the current process list
        """
        return None


if psutil:
    def kill_pid(pid, sig=None):
        """
        Kill the current process by using signal sig (defaults to SIGKILL).
        """
        psutil.Process(pid).kill(sig)

elif hasattr(os, 'kill'):
    def kill_pid(pid, sig=None):
        """
        Kill the current process by using signal sig (defaults to SIGKILL).
        """
        if sig is not None:
            try:
                return os.kill(pid, sig)
            except OSError, err:
                raise NoSuchProcess(err)
        else:
            try:
                return os.kill(pid, 9)
            except OSError, err:
                raise NoSuchProcess(err)
else:
    def kill_pid(dummy, dummy2=None):
        """
        Kill the current process by using signal sig (defaults to SIGKILL).
        """
        return None

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.