misc/scripts/ldapuser2ldapfeed.py
changeset 8535 268b6349baf3
parent 8483 4ba11607d84a
parent 8521 dfdffebce8a4
child 8645 310040c668c0
equal deleted inserted replaced
8525:c09feae04094 8535:268b6349baf3
     1 """turn a pyro source into a datafeed source
     1 """turn a pyro source into a datafeed source
     2 
     2 
     3 Once this script is run, execute c-c db-check to cleanup relation tables.
     3 Once this script is run, execute c-c db-check to cleanup relation tables.
     4 """
     4 """
     5 import sys
     5 import sys
       
     6 from collections import defaultdict
       
     7 from logilab.common.shellutils import generate_password
     6 
     8 
     7 try:
     9 try:
     8     source_name, = __args__
    10     source_name, = __args__
     9     source = repo.sources_by_uri[source_name]
    11     source = repo.sources_by_uri[source_name]
    10 except ValueError:
    12 except ValueError:
    31 
    33 
    32 session.mode = 'write' # hold on the connections set
    34 session.mode = 'write' # hold on the connections set
    33 
    35 
    34 print '******************** backport entity content ***************************'
    36 print '******************** backport entity content ***************************'
    35 
    37 
    36 todelete = {}
    38 todelete = defaultdict(list)
       
    39 extids = set()
       
    40 duplicates = []
    37 for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities():
    41 for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities():
    38         etype = entity.__regid__
    42     etype = entity.__regid__
    39         if not source.support_entity(etype):
    43     if not source.support_entity(etype):
    40             print "source doesn't support %s, delete %s" % (etype, entity.eid)
    44         print "source doesn't support %s, delete %s" % (etype, entity.eid)
    41         else:
    45         todelete[etype].append(entity)
    42             try:
    46         continue
    43                 entity.complete()
    47     try:
    44             except Exception:
    48         entity.complete()
    45                 print '%s %s much probably deleted, delete it (extid %s)' % (
    49     except Exception:
    46                     etype, entity.eid, entity.cw_metainformation()['extid'])
    50         print '%s %s much probably deleted, delete it (extid %s)' % (
    47             else:
    51             etype, entity.eid, entity.cw_metainformation()['extid'])
    48                 print 'get back', etype, entity.eid
    52         todelete[etype].append(entity)
    49                 entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache)
    53         continue
    50                 if not entity.creation_date:
    54     print 'get back', etype, entity.eid
    51                     entity.cw_edited['creation_date'] = datetime.now()
    55     entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache)
    52                 if not entity.modification_date:
    56     if not entity.creation_date:
    53                     entity.cw_edited['modification_date'] = datetime.now()
    57         entity.cw_edited['creation_date'] = datetime.now()
    54                 if not entity.upassword:
    58     if not entity.modification_date:
    55                     entity.cw_edited['upassword'] = u''
    59         entity.cw_edited['modification_date'] = datetime.now()
    56                 if not entity.cwuri:
    60     if not entity.upassword:
    57                     entity.cw_edited['cwuri'] = '%s/?dn=%s' % (
    61         entity.cw_edited['upassword'] = generate_password()
    58                         source.urls[0], entity.cw_metainformation()['extid'])
    62     extid = entity.cw_metainformation()['extid']
    59                 print entity.cw_edited
    63     if not entity.cwuri:
    60                 system_source.add_entity(session, entity)
    64         entity.cw_edited['cwuri'] = '%s/?dn=%s' % (
    61                 sql("UPDATE entities SET source='system' "
    65             source.urls[0], extid.decode('utf-8', 'ignore'))
    62                     "WHERE eid=%(eid)s", {'eid': entity.eid})
    66     print entity.cw_edited
    63                 continue
    67     if extid in extids:
    64         todelete.setdefault(etype, []).append(entity)
    68         duplicates.append(extid)
       
    69         continue
       
    70     extids.add(extid)
       
    71     system_source.add_entity(session, entity)
       
    72     sql("UPDATE entities SET source='system' "
       
    73         "WHERE eid=%(eid)s", {'eid': entity.eid})
    65 
    74 
    66 # only cleanup entities table, remaining stuff should be cleaned by a c-c
    75 # only cleanup entities table, remaining stuff should be cleaned by a c-c
    67 # db-check to be run after this script
    76 # db-check to be run after this script
    68 for entities in todelete.values():
    77 if duplicates:
       
    78     print 'found %s duplicate entries' % len(duplicates)
       
    79     from pprint import pprint
       
    80     pprint(duplicates)
       
    81 
       
    82 print len(todelete), 'entities will be deleted'
       
    83 for etype, entities in todelete.values():
       
    84     print 'deleting', etype, [e.login for e in entities]
    69     system_source.delete_info_multi(session, entities, source_name)
    85     system_source.delete_info_multi(session, entities, source_name)
       
    86 
    70 
    87 
    71 
    88 
    72 source_ent = rql('CWSource S WHERE S eid %(s)s', {'s': source.eid}).get_entity(0, 0)
    89 source_ent = rql('CWSource S WHERE S eid %(s)s', {'s': source.eid}).get_entity(0, 0)
    73 source_ent.cw_set(type=u"ldapfeed", parser=u"ldapfeed")
    90 source_ent.cw_set(type=u"ldapfeed", parser=u"ldapfeed")
    74 
    91 
    75 
    92 
    76 commit()
    93 if raw_input('Commit ?') in 'yY':
       
    94     print 'committing'
       
    95     commit()
       
    96 else:
       
    97     rollback()
       
    98     print 'rollbacked'
       
    99