[Checkins] SVN: z3c.recipe.ldap/trunk/z3c/recipe/ldap/ Refactor

Ross Patterson me at rpatterson.net
Mon Dec 10 13:49:44 EST 2007


Log message for revision 82244:
  Refactor
  

Changed:
  A   z3c.recipe.ldap/trunk/z3c/recipe/ldap/conf.py
  A   z3c.recipe.ldap/trunk/z3c/recipe/ldap/ctl.py
  U   z3c.recipe.ldap/trunk/z3c/recipe/ldap/slapd.py

-=-
Added: z3c.recipe.ldap/trunk/z3c/recipe/ldap/conf.py
===================================================================
--- z3c.recipe.ldap/trunk/z3c/recipe/ldap/conf.py	                        (rev 0)
+++ z3c.recipe.ldap/trunk/z3c/recipe/ldap/conf.py	2007-12-10 18:49:43 UTC (rev 82244)
@@ -0,0 +1,71 @@
+import os
+
+exclude = ['slapd', 'conf', 'urls', 'use-socket', 'recipe',
+           'location', 'executable', 'bin-directory',
+           'eggs-directory', 'develop-eggs-directory',
+           '_e', '_d', '_b']
+paths = ['include', 'pidfile', 'argsfile', 'modulepath', 'directory']
+multiple = ['include', 'access', 'moduleload', 'dbconfig', 'index']
+defaults = [('include', '/etc/openldap/schema/core.schema'),
+            ('modulepath', '/usr/lib/openldap'),
+            ('moduleload', 'back_bdb'),
+            ('database', 'bdb'),
+            ('dbconfig', ('set_cachesize\t0\t268435456\t1\n'
+                          'set_lg_regionmax\t262144\n'
+                          'set_lg_bsize\t2097152')),
+            ('index', 'objectClass\teq')]
+order = ['include', 'pidfile', 'argsfile', 'access', 'modulepath',
+         'moduleload', 'database', 'suffix', 'directory', 'dbconfig',
+         'index']
+
+def init_options(options, dir='.', exclude=exclude,
+                      paths=paths, multiple=multiple,
+                      defaults=defaults):
+    for key, value in defaults:
+        if key not in options:
+            options[key] = value
+
+    for key, value in options.iteritems():
+        value = value.strip()
+        if not value:
+            del options[key]
+            continue
+        if key in exclude:
+            continue
+
+        if key in multiple:
+            values = []
+            for v in value.split('\n'):
+                v = v.strip()
+                if not v:
+                    continue
+                if key in paths:
+                    # expand file paths
+                    v = os.path.join(dir, v)
+                values.append(v)
+            options[key] = '\n'.join(values)
+            continue
+
+        if key in paths:
+            options[key] = os.path.join(dir, value)
+
+def order_keys(keys, order=order):
+    for key in order:
+        if key in keys:
+            yield key
+    for key in keys:
+        if key not in order:
+            yield key
+
+def get_lines(options, exclude=exclude,
+                   multiple=multiple, template='%s\t%s\n'):
+    for key in order_keys(options):
+        if key in exclude:
+            continue
+
+        value = options[key]
+        if key in multiple:
+            for v in value.split('\n'):
+                yield template % (key, v)
+        else:
+            yield template % (key, value)

Added: z3c.recipe.ldap/trunk/z3c/recipe/ldap/ctl.py
===================================================================
--- z3c.recipe.ldap/trunk/z3c/recipe/ldap/ctl.py	                        (rev 0)
+++ z3c.recipe.ldap/trunk/z3c/recipe/ldap/ctl.py	2007-12-10 18:49:43 UTC (rev 82244)
@@ -0,0 +1,17 @@
+import sys, os, subprocess, signal
+
+def main(options):
+    command = sys.argv[1]
+    if command.lower() == 'start':
+        args = [options['slapd'], '-f', options['conf']]
+        if 'urls' in options:
+            args.extend(['-h', options['urls']])
+        args.extend(sys.argv[2:])
+        subprocess.Popen(args)
+    elif command.lower() == 'stop':
+        pidfile = file(options['pidfile'])
+        pid = int(pidfile.read())
+        pidfile.close()
+        os.kill(pid, signal.SIGTERM)
+    else:
+        raise ValueError('Command %s unsupported' % command)

