Can Xue avatar Can Xue committed ae6a9b3

signal handlers now in initial.py

Comments (0)

Files changed (4)

kahgean/daemonize.py

         os.unlink(pidfile)
 
 
-def _sys_exit(*args):
-    sys.exit()
-
-
 def daemonize(pidfile):
     """daemonize"""
     if hasattr(os, 'fork'):
         _check_pidfile(pidfile)
         _daemonize()
         _write_pidfile(pidfile)
-        for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGQUIT,
-                    signal.SIGHUP, signal.SIGABRT]:
-            signal.signal(sig, _sys_exit)
     else:
         warnings.warn('Your platform does not support fork() so that '
                       'daemonizing has been skipped, the process will '

kahgean/initial.py

 """
 
 import os
+import sys
 try:
     import resource, grp, pwd
 except ImportError:
     resource = grp = pwd = None
+import signal
 
-__all__ = ['change_group', 'change_user', 'change_rlimit_nofile',
+__all__ = [SCRIPT_NAME, SCRIPT_PATH, INTERPRETER,
+           'change_group', 'change_user', 'change_rlimit_nofile',
            'change_workdir', 'change_umask']
 
 
+SCRIPT_NAME = os.path.basename(sys.argv[0])
+SCRIPT_PATH = os.path.dirname(sys.argv[0]) or os.getcwd()
+INTERPRETER = sys.executable or 'python'
+
+
 def change_group(groupname):
     """change the group"""
     if not grp or not groupname: return
                        help='umask of the %(prog)s process(es)')
 
 
+def _sys_exit(*args):
+    sys.exit()
+
+
 def deal_with_options(options):
     """work with ``append_options()``, deal with the parsing result of
     the ``options`` object"""
     change_user(options.get('user'))
     change_workdir(options.get('workdir'))
     change_umask(options.get('umask'))
+    for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGQUIT,
+                signal.SIGHUP, signal.SIGABRT]:
+        signal.signal(sig, _sys_exit)
 
 from distutils.core import setup
 
-__version__ = "0.1.2"
+__version__ = "0.1.3"
 
 with open('README.rst', 'r') as f:
     long_description = f.read()

test/simple_daemon.py

 
 from SimpleXMLRPCServer import SimpleXMLRPCServer
 from kahgean.options import Options
+from kahgean import initial
 from kahgean import logkit
 from kahgean import daemonize
 
 if __name__ == '__main__':
     options = Options()
     options.add_option('--port', type=int, default=38080)
+    initial.append_options(options)
     logkit.append_options(options)
     daemonize.append_options(options)
     options.parse_options()
+    initial.deal_with_options(options)
     logkit.deal_with_options(options)
     daemonize.deal_with_options(options)
     server = SimpleXMLRPCServer(("localhost", options.get('port')))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.