many improvements
- handle mail from sun source - print pretty tracebacks - ignore ref/control entries (emitted by ldapsearch) - replace last raise Error by assert - add objectclass to output ldif - replace misnaed icentite by iclibelleentite - replace use of ic_equipe_recherche by ic_equipe_recherche_dn - copy mail from sun source - filter None values
This commit is contained in:
parent
c193114b13
commit
5a46d546d1
|
@ -8,6 +8,8 @@ import collections
|
|||
import ldif
|
||||
import ldap.dn
|
||||
|
||||
from .tb import print_tb
|
||||
|
||||
DC = 'curie'
|
||||
O = 'Institut curie'
|
||||
UAI = '{UAI}ATROUVER'
|
||||
|
@ -72,7 +74,6 @@ class CurieLdifParser(ldif.LDIFParser):
|
|||
self.users = kwargs.pop('users', None) or collections.defaultdict(lambda: {})
|
||||
ldif.LDIFParser.__init__(self, *args, **kwargs)
|
||||
|
||||
|
||||
def assert_sv_attribute(self, entry, name):
|
||||
assert name in entry, 'attribut %s manquant' % name
|
||||
assert len(entry[name]) == 1, 'plus d\'un attribut %s' % name
|
||||
|
@ -81,13 +82,17 @@ class CurieLdifParser(ldif.LDIFParser):
|
|||
def handle(self, dn, entry):
|
||||
entry = lowercase_keys(entry)
|
||||
try:
|
||||
if 'ref' in entry:
|
||||
return
|
||||
if 'control' in entry:
|
||||
return
|
||||
if 'employeenumber' in entry:
|
||||
self.handle_ad(dn, entry)
|
||||
elif 'ICPersonne' in entry['objectclass']:
|
||||
self.handle_sun(dn, entry)
|
||||
else:
|
||||
raise Error(dn, 'entrée ignorée, car absence d\'attribut employeeNumber ou '
|
||||
'objectClass=ICPersonne')
|
||||
assert False, ('entrée ignorée, car absence d\'attribut employeeNumber ou '
|
||||
'objectClass=ICPersonne')
|
||||
except AssertionError, e:
|
||||
self.errors.append(Error(dn, str(e)))
|
||||
|
||||
|
@ -98,6 +103,8 @@ class CurieLdifParser(ldif.LDIFParser):
|
|||
supann_liste_rouge = 'FALSE'
|
||||
|
||||
self.users[uid].update({
|
||||
'objectClass': ['person', 'supannPerson', 'organizationalPerson', 'eduPerson',
|
||||
'inetOrgPerson'],
|
||||
'uid': uid,
|
||||
'supannAliasLogin': supann_alias_login,
|
||||
'userPassword': user_password,
|
||||
|
@ -108,7 +115,7 @@ class CurieLdifParser(ldif.LDIFParser):
|
|||
def handle_sun(self, dn, entry):
|
||||
uid = self.assert_sv_attribute(entry, 'uid')
|
||||
try:
|
||||
ic_entite = self.assert_sv_attribute(entry, 'icentite')
|
||||
ic_entite = self.assert_sv_attribute(entry, 'iclibelleentite')
|
||||
except AssertionError:
|
||||
ic_entite = None
|
||||
else:
|
||||
|
@ -118,12 +125,13 @@ class CurieLdifParser(ldif.LDIFParser):
|
|||
prenom = self.assert_sv_attribute(entry, 'icprenomnaissance')
|
||||
nom = self.assert_sv_attribute(entry, 'icnomnaissance')
|
||||
telephone = entry.get('telephoneNumber', [])
|
||||
mail = entry.get('mail', [])
|
||||
if ic_entite == 'Recherche':
|
||||
ic_equipe_recherche = self.assert_sv_attribute(entry, 'icequiperecherche')
|
||||
ic_equipe_recherche_dn = ldap.dn.str2dn(ic_equipe_recherche)
|
||||
assert ic_equipe_recherche[0][0][0].lower() == 'ou', ('ICEquipeRecherche ne contient '
|
||||
'pas le DN d\'une OU: %s'
|
||||
% ic_equipe_recherche)
|
||||
assert ic_equipe_recherche_dn[0][0][0].lower() == 'ou', \
|
||||
('ICEquipeRecherche ne contient pas le DN d\'une OU: %r %s' % (
|
||||
ic_equipe_recherche[0][0][0].lower(), ic_equipe_recherche))
|
||||
supann_entite_affectation_principale = ic_equipe_recherche_dn[0][0][1]
|
||||
else:
|
||||
supann_entite_affectation_principale = None
|
||||
|
@ -132,6 +140,7 @@ class CurieLdifParser(ldif.LDIFParser):
|
|||
'sn': nom,
|
||||
'givenName': prenom,
|
||||
'cn': strip_accents('%s %s' % (prenom, nom)).strip(),
|
||||
'mail': mail,
|
||||
}
|
||||
if supann_entite_affectation:
|
||||
d['supannEntiteAffectation'] = supann_entite_affectation
|
||||
|
@ -180,10 +189,14 @@ def main():
|
|||
if k.startswith('_'):
|
||||
continue
|
||||
if isinstance(v, list):
|
||||
v = filter(None, v)
|
||||
entry[k] = v
|
||||
else:
|
||||
elif v:
|
||||
entry[k] = [v]
|
||||
writer.unparse(ldap.dn.dn2str(dn), entry)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
try:
|
||||
main()
|
||||
except:
|
||||
print_tb()
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
from StringIO import StringIO
|
||||
import sys
|
||||
import linecache
|
||||
|
||||
|
||||
def print_tb():
|
||||
exc_type, exc_value, tb = sys.exc_info()
|
||||
if exc_value:
|
||||
exc_value = unicode(str(exc_value), errors='ignore')
|
||||
error_file = StringIO()
|
||||
|
||||
limit = None
|
||||
if hasattr(sys, 'tracebacklimit'):
|
||||
limit = sys.tracebacklimit
|
||||
print >>error_file, "Exception:"
|
||||
print >>error_file, " type = '%s', value = '%s'" % (exc_type, exc_value)
|
||||
print >>error_file
|
||||
|
||||
# format the traceback
|
||||
print >>error_file, 'Stack trace (most recent call first):'
|
||||
n = 0
|
||||
while tb is not None and (limit is None or n < limit):
|
||||
frame = tb.tb_frame
|
||||
function = frame.f_code.co_name
|
||||
filename = frame.f_code.co_filename
|
||||
exclineno = frame.f_lineno
|
||||
locals = frame.f_locals.items()
|
||||
|
||||
print >>error_file, ' File "%s", line %s, in %s' % (filename, exclineno, function)
|
||||
linecache.checkcache(filename)
|
||||
for lineno in range(exclineno - 2, exclineno + 3):
|
||||
line = linecache.getline(filename, lineno, frame.f_globals)
|
||||
if line:
|
||||
if lineno == exclineno:
|
||||
print >>error_file, '>%5s %s' % (lineno, line.rstrip())
|
||||
else:
|
||||
print >>error_file, ' %5s %s' % (lineno, line.rstrip())
|
||||
print >>error_file
|
||||
if locals:
|
||||
print >>error_file, " locals: "
|
||||
for key, value in locals:
|
||||
print >>error_file, " %s =" % key,
|
||||
try:
|
||||
repr_value = repr(value)
|
||||
if len(repr_value) > 10000:
|
||||
repr_value = repr_value[:10000] + ' [...]'
|
||||
print >>error_file, repr_value,
|
||||
except:
|
||||
print >>error_file, "<ERROR WHILE PRINTING VALUE>",
|
||||
print >>error_file
|
||||
print >>error_file
|
||||
tb = tb.tb_next
|
||||
n = n + 1
|
||||
|
||||
print error_file.getvalue()
|
Reference in New Issue