import logging
import re
# import sys
+from functools import cmp_to_key
from pathlib import Path
# Third party modules
from .ldap import BaseLdapApplication
from ..xlate import XLATOR
-__version__ = '0.8.5'
+__version__ = '0.8.6'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
# -------------------------------------------------------------------------
def eval_diffs(self):
- """Evaluate all entries to create, to change or to modify."""
- LOG.info(_('Evaluating all LDAP entries to create, to change or to modify.'))
+ """Evaluate all entries to create, to remove or to modify."""
+ LOG.info(_('Evaluating all LDAP entries to create, to remove or to modify.'))
+
+ self.eval_entries_to_remove()
+ self.eval_entries_to_create()
- self.aliases_to_create = []
- self.aliases_to_remove = []
self.aliases_to_modify = []
+ # -------------------------------------------------------------------------
+ def eval_entries_to_remove(self):
+ """Evaluate all LDAP entries to remove."""
+ LOG.debug(_('Evaluating all LDAP entries to remove ...'))
+
+ self.aliases_to_remove = []
+
for dn in self.ldap_aliases:
entry = self.ldap_aliases[dn]
do_delete = False
if do_delete:
self.aliases_to_remove.append(dn)
- if self.berbose > 1:
+ self.aliases_to_remove = sorted(
+ self.aliases_to_remove, key=cmp_to_key(self.compare_ldap_dns))
+
+ nr_remove = len(self.aliases_to_remove)
+
+ if nr_remove:
+ msg = ngettext(
+ '{} LDAP entry should be removed.', '{} LDAP entries should be removed.',
+ nr_remove).format(nr_remove)
+ else:
+ msg = _('No LDAP entries should be removed.')
+ LOG.info(msg)
+ if self.verbose > 2:
msg = _('LDAP Entries to remove:') + '\n' + pp(self.aliases_to_remove)
LOG.debug(msg)
+ # -------------------------------------------------------------------------
+ def eval_entries_to_create(self):
+ """Evaluate all LDAP entries to create."""
+ LOG.debug(_('Evaluating all LDAP entries to create ...'))
+
+ self.aliases_to_create = []
+
+ for cn in self.existing_aliases.keys():
+ local_alias = self.existing_aliases[cn]
+ mail = local_alias['alias']
+
+ if mail in self.ldap_mail_dns:
+ if self.verbose > 2:
+ LOG.debug(_(
+ 'Mail {!r} already exists in regular LDAP entries.').format(mail))
+ continue
+
+ do_create = True
+ for dn in self.ldap_aliases:
+ entry = self.ldap_aliases[dn]
+ if mail in entry['mail']:
+ do_create = False
+ break
+ if do_create:
+ self.aliases_to_create.append(cn)
+
+ self.aliases_to_create.sort(key=str.lower)
+
+ nr_create = len(self.aliases_to_create)
+
+ if nr_create:
+ msg = ngettext(
+ '{} LDAP entry should be created.', '{} LDAP entries should be created.',
+ nr_create).format(nr_create)
+ else:
+ msg = _('No LDAP entries should be created.')
+ LOG.info(msg)
+ if self.verbose > 2:
+ msg = _('LDAP Entries to create:') + '\n' + pp(self.aliases_to_create)
+ LOG.debug(msg)
+
+
# -------------------------------------------------------------------------
def _run(self):