]> Frank Brehm's Git Trees - pixelpark/ldap-migration.git/commitdiff
Retrieving structural Entries
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 16 Nov 2020 15:29:43 +0000 (16:29 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 16 Nov 2020 15:29:43 +0000 (16:29 +0100)
lib/ldap_migration/__init__.py

index 3f36f45c48e89ba97ef71e91c010f8da3ad0447b..533afa4dcab6e0efe58ff35575e241d188086a44 100644 (file)
@@ -35,7 +35,7 @@ from fb_tools.errors import FbAppError
 from .config import LDAPMigrationConfiguration
 from .idict import CaseInsensitiveDict
 
-__version__ = '0.5.1'
+__version__ = '0.5.2'
 
 LOG = logging.getLogger(__name__)
 CFG_BASENAME = 'ldap-migration.ini'
@@ -83,7 +83,8 @@ class LDAPMigrationApplication(BaseApplication):
 
         self.object_classes = {}
         self.attribute_types = {}
-        self.dns = {}
+        self.dns = CaseInsensitiveDict()
+        self.struct_dns = CaseInsensitiveDict()
 
         super(LDAPMigrationApplication, self).__init__(
             appname=appname, verbose=verbose, version=version, base_dir=base_dir,
@@ -122,6 +123,8 @@ class LDAPMigrationApplication(BaseApplication):
         res = super(LDAPMigrationApplication, self).as_dict(short=short)
         res['cfg_dir'] = self.cfg_dir
         res['cfg_file'] = self.cfg_file
+        res['dns'] = self.dns.as_dict(short=short)
+        res['struct_dns'] = self.struct_dns.as_dict(short=short)
 
         return res
 
@@ -571,14 +574,29 @@ class LDAPMigrationApplication(BaseApplication):
         return ','.join(parts)
 
     # -------------------------------------------------------------------------
-    def register_dn_tokens(self, dn):
-
-        tokens = reversed(self.re_dn_split.split(dn))
-        cur_hash = self.dns
-        for token in tokens:
-            if token not in cur_hash:
-                cur_hash[token] = {}
-            cur_hash = cur_hash[token]
+    def register_dn_tokens(self, dn, object_classes, cur_hash=None):
+
+        if cur_hash is None:
+            cur_hash = self.dns
+
+        iter_hash = cur_hash
+        tokens = []
+        for token in reversed(self.re_dn_split.split(dn)):
+            tokens.append(token)
+        tokens_copy = copy.copy(tokens)
+        while len(tokens):
+            token = tokens.pop(0)
+            if 'childs' not in iter_hash:
+                iter_hash['childs'] = CaseInsensitiveDict()
+            if token not in iter_hash['childs']:
+                iter_hash['childs'][token] = CaseInsensitiveDict()
+            if 'childs' not in iter_hash['childs'][token]:
+                iter_hash['childs'][token]['childs'] = CaseInsensitiveDict()
+            iter_hash = iter_hash['childs'][token]
+            if not len(tokens):
+                iter_hash['dn'] = dn
+                iter_hash['path'] = tokens_copy
+                iter_hash['object_classes'] = object_classes
 
     # -------------------------------------------------------------------------
     def get_all_dns(self):
@@ -592,6 +610,7 @@ class LDAPMigrationApplication(BaseApplication):
             'mode': 'w',
         }
         sfilter = '(objectClass=*)'
+        attrs = ['objectClass']
 
         item_count = 0
 
@@ -599,20 +618,24 @@ class LDAPMigrationApplication(BaseApplication):
 
             status, result, response, _ = self.source.search(
                 search_base=self.config.suffix, search_scope=SUBTREE, search_filter=sfilter,
-                attributes=['objectClass'], time_limit=self.config.timeout)
+                attributes=attrs, time_limit=self.config.timeout)
             for entry in response:
                 item_count += 1
+                if self.verbose > 3:
+                    LOG.debug("Entry no. {}:".format(item_count) + '\n' + pp(entry))
                 old_dn = entry['dn']
                 new_dn = self.mangle_dn(old_dn)
                 if self.verbose > 2:
                     LOG.debug("Found DN {!r}.".format(old_dn))
-                self.register_dn_tokens(old_dn)
+                self.register_dn_tokens(old_dn, entry['attributes']['objectClass'])
                 fh.write("{old} => {new}\n".format(old=old_dn, new=new_dn))
+#                if item_count >= 100:
+#                    break
 
         LOG.info("Found {nr} items in subtree of {sfx!r}.".format(
             nr=item_count, sfx=self.config.suffix))
-        if self.verbose > 3:
-            LOG.debug("Registred DN tokens:\n{}".format(pp(self.dns)))
+        if self.verbose > 4:
+            LOG.debug("Registred DN tokens:\n{}".format(pp(self.dns.as_dict())))
 
     # -------------------------------------------------------------------------
     def get_structural_dns(self):
@@ -621,7 +644,6 @@ class LDAPMigrationApplication(BaseApplication):
             str(self.structural_dns_file)))
 
         cur_hash = self.dns
-        cur_tokens = []
 
         open_args = {
             'encoding': 'utf-8',
@@ -630,26 +652,41 @@ class LDAPMigrationApplication(BaseApplication):
         }
 
         with self.structural_dns_file.open(**open_args) as fh:
-            self._get_structural_dns(fh, cur_hash, cur_tokens)
+            count_dns = self._get_structural_dns(fh, cur_hash, count_dns=0, is_root=True)
 
-        # TODO
-        # Noch viele viele Fehler!
+        LOG.info("Found {nr} structural items in subtree of {sfx!r}.".format(
+            nr=count_dns, sfx=self.config.suffix))
 
     # -------------------------------------------------------------------------
-    def _get_structural_dns(self, fh, cur_hash, cur_tokens):
+    def _get_structural_dns(self, fh, cur_hash, count_dns, is_root=False):
 
-        if not cur_hash.keys():
-            return
+        if 'childs' not in cur_hash:
+            if self.verbose > 3:
+                LOG.debug("'childs' not in cur_hash")
+            return count_dns
 
-        if cur_tokens:
-            my_dn = ','.join(reversed(cur_tokens))
-            LOG.debug("Found stuctural DN: {!r}".format(my_dn))
+        if not cur_hash['childs'].keys():
+            if self.verbose > 3:
+                LOG.debug("cur_hash['childs'] has no subentries.")
+            return count_dns
+
+        if not is_root:
+            if 'path' not in cur_hash:
+                if self.verbose > 3:
+                    LOG.debug("'path' not in cur_hash")
+                return count_dns
+
+            my_dn = cur_hash['dn']
+            if self.verbose > 2:
+                LOG.debug("Found stuctural DN: {!r}".format(my_dn))
             fh.write("{}\n".format(my_dn))
 
-        for key in sorted(cur_hash.keys(), key=str.lower):
-            sub_tokens = copy.copy(cur_tokens)
-            sub_tokens.append(key)
-            self._get_structural_dns(fh, cur_hash[key], sub_tokens)
+            count_dns += 1
+
+        for key in cur_hash['childs'].keys():
+            count_dns = self._get_structural_dns(fh, cur_hash['childs'][key], count_dns, False)
+
+        return count_dns
 
     # -------------------------------------------------------------------------
     def _run(self):