Eu tive o mesmo problema há vários anos e escrevi um script python muito pequeno para unificar a exportação LDIF de um catálogo de endereços do Thunderbird:
- exportar o livro de endereços como LDIF para, e. abook.ldif
- executar
cat abook.ldif | unify_ldif.py > abook_new.ldif
- import abook_new.ldif novamente (talvez renomeie o antigo addressbook antes)
O script atualmente corresponde a entradas duplicadas no endereço de e-mail e nome idêntico, mas isso pode ser adaptado (na função find_existing_entry
). Isso funciona para você?
O programa está aqui (EDIT: você precisa do pacote python-ldap
):
#!/usr/bin/env python
import sys
from ldif import LDIFParser, LDIFWriter
def find_existing_entry(ldif_entries, ldif_entry):
for dn, entry in ldif_entries.items():
if 'mail' in ldif_entry and 'mail' in entry:
for mail in ldif_entry['mail']:
if 'mail' in entry and mail in entry['mail']:
return dn
if 'cn' in ldif_entry and 'cn' in entry and ldif_entry['cn'][0] in entry['cn']:
return dn
if 'sn' in ldif_entry and 'sn' in entry and 'givenName' in ldif_entry and 'givenName' in entry and ldif_entry['sn'][0] in entry['sn'] and ldif_entry['givenName'][0] in entry['givenName']:
return dn
return ''
class MyLDIF(LDIFParser):
def __init__(self, input, output):
LDIFParser.__init__(self, input)
self.writer = LDIFWriter(output)
self.entries = {}
def merge(self, dn, entry):
if 'mail' in entry.keys():
if 'mail' in self.entries[dn].keys():
for mail in entry['mail']:
if mail not in self.entries[dn]['mail']:
self.entries[dn]['mail'].append(mail)
else:
self.entries[dn]['mail'] = entry['mail']
for key in entry.keys():
if key not in self.entries[dn].keys():
self.entries[dn][key] = entry[key]
def handle(self, dn, entry):
if dn in self.entries.keys():
self.merge(dn, entry)
else:
found_dn = find_existing_entry(self.entries, entry)
if found_dn != '':
self.merge(found_dn, entry)
else:
self.entries[dn] = entry
def output(self):
for dn, entry in self.entries.items():
self.writer.unparse(dn, entry)
parser = MyLDIF(sys.stdin, sys.stdout)
parser.parse()
parser.output()