"""turn a pyro source into a datafeed sourceOnce this script is run, execute c-c db-check to cleanup relation tables."""from__future__importprint_functionimportsysfromcollectionsimportdefaultdictfromlogilab.common.shellutilsimportgenerate_passwordtry:source_name,=__args__source=repo.sources_by_uri[source_name]exceptValueError:print('you should specify the source name as script argument (i.e. after --'' on the command line)')sys.exit(1)exceptKeyError:print('%s is not an active source'%source_name)sys.exit(1)# check source is reachable before doing anythingifnotsource.get_connection().cnx:print('%s is not reachable. Fix this before running this script'%source_name)sys.exit(1)raw_input('Ensure you have shutdown all instances of this application before continuing.'' Type enter when ready.')system_source=repo.system_sourcefromdatetimeimportdatetimefromcubicweb.server.editionimportEditedEntityprint('******************** backport entity content ***************************')todelete=defaultdict(list)extids=set()duplicates=[]forentityinrql('Any X WHERE X cw_source S, S eid %(s)s',{'s':source.eid}).entities():etype=entity.cw_etypeifnotsource.support_entity(etype):print("source doesn't support %s, delete %s"%(etype,entity.eid))todelete[etype].append(entity)continuetry:entity.complete()exceptException:print('%s%s much probably deleted, delete it (extid %s)'%(etype,entity.eid,entity.cw_metainformation()['extid']))todelete[etype].append(entity)continueprint('get back',etype,entity.eid)entity.cw_edited=EditedEntity(entity,**entity.cw_attr_cache)ifnotentity.creation_date:entity.cw_edited['creation_date']=datetime.now()ifnotentity.modification_date:entity.cw_edited['modification_date']=datetime.now()ifnotentity.upassword:entity.cw_edited['upassword']=generate_password()extid=entity.cw_metainformation()['extid']ifnotentity.cwuri:entity.cw_edited['cwuri']='%s/?dn=%s'%(source.urls[0],extid.decode('utf-8','ignore'))print(entity.cw_edited)ifextidinextids:duplicates.append(extid)continueextids.add(extid)system_source.add_entity(session,entity)sql("UPDATE entities SET source='system' ""WHERE eid=%(eid)s",{'eid':entity.eid})# only cleanup entities table, remaining stuff should be cleaned by a c-c# db-check to be run after this scriptifduplicates:print('found %s duplicate entries'%len(duplicates))frompprintimportpprintpprint(duplicates)print(len(todelete),'entities will be deleted')foretype,entitiesintodelete.items():print('deleting',etype,[e.loginforeinentities])system_source.delete_info_multi(session,entities,source_name)source_ent=rql('CWSource S WHERE S eid %(s)s',{'s':source.eid}).get_entity(0,0)source_ent.cw_set(type=u"ldapfeed",parser=u"ldapfeed")ifraw_input('Commit?')in'yY':print('committing')commit()else:rollback()print('rolled back')