Commits

Joseph Tate  committed ecdc0f6

First cut of options parser for daemon library

  • Participants

Comments (0)

Files changed (6)

+.*\.swp$
+.*\.py(c|o)$
+.*\.so
+.*\.o
+.*~
+tags$
+build/.*
+dist/.*
+.*\.egg-info/.*
+.coverage
+.noseids

File daemon/__init__.py

+__import__('pkg_resources').declare_namespace(__name__)

File daemon/parse/__init__.py

Empty file added.

File daemon/parse/cli.py

+import daemon
+import daemon.pidlockfile
+import optparse
+import os
+import pwd, grp
+
+def lookupuid(option, opt, value, parser):
+    try:
+        uid = int(value)
+    except ValueError:
+        uid = pwd.getpwnam(value).pw_uid
+    parser.values.uid = uid
+
+def lookupgid(option, opt, value, parser):
+    try:
+        gid = int(value)
+    except ValueError:
+        gid = grp.getgrnam(value).gr_gid
+    parser.values.gid = gid
+
+def createpidlockfile(option, opt, value, parser):
+    if not value.startswith('/'):
+        raise optparse.OptionValueError('pidfile must be a full path, not %s' % value)
+    if not os.path.isdir(os.path.dirname(value)):
+        raise optparse.OptionValueError('pidfile directory (%s) does not exist' % os.path.dirname(value))
+    if os.path.exists(value):
+        raise optparse.OptionValueError('pidfile exists: %s'% value)
+    parser.values.pidfile = daemon.pidlockfile.PIDLockFile(value)
+
+def opts_to_kw(opts):
+    return opts.__dict__
+
+def options(context=daemon.DaemonContext, **kw):
+    '''
+    See C{optparse.OptionParser.__init__()} for possible parameters.
+    '''
+    parser = optparse.OptionParser(**kw)
+    parser.add_option('--chroot-directory', dest='chroot_directory',
+            metavar='DIR', help='Full path to a directory to set as the effective root directory of the process.',
+            default=None)
+    parser.add_option('--working-directory', dest='working_directory',
+            metavar='DIR', help='Full path to a directory which the process will change to on daemon start.',
+            default=None)
+    parser.add_option('--umask', dest='umask', default=0, type="int", metavar='MASK', help="Set the umask to MASK on daemon start.")
+    parser.add_option('--pidfile', dest='pidfile', action='callback', callback=createpidlockfile, type='string', default=None, metavar='FILE', help="Create a PIDLockFile context manager for the specified file.")
+    parser.add_option('-d', '--detach', '--detach-process', dest='detach_process', default=True, help="Detach the process context. [default]")
+    parser.add_option('-n', '--no-detach', '--no-detach-process', dest='detach_process', action='store_false', help="Detach the process context.")
+    parser.add_option('--uid', '--user', '-u', dest='uid', type='string', metavar='UID', action='callback', default=os.getuid(), callback=lookupuid, help='The real user to run as.')
+    parser.add_option('--gid', '--group', '-g', dest='gid', type='string', metavar='GID', action='callback', default=os.getgid(), callback=lookupgid, help='The real group to run as.')
+    parser.add_option('--prevent-core', default=True, action='store_true', help='Prevent the generation of core files.')
+    parser.add_option('--drop-core', action='store_false', help='Allow the generation of core files.')
+    # TODO: stderr, stdin, stdout and signal handling
+    opts, args = parser.parse_args()
+    dcontext = context(**opts_to_kw(opts))
+    parser.destroy()
+    return dcontext
+
+if __name__=='__main__':
+    import pprint
+    class Context(object):
+        def __init__(self, **kw):
+            self.values = {}
+            for k, v in kw.iteritems():
+                self.values[k] = v
+
+    foo = options(Context, usage='%prog [options]', version='%prog 1.0', prog='test-options')
+    pprint.pprint(foo.values)
+[egg_info]
+
+[nosetests]
+where = tests
+from setuptools import setup, find_packages
+
+setup(
+    name='daemon.parse',
+    version='0.1',
+    description='An add-on to python-daemon (http://pypi.python.org/pypi/python-daemon/) that adds command line and config file support for most DaemonContext options',
+    author='Joseph Tate',
+    author_email='jtate@dragonstrider.com',
+    url='',
+    install_requires=[
+        'python-daemon',
+        ],
+    packages=find_packages(exclude=['ez_setup', 'tests']),
+    namespace_packages = ['daemon'],
+    zip_safe=True,
+    test_suite = 'nose.collector',
+    keywords = ['daemon', 'fork', 'unix', 'optparse', 'ConfigObj'
+    ],
+    classifiers = [
+        'Development Status :: 3 - Alpha',
+        'Topic :: Software Development :: Libraries :: Python Modules',
+        'Intended Audience :: Developers',
+        'Operating System :: POSIX',
+        'Programming Language :: Python',
+        'License :: OSI Approved :: MIT',
+    ],
+)