misc/scripts/ldapuser2ldapfeed.py
changeset 8188 1867e252e487
child 8468 f52bb4226020
child 8483 4ba11607d84a
equal deleted inserted replaced
8187:981f6e487788 8188:1867e252e487
       
     1 """turn a pyro source into a datafeed source
       
     2 
       
     3 Once this script is run, execute c-c db-check to cleanup relation tables.
       
     4 """
       
     5 import sys
       
     6 
       
     7 try:
       
     8     source_name, = __args__
       
     9     source = repo.sources_by_uri[source_name]
       
    10 except ValueError:
       
    11     print('you should specify the source name as script argument (i.e. after --'
       
    12           ' on the command line)')
       
    13     sys.exit(1)
       
    14 except KeyError:
       
    15     print '%s is not an active source' % source_name
       
    16     sys.exit(1)
       
    17 
       
    18 # check source is reachable before doing anything
       
    19 if not source.get_connection().cnx:
       
    20     print '%s is not reachable. Fix this before running this script' % source_name
       
    21     sys.exit(1)
       
    22 
       
    23 raw_input('Ensure you have shutdown all instances of this application before continuing.'
       
    24           ' Type enter when ready.')
       
    25 
       
    26 system_source = repo.system_source
       
    27 
       
    28 from datetime import datetime
       
    29 from cubicweb.server.edition import EditedEntity
       
    30 
       
    31 
       
    32 session.mode = 'write' # hold on the connections set
       
    33 
       
    34 print '******************** backport entity content ***************************'
       
    35 
       
    36 todelete = {}
       
    37 for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities():
       
    38         etype = entity.__regid__
       
    39         if not source.support_entity(etype):
       
    40             print "source doesn't support %s, delete %s" % (etype, entity.eid)
       
    41         else:
       
    42             try:
       
    43                 entity.complete()
       
    44             except Exception:
       
    45                 print '%s %s much probably deleted, delete it (extid %s)' % (
       
    46                     etype, entity.eid, entity.cw_metainformation()['extid'])
       
    47             else:
       
    48                 print 'get back', etype, entity.eid
       
    49                 entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache)
       
    50                 if not entity.creation_date:
       
    51                     entity.cw_edited['creation_date'] = datetime.now()
       
    52                 if not entity.modification_date:
       
    53                     entity.cw_edited['modification_date'] = datetime.now()
       
    54                 if not entity.upassword:
       
    55                     entity.cw_edited['upassword'] = u''
       
    56                 if not entity.cwuri:
       
    57                     entity.cw_edited['cwuri'] = '%s/?dn=%s' % (
       
    58                         source.urls[0], entity.cw_metainformation()['extid'])
       
    59                 print entity.cw_edited
       
    60                 system_source.add_entity(session, entity)
       
    61                 sql("UPDATE entities SET source='system' "
       
    62                     "WHERE eid=%(eid)s", {'eid': entity.eid})
       
    63                 continue
       
    64         todelete.setdefault(etype, []).append(entity)
       
    65 
       
    66 # only cleanup entities table, remaining stuff should be cleaned by a c-c
       
    67 # db-check to be run after this script
       
    68 for entities in todelete.values():
       
    69     system_source.delete_info_multi(session, entities, source_name)
       
    70 
       
    71 
       
    72 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")
       
    74 
       
    75 
       
    76 commit()