From 2bf6ae93c9d47a87aea38ab25add2cf558e20ff5 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 13 Nov 2020 15:04:58 +0100 Subject: [PATCH] Trying to detect structural items --- lib/ldap_migration/__init__.py | 58 ++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/ldap_migration/__init__.py b/lib/ldap_migration/__init__.py index a790614..02e901a 100644 --- a/lib/ldap_migration/__init__.py +++ b/lib/ldap_migration/__init__.py @@ -34,7 +34,7 @@ from fb_tools.errors import FbAppError from .config import LDAPMigrationConfiguration -__version__ = '0.5.0' +__version__ = '0.5.1' LOG = logging.getLogger(__name__) CFG_BASENAME = 'ldap-migration.ini' @@ -78,9 +78,11 @@ class LDAPMigrationApplication(BaseApplication): self.target = None self.tmp_dir = None self.all_dns_file = None + self.structural_dns_file = None self.object_classes = {} self.attribute_types = {} + self.dns = {} super(LDAPMigrationApplication, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, @@ -89,6 +91,7 @@ class LDAPMigrationApplication(BaseApplication): self.tmp_dir = self.base_dir / 'tmp' self.all_dns_file = self.tmp_dir / 'all-dns.txt' + self.structural_dns_file = self.tmp_dir / 'structural-dns.txt' self.initialized = True # ------------------------------------------------------------------------- @@ -566,10 +569,20 @@ 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 get_all_dns(self): - LOG.info("Collecting all source SNs and writing them into {!r} ...".format( + LOG.info("Collecting all source DNs and writing them into {!r} ...".format( str(self.all_dns_file))) open_args = { @@ -592,10 +605,50 @@ class LDAPMigrationApplication(BaseApplication): 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) fh.write("{old} => {new}\n".format(old=old_dn, new=new_dn)) 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))) + + # ------------------------------------------------------------------------- + def get_structural_dns(self): + + LOG.info("Collecting all structural and writing them into {!r} ...".format( + str(self.structural_dns_file))) + + cur_hash = self.dns + cur_tokens = [] + + open_args = { + 'encoding': 'utf-8', + 'errors': 'surrogateescape', + 'mode': 'w', + } + + with self.structural_dns_file.open(**open_args) as fh: + self._get_structural_dns(fh, cur_hash, cur_tokens) + + # TODO + # Noch viele viele Fehler! + + # ------------------------------------------------------------------------- + def _get_structural_dns(self, fh, cur_hash, cur_tokens): + + if not cur_hash.keys(): + return + + if cur_tokens: + my_dn = ','.join(reversed(cur_tokens)) + 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) # ------------------------------------------------------------------------- def _run(self): @@ -609,6 +662,7 @@ class LDAPMigrationApplication(BaseApplication): self.discover_target_schema() self.check_tmp_dir() self.get_all_dns() + self.get_structural_dns() LOG.info("Sleeping ...") time.sleep(2) finally: -- 2.39.5