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