From 9f183fbd89ebea0a3a66cf7da3cfb6effa1a4472 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 30 Nov 2020 18:08:33 +0100 Subject: [PATCH] Rewriting logic of iteratinf through the tree --- lib/ldap_migration/__init__.py | 54 +++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/ldap_migration/__init__.py b/lib/ldap_migration/__init__.py index b4726b5..a3dbf98 100644 --- a/lib/ldap_migration/__init__.py +++ b/lib/ldap_migration/__init__.py @@ -45,7 +45,7 @@ from .config import LDAPMigrationConfiguration from .idict import CaseInsensitiveDict from .istringset import CaseInsensitiveStringSet -__version__ = '0.8.2' +__version__ = '0.8.3' LOG = logging.getLogger(__name__) CFG_BASENAME = 'ldap-migration.ini' @@ -196,6 +196,12 @@ class LDAPMigrationApplication(BaseApplication): self.all_dns_file = None self.structural_dns_file = None self.migrated_file = None + self.lap = 0 + self.total_count = 0 + + self.struct_entries = [] + self.all_entries = [] + self.group_entries = CaseInsensitiveDict() self.limit = 0 @@ -209,7 +215,6 @@ class LDAPMigrationApplication(BaseApplication): self.struct_dns = CaseInsensitiveDict() self.migrated_entries = CaseInsensitiveDict() self.integer_attribute_types = CaseInsensitiveStringSet([]) - self.group_entries = CaseInsensitiveDict() super(LDAPMigrationApplication, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, @@ -958,9 +963,30 @@ class LDAPMigrationApplication(BaseApplication): if self.verbose > 4: LOG.debug("Registred DN tokens:\n{}".format(pp(self.dns.as_dict()))) + # ------------------------------------------------------------------------- + def get_entry_list(self): + + print() + LOG.info("Getting an ordered list of all source entries ...") + + self.all_entries = [] + cur_hash = self.dns + self._get_entries(cur_hash) + + # ------------------------------------------------------------------------- + def _get_entries(cur_hash): + + if 'dn' in cur_hash: + self.struct_entries.append(cur_hash['dn']) + + if 'childs' in cur_hash: + for key in cur_hash['childs'].keys(): + self._get_entries(fh, cur_hash['childs'][key]) + # ------------------------------------------------------------------------- def get_structural_dns(self): + print() LOG.info("Collecting all structural and writing them into {!r} ...".format( str(self.structural_dns_file))) @@ -1028,11 +1054,13 @@ class LDAPMigrationApplication(BaseApplication): with self.migrated_file.open(**open_args) as fh: + self.lap = 1 if not self.migrate_structural_entries(fh): return False + self.total_count = 0 + self.lap = 2 if not self.only_struct: - if not self.migrate_all_entries(fh): return False @@ -1246,19 +1274,36 @@ class LDAPMigrationApplication(BaseApplication): with_group_entries=True, with_acl=False): wait = self.config.wait_after_write + point = '' + + self.total_count += 1 + if self.verbose > 2: + LOG.debug("Migrating entry number {} ...".format(self.total_count)) if not is_root: + if 'dn' not in cur_hash: + msg = "Key 'dn' not found in current hash. Current keys:\n{}".format( + pp(cur_hash.keys())) + raise KeyError(msg) + src_dn = cur_hash['dn'] + point = src_dn if self.migrate_entry( src_dn, fh=fh, force=force, with_acl=with_acl, migrate_if_group=with_group_entries): if wait: time.sleep(wait) + if self.verbose > 2: + LOG.debug("Migrating childs of {!r} ...".format(point)) for key in cur_hash['childs'].keys(): + child = cur_hash['childs'][key] + if self.lap == 2 and self.total_count > 1 and self.verbose > 2: + LOG.debug("Migrating child of key {k!r}:\n{c}".format( + k=key, c=pp(child))) self._migrate_entries( - cur_hash['childs'][key], fh=fh, force=force, is_root=False, + child, fh=fh, force=force, is_root=False, with_group_entries=with_group_entries, with_acl=with_acl) # ------------------------------------------------------------------------- @@ -1421,6 +1466,7 @@ class LDAPMigrationApplication(BaseApplication): self.discover_target_schema() self.check_tmp_dir() self.get_all_dns() + self.get_entry_list() self.get_structural_dns() self.migrate_entries() if self.verbose > 1: -- 2.39.5