Patch for broken slapd.py

Christian Oudard christian.oudard at gmail.com
Tue Dec 15 15:53:09 CET 2009


On my system, slapd.py fails because of a missing /var/tmp/python-ldap-test .

Not sure what your patch submission procedure is, but here's a patch
to fix that:
==================================


diff --git a/pstat/dynamic_ldap/slapd.py b/pstat/dynamic_ldap/slapd.py
index 735b58e..2cd3e25 100644
--- a/pstat/dynamic_ldap/slapd.py
+++ b/pstat/dynamic_ldap/slapd.py
@@ -13,12 +13,25 @@ def quote(s):
     '''Quotes the '"' and '\' characters in a string and surrounds
with "..."'''
     return '"' + s.replace('\\','\\\\').replace('"','\\"') + '"'

-def mkdirs(path):
-    """Creates the directory path unless it already exists"""
-    if not os.access(os.path.join(path, os.path.curdir), os.F_OK):
-        _log.debug("creating temp directory %s", path)
-        os.mkdir(path)
-    return path
+def mkdir(newdir):
+    #http://code.activestate.com/recipes/82465/
+    """works the way a good mkdir should :)
+        - already exists, silently complete
+        - regular file in the way, raise an exception
+        - parent directory(ies) does not exist, make them as well
+    """
+    if os.path.isdir(newdir):
+        pass
+    elif os.path.isfile(newdir):
+        raise OSError("a file with the same name as the desired " \
+                      "dir, '%s', already exists." % newdir)
+    else:
+        head, tail = os.path.split(newdir)
+        if head and not os.path.isdir(head):
+            mkdir(head)
+        if tail:
+            os.mkdir(newdir)
+    return newdir

 def delete_directory_content(path):
     for dirpath,dirnames,filenames in os.walk(path, topdown=False):
@@ -138,7 +151,8 @@ class Slapd:
         cfg.append("allow bind_v2")

         # Database
-        ldif_dir = mkdirs(os.path.join(self.get_tmpdir(), "ldif-data"))
+        ldif_dir = os.path.join(self.get_tmpdir(), "ldif-data")
+        mkdir(ldif_dir)
         delete_directory_content(ldif_dir) # clear it out
         cfg.append("database ldif")
         cfg.append("directory " + quote(ldif_dir))
@@ -150,7 +164,7 @@ class Slapd:
     def _write_config(self):
         """Writes the slapd.conf file out, and returns the path to it."""
         path = os.path.join(self._tmpdir, "slapd.conf")
-        ldif_dir = mkdirs(self._tmpdir)
+        mkdir(self._tmpdir)
         if os.access(path, os.F_OK):
             self._log.debug("deleting existing %s", path)
             os.remove(path)




More information about the python-ldap mailing list