Author: remi
Date: 2008-12-13 17:12:14 +0100 (Sat, 13 Dec 2008)
New Revision: 3085

Modified:
   
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/Daemonizer.py
   
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/__init__.py
   
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/version.py
Log:
* fixed encoding
* updated version
* updated comments

Modified: 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/Daemonizer.py
===================================================================
--- 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/Daemonizer.py
    2008-12-13 15:42:10 UTC (rev 3084)
+++ 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/Daemonizer.py
    2008-12-13 16:12:14 UTC (rev 3085)
@@ -1,179 +1,217 @@
-# -*- coding: latin1 -*-
-
-import version
-__date__ = version.date
-__version__ = version.version
-__licence__ = version.licence
-del version
-
-#    Distributed under the terms of the GNU General Public License
-#    http://www.gnu.org/copyleft/gpl.html
-
-import os
-import sys
-import errno
-
-class PIDFile(object):
-    """Class to deal with the pidfile. The original version come from the 
sysklogd package,
-    under GPL Copyright (c) 1995  Martin Schulze <[email protected]>
-    """
-    def __init__(self, name, path = "/var/run"):
-        """Constructor of the class.
-
-        @param name: name of the daemon.
-        @param path: path of the pid file.
-        """
-        self.__PIDFILE = "%s/%s.pid" % (path, name)
-
-    def read(self):
-        """Read the pid value from the pid file.
-
-        @return: pid if exists or 0.
-        """
-        pid = 0
-        try:
-            f = open(self.__PIDFILE, "r")
-            try:
-                pid = int(f.readline())
-            finally:
-                f.close()
-        except:
-            pass
-        return pid
-
-    def write(self):
-        """Write the pid value in the pid file.
-
-        @return: the success of the operation.
-        """
-        pid = os.getpid()
-        result = False
-        try:
-            f = open(self.__PIDFILE, "w")
-            try:
-                f.write("%d\n" % pid)
-            finally:
-                f.close()
-                result = True
-        except:
-            print 'can not write file'
-        return result
-
-    def check(self):
-        """Check that an instance of the same program is started.
-        If the program is already started, the function kill it.
-        @return: False in all cases except for that a program is already 
started.
-        """
-        pid = os.getpid()
-        oldPid = self.read()
-        # Program not runned or its pid is the same
-        if (oldPid == pid) or (oldPid == 0):
-            # RETURN only one instance.
-            return False
-        # Program is already started
-        # Try to kill it.
-        try:
-            os.kill(oldPid, 0)
-        # Can't kill it.
-        except OSError, why:
-            if why[0] == errno.ESRCH:
-                # RETURN only one instance (old pid reference is broken)
-                return False
-            else:
-                # RETURN not the only one instance (Can't kill the program)
-                return True
-        # RETURN only one instance (old pid instance has been killed)
-        return False
-
-    def remove(self):
-        """Remove the pid file.
-        """
-        try:
-            os.remove(self.__PIDFILE)
-        except:
-            pass
-
-class Daemonizer(object):
-    """Class to daemonizing a program. (Unix)
-    """
-
-    def __init__(self, name, logPath, serviceFunct, redirectIO = True):
-        """Constructor of the class.
-
-        @param name: name of the program.
-        @param logPath: path of the log file.
-        @param serviceFunct: pointer to the service to run.
-        @param redirectIO: indicate if the stdout and stdin must be redirected
-        in the log file.
-        """
-        logFilename = '%s/%s.log' % (logPath, name)
-        self.__daemonize(redirectIO, '/dev/null', logFilename, logFilename)
-        self.__pidfile = PIDFile(name)
-        self.__serviceFunct = serviceFunct
-
-    def start(self):
-        """Start the service as daemon.
-
-        @return: the success of the operation.
-        """
-        result = True
-        if not self.__pidfile.check():
-            if not self.__pidfile.write():
-                result = False
-        else:
-            result = False
-        if result:
-            try:
-                self.__serviceFunct()
-            except:
-                result = False
-        return result
-
-    def destroy(self):
-        """Destructor of the class.
-        """
-        pass
-        #self.__pidfile.remove()
-
-    #References: UNIX Programming FAQ
-    #1.7 How do I get my program to act like a daemon?
-    #http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
-    #
-    #Advanced Programming in the Unix Environment
-    #W. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7.
-    def __daemonize(self, redirectIO, stdin, stdout, stderr):
-        """
-        """
-        # Do first fork.
-        try:
-            pid = os.fork()
-            if pid > 0:
-                sys.exit(0) # Exit first parent.
-        except OSError, e:
-            sys.stderr.write ("fork #1 failed: (%d) %s\n" % (e.errno, 
e.strerror))
-            sys.exit(1)
-
-        # Decouple from parent environment.
-        os.chdir("/")
-        os.umask(0)
-        os.setsid()
-
-        # Do second fork.
-        try:
-            pid = os.fork()
-            if pid > 0:
-                sys.exit(0) # Exit second parent.
-        except OSError, e:
-            sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, 
e.strerror))
-            sys.exit(1)
-
-        # Now I am a daemon!
-
-        # Redirect standard file descriptors.
-        if redirectIO:
-            si = file(stdin, 'r')
-            so = file(stdout, 'w')
-            se = file(stderr, 'w', 0)
-            os.dup2(si.fileno(), sys.stdin.fileno())
-            os.dup2(so.fileno(), sys.stdout.fileno())
-            os.dup2(se.fileno(), sys.stderr.fileno())
+# -*- coding: utf-8 -*-
+
+import version
+__date__ = version.date
+__version__ = version.version
+__licence__ = version.licence
+del version
+
+#    Copyleft (C) 2008 C2ME Sa
+#    Remi Jocaille <[email protected]>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+import os
+import sys
+import errno
+
+# 
==============================================================================
+# Public class
+# 
==============================================================================
+
+# 
------------------------------------------------------------------------------
+# Class to deal with the pidfile.
+# 
------------------------------------------------------------------------------
+class PIDFile(object):
+    """Class to deal with the pidfile.
+    The original version come from the sysklogd package,
+    under GPL Copyright (c) 1995  Martin Schulze <[email protected]>
+    """
+
+    # 
--------------------------------------------------------------------------
+    # Constructor.
+    # 
--------------------------------------------------------------------------
+    def __init__(self, name, path = "/var/run"):
+        """Constructor of the class.
+
+        @param name: name of the daemon.
+        @param path: path of the pid file.
+        """
+        self.__PIDFILE = "%s/%s.pid" % (path, name)
+
+    # 
--------------------------------------------------------------------------
+    # Read the pid value from the pid file.
+    # 
--------------------------------------------------------------------------
+    def read(self):
+        """Read the pid value from the pid file.
+        @return: pid if exists or 0.
+        """
+        pid = 0
+        try:
+            f = open(self.__PIDFILE, "r")
+            try:
+                pid = int(f.readline())
+            finally:
+                f.close()
+        except:
+            pass
+        return pid
+
+    # 
--------------------------------------------------------------------------
+    # Write the pid value in the pid file.
+    # 
--------------------------------------------------------------------------
+    def write(self):
+        """Write the pid value in the pid file.
+        @return: the success of the operation.
+        """
+        pid = os.getpid()
+        result = False
+        try:
+            f = open(self.__PIDFILE, "w")
+            try:
+                f.write("%d\n" % pid)
+            finally:
+                f.close()
+                result = True
+        except:
+            print 'can not write file'
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Check that an instance of the same program is started.
+    # 
--------------------------------------------------------------------------
+    def check(self):
+        """Check that an instance of the same program is started.
+        If the program is already started, the function kill it.
+        @return: False in all cases except for that a program is already 
started.
+        """
+        pid = os.getpid()
+        oldPid = self.read()
+        # Program not runned or its pid is the same
+        if (oldPid == pid) or (oldPid == 0):
+            # RETURN only one instance.
+            return False
+        # Program is already started
+        # Try to kill it.
+        try:
+            os.kill(oldPid, 0)
+        # Can't kill it.
+        except OSError, why:
+            if why[0] == errno.ESRCH:
+                # RETURN only one instance (old pid reference is broken)
+                return False
+            else:
+                # RETURN not the only one instance (Can't kill the program)
+                return True
+        # RETURN only one instance (old pid instance has been killed)
+        return False
+
+    # 
--------------------------------------------------------------------------
+    # Remove the pid file.
+    # 
--------------------------------------------------------------------------
+    def remove(self):
+        """Remove the pid file.
+        """
+        try:
+            os.remove(self.__PIDFILE)
+        except:
+            pass
+
+# 
------------------------------------------------------------------------------
+# Class to daemonizing a program. (Unix)
+# 
------------------------------------------------------------------------------
+class Daemonizer(object):
+    """Class to daemonizing a program. (Unix)
+    """
+
+    # 
--------------------------------------------------------------------------
+    # Constructor.
+    # 
--------------------------------------------------------------------------
+    def __init__(self, name, logPath, serviceFunct, redirectIO = True):
+        """Constructor of the class.
+
+        @param name: name of the program.
+        @param logPath: path of the log file.
+        @param serviceFunct: pointer to the service to run.
+        @param redirectIO: indicate if the stdout and stdin must be redirected
+        in the log file.
+        """
+        logFilename = '%s/%s.log' % (logPath, name)
+        self.__daemonize(redirectIO, '/dev/null', logFilename, logFilename)
+        self.__pidfile = PIDFile(name)
+        self.__serviceFunct = serviceFunct
+
+    # 
--------------------------------------------------------------------------
+    # Start the service as daemon.
+    # 
--------------------------------------------------------------------------
+    def start(self):
+        """Start the service as daemon.
+        @return: the success of the operation.
+        """
+        result = True
+        if not self.__pidfile.check():
+            if not self.__pidfile.write():
+                result = False
+        else:
+            result = False
+        if result:
+            try:
+                self.__serviceFunct()
+            except:
+                result = False
+        return result
+
+    # 
--------------------------------------------------------------------------
+    # Destructor of the class.
+    # 
--------------------------------------------------------------------------
+    def destroy(self):
+        """Destructor of the class.
+        """
+        pass
+        #self.__pidfile.remove()
+
+    # 
--------------------------------------------------------------------------
+    # Daemonize the application.
+    # 
--------------------------------------------------------------------------
+    #References: UNIX Programming FAQ
+    #1.7 How do I get my program to act like a daemon?
+    #http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
+    #
+    #Advanced Programming in the Unix Environment
+    #W. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7.
+    def __daemonize(self, redirectIO, stdin, stdout, stderr):
+        """Daemonize the application.
+        """
+        # Do first fork.
+        try:
+            pid = os.fork()
+            if pid > 0:
+                sys.exit(0) # Exit first parent.
+        except OSError, e:
+            sys.stderr.write ("fork #1 failed: (%d) %s\n" % (e.errno, 
e.strerror))
+            sys.exit(1)
+
+        # Decouple from parent environment.
+        os.chdir("/")
+        os.umask(0)
+        os.setsid()
+
+        # Do second fork.
+        try:
+            pid = os.fork()
+            if pid > 0:
+                sys.exit(0) # Exit second parent.
+        except OSError, e:
+            sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, 
e.strerror))
+            sys.exit(1)
+
+        # Now I am a daemon!
+
+        # Redirect standard file descriptors.
+        if redirectIO:
+            si = file(stdin, 'r')
+            so = file(stdout, 'w')
+            se = file(stderr, 'w', 0)
+            os.dup2(si.fileno(), sys.stdin.fileno())
+            os.dup2(so.fileno(), sys.stdout.fileno())
+            os.dup2(se.fileno(), sys.stderr.fileno())

Modified: 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/__init__.py
===================================================================
--- 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/__init__.py
      2008-12-13 15:42:10 UTC (rev 3084)
+++ 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/__init__.py
      2008-12-13 16:12:14 UTC (rev 3085)
@@ -1,28 +1,28 @@
-# -*- coding: latin1 -*-
-
-"""
-daemonizer
-==========
-
-    Simple daemonizer for python.
-
-    http://www.tuxisalive.com
-"""
-
-import version
-__name__ = version.name
-__author__ = version.author
-__date__ = version.date
-__version__ = version.version
-__licence__ = version.licence
-del version
-
-#    Copyright (C) 2008 C2ME Sa
-#    R�mi Jocaille <[email protected]>
-#    Distributed under the terms of the GNU General Public License
-#    http://www.gnu.org/copyleft/gpl.html
-
-#
-# logger package modules
-#
+# -*- coding: utf-8 -*-
+
+"""
+daemonizer
+==========
+
+    Simple daemonizer for python.
+
+    http://www.tuxisalive.com
+"""
+
+import version
+__name__ = version.name
+__author__ = version.author
+__date__ = version.date
+__version__ = version.version
+__licence__ = version.licence
+del version
+
+#    Copyleft (C) 2008 C2ME Sa
+#    Remi Jocaille <[email protected]>
+#    Distributed under the terms of the GNU General Public License
+#    http://www.gnu.org/copyleft/gpl.html
+
+#
+# daemonizer package modules
+#
 from tuxisalive.lib.daemonizer.Daemonizer import *
\ No newline at end of file

Modified: 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/version.py
===================================================================
--- 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/version.py
       2008-12-13 15:42:10 UTC (rev 3084)
+++ 
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/daemonizer/version.py
       2008-12-13 16:12:14 UTC (rev 3085)
@@ -1,17 +1,17 @@
-# -*- coding: latin1 -*-
+# -*- coding: utf-8 -*-
 
 """Version data for daemonizer"""
 
-#    Copyright (C) 2008 C2ME Sa
-#    R�mi Jocaille <[email protected]>
+#    Copyleft (C) 2008 C2ME Sa
+#    Remi Jocaille <[email protected]>
 #    Distributed under the terms of the GNU General Public License
 #    http://www.gnu.org/copyleft/gpl.html
 
 name = 'tuxisalive.lib.daemonizer'
-version = '0.0.1'
+version = '0.0.2'
 author = "Remi Jocaille ([email protected])"
 
 description = "Python module to daemonizing a program."
 
 licence = "GPL"
-date = "June 2008"
\ No newline at end of file
+date = "December 2008"
\ No newline at end of file


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to