misc/scripts/pyroforge2datafeed.py
branchstable
changeset 7917 436400e7f807
child 8008 9489dc9bc114
equal deleted inserted replaced
7916:54e240c1b419 7917:436400e7f807
       
     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 try:
       
    20     source.get_connection()._repo
       
    21 except AttributeError:
       
    22     print '%s is not reachable. Fix this before running this script' % source_name
       
    23     sys.exit(1)
       
    24 
       
    25 raw_input('Ensure you have shutdown all instances of this application before continuing.'
       
    26           ' Type enter when ready.')
       
    27 
       
    28 system_source = repo.system_source
       
    29 
       
    30 from base64 import b64encode
       
    31 from cubicweb.server.edition import EditedEntity
       
    32 
       
    33 DONT_GET_BACK_ETYPES = set(( # XXX edit as desired
       
    34         'State',
       
    35         'RecipeStep', 'RecipeStepInput', 'RecipeStepOutput',
       
    36         'RecipeTransition', 'RecipeTransitionCondition',
       
    37         'NarvalConditionExpression', 'Recipe',
       
    38         # XXX TestConfig
       
    39         ))
       
    40 
       
    41 
       
    42 session.mode = 'write' # hold on the connections set
       
    43 
       
    44 print '******************** backport entity content ***************************'
       
    45 
       
    46 from cubicweb.server import debugged
       
    47 todelete = {}
       
    48 for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities():
       
    49     with debugged('DBG_SQL'):
       
    50         etype = entity.__regid__
       
    51         if not source.support_entity(etype):
       
    52             print "source doesn't support %s, delete %s" % (etype, entity.eid)
       
    53         elif etype in DONT_GET_BACK_ETYPES:
       
    54             print 'ignore %s, delete %s' % (etype, entity.eid)
       
    55         else:
       
    56             try:
       
    57                 entity.complete()
       
    58             except:
       
    59                 print '%s %s much probably deleted, delete it (extid %s)' % (
       
    60                     etype, entity.eid, entity.cw_metainformation()['extid'])
       
    61             else:
       
    62                 print 'get back', etype, entity.eid
       
    63                 entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache)
       
    64                 system_source.add_entity(session, entity)
       
    65                 sql("UPDATE entities SET asource=%(asource)s, source='system', extid=%(extid)s "
       
    66                     "WHERE eid=%(eid)s", {'asource': source_name,
       
    67                                           'extid': b64encode(entity.cwuri),
       
    68                                           'eid': entity.eid})
       
    69                 continue
       
    70         todelete.setdefault(etype, []).append(entity)
       
    71 
       
    72 # only cleanup entities table, remaining stuff should be cleaned by a c-c
       
    73 # db-check to be run after this script
       
    74 for entities in todelete.values():
       
    75     system_source.delete_info_multi(session, entities, source_name)
       
    76 
       
    77 
       
    78 print '******************** backport mapping **********************************'
       
    79 session.disable_hook_categories('cw.sources')
       
    80 mapping = []
       
    81 for mappart in rql('Any X,SCH WHERE X cw_schema SCH, X cw_for_source S, S eid %(s)s',
       
    82                    {'s': source.eid}).entities():
       
    83     schemaent = mappart.cw_schema[0]
       
    84     if schemaent.__regid__ != 'CWEType':
       
    85         assert schemaent.__regid__ == 'CWRType'
       
    86         sch = schema._eid_index[schemaent.eid]
       
    87         for rdef in sch.rdefs.values():
       
    88             if not source.support_entity(rdef.subject) \
       
    89                     or not source.support_entity(rdef.object):
       
    90                 continue
       
    91             if rdef.subject in DONT_GET_BACK_ETYPES \
       
    92                     and rdef.object in DONT_GET_BACK_ETYPES:
       
    93                 print 'dont map', rdef
       
    94                 continue
       
    95             if rdef.subject in DONT_GET_BACK_ETYPES:
       
    96                 options = u'action=link\nlinkattr=name'
       
    97                 roles = 'object',
       
    98             elif rdef.object in DONT_GET_BACK_ETYPES:
       
    99                 options = u'action=link\nlinkattr=name'
       
   100                 roles = 'subject',
       
   101             else:
       
   102                 options = u'action=copy'
       
   103                 if rdef.rtype in ('use_environment',):
       
   104                     roles = 'object',
       
   105                 else:
       
   106                     roles = 'subject',
       
   107             print 'map', rdef, options, roles
       
   108             for role in roles:
       
   109                 mapping.append( (
       
   110                         (str(rdef.subject), str(rdef.rtype), str(rdef.object)),
       
   111                         options + '\nrole=%s' % role) )
       
   112     mappart.cw_delete()
       
   113 
       
   114 source_ent = rql('CWSource S WHERE S eid %(s)s', {'s': source.eid}).get_entity(0, 0)
       
   115 source_ent.init_mapping(mapping)
       
   116 
       
   117 # change source properties
       
   118 config = u'''synchronize=yes
       
   119 synchronization-interval=10min
       
   120 delete-entities=no
       
   121 '''
       
   122 rql('SET X type "datafeed", X parser "cw.entityxml", X url %(url)s, X config %(config)s '
       
   123     'WHERE X eid %(x)s',
       
   124     {'x': source.eid, 'config': config,
       
   125      'url': source.config['base-url']+'/project'})
       
   126 
       
   127 
       
   128 commit()
       
   129 
       
   130 from cubes.apycot import recipes
       
   131 recipes.create_quick_recipe(session)