Modified: z3c.recipe.ldap/trunk/z3c/recipe/ldap/slapd.py
===================================================================
--- z3c.recipe.ldap/trunk/z3c/recipe/ldap/slapd.py	2007-12-10 17:14:05 UTC (rev 82243)
+++ z3c.recipe.ldap/trunk/z3c/recipe/ldap/slapd.py	2007-12-10 18:49:43 UTC (rev 82244)
@@ -6,6 +6,8 @@
 import zc.buildout
 import zc.recipe.egg
 
+import conf
+
 class Slapd(object):
     """This recipe is used by zc.buildout"""
 
@@ -42,16 +44,16 @@
                 options['location'], name+'.socket')
 
         # Initialize the conf options
-        init_conf_options(
+        conf.init_options(
             options, dir=buildout['buildout']['directory'])
 
     def install(self):
         """installer"""
         # Install slapd.conf
         os.makedirs(self.options['location'])
-        conf = file(self.options['conf'], 'w')
-        conf.writelines(get_conf_lines(self.options))
-        conf.close()
+        conf_file = file(self.options['conf'], 'w')
+        conf_file.writelines(conf.get_lines(self.options))
+        conf_file.close()
 
         if not os.path.exists(self.options['directory']):
             # Install the DB dir
@@ -60,7 +62,7 @@
         # Install the control script
         _, ws = self.egg.working_set(['z3c.recipe.ldap'])
         zc.buildout.easy_install.scripts(
-            [(self.name, 'z3c.recipe.ldap.slapd', 'ctl')],
+            [(self.name, 'z3c.recipe.ldap.ctl', 'main')],
             ws, self.options['executable'],
             self.options['bin-directory'],
             arguments=repr(self.options))
@@ -70,81 +72,3 @@
     def update(self):
         """updater"""
         pass
-
-conf_exclude = [
-    'slapd', 'conf', 'urls', 'recipe', 'location', 'executable',
-    'bin-directory', 'eggs-directory', 'develop-eggs-directory',
-    '_e', '_d', '_b']
-conf_paths = [
-    'include', 'pidfile', 'argsfile', 'directory', 'modulepath']
-conf_multiple = ['include', 'moduleload', 'access', 'index']
-conf_defaults = [('modulepath', '/usr/lib/ldap'),
-                 ('moduleload', 'back_bdb'),
-                 ('database', 'bdb'),
-                 ('index', 'objectClass\teq')]
-conf_order = [
-    'include', 'pidfile', 'argsfile', 'access', 'modulepath',
-    'moduleload', 'database', 'suffix', 'directory', 'index']
-
-def init_conf_options(options, dir='.', exclude=conf_exclude,
-                      paths=conf_paths, multiple=conf_multiple,
-                      defaults=conf_defaults):
-    for key, value in defaults:
-        if key not in options:
-            options[key] = value
-
-    for key, value in options.iteritems():
-        if key in exclude:
-            continue
-
-        if key in multiple:
-            values = []
-            for v in value.split('\n'):
-                v = v.strip()
-                if not v:
-                    continue
-                if key in paths:
-                    # expand file paths
-                    v = os.path.join(dir, v)
-                values.append(v)
-            options[key] = '\n'.join(values)
-            continue
-
-        if key in paths:
-            options[key] = os.path.join(dir, value)
-
-def order_keys(keys, order=conf_order):
-    for key in order:
-        if key in keys:
-            yield key
-    for key in keys:
-        if key not in order:
-            yield key
-
-def get_conf_lines(options, exclude=conf_exclude,
-                   multiple=conf_multiple, template='%s\t%s\n'):
-    for key in order_keys(options):
-        if key in exclude:
-            continue
-
-        value = options[key]
-        if key in multiple:
-            for v in value.split('\n'):
-                yield template % (key, v)
-        else:
-            yield template % (key, value)
-
-def ctl(options):
-    command, = sys.argv[1:]
-    if command.lower() == 'start':
-        args = [options['slapd'], '-f', options['conf']]
-        if 'urls' in options:
-            args.extend(['-h', options['urls']])
-        subprocess.Popen(args)
-    elif command.lower() == 'stop':
-        pidfile = file(options['pidfile'])
-        pid = int(pidfile.read())
-        pidfile.close()
-        os.kill(pid, signal.SIGTERM)
-    else:
-        raise ValueError('Command %s unsupported' % command)



More information about the Checkins mailing list