misc/scripts/pyroforge2datafeed.py
changeset 10589 7c23b7de2b8d
parent 10366 38c7598b5c61
child 10663 54b8a1f249fb
equal deleted inserted replaced
10588:fdaa0e4b7eaf 10589:7c23b7de2b8d
     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 from __future__ import print_function
       
     6 
     5 import sys
     7 import sys
     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:
    11     print('you should specify the source name as script argument (i.e. after --'
    13     print('you should specify the source name as script argument (i.e. after --'
    12           ' on the command line)')
    14           ' on the command line)')
    13     sys.exit(1)
    15     sys.exit(1)
    14 except KeyError:
    16 except KeyError:
    15     print '%s is not an active source' % source_name
    17     print('%s is not an active source' % source_name)
    16     sys.exit(1)
    18     sys.exit(1)
    17 
    19 
    18 # check source is reachable before doing anything
    20 # check source is reachable before doing anything
    19 try:
    21 try:
    20     source.get_connection()._repo
    22     source.get_connection()._repo
    21 except AttributeError:
    23 except AttributeError:
    22     print '%s is not reachable. Fix this before running this script' % source_name
    24     print('%s is not reachable. Fix this before running this script' % source_name)
    23     sys.exit(1)
    25     sys.exit(1)
    24 
    26 
    25 raw_input('Ensure you have shutdown all instances of this application before continuing.'
    27 raw_input('Ensure you have shutdown all instances of this application before continuing.'
    26           ' Type enter when ready.')
    28           ' Type enter when ready.')
    27 
    29 
    37         'NarvalConditionExpression', 'Recipe',
    39         'NarvalConditionExpression', 'Recipe',
    38         # XXX TestConfig
    40         # XXX TestConfig
    39         ))
    41         ))
    40 
    42 
    41 
    43 
    42 print '******************** backport entity content ***************************'
    44 print('******************** backport entity content ***************************')
    43 
    45 
    44 from cubicweb.server import debugged
    46 from cubicweb.server import debugged
    45 todelete = {}
    47 todelete = {}
    46 host = source.config['base-url'].split('://')[1]
    48 host = source.config['base-url'].split('://')[1]
    47 for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities():
    49 for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities():
    48         etype = entity.cw_etype
    50         etype = entity.cw_etype
    49         if not source.support_entity(etype):
    51         if not source.support_entity(etype):
    50             print "source doesn't support %s, delete %s" % (etype, entity.eid)
    52             print("source doesn't support %s, delete %s" % (etype, entity.eid))
    51         elif etype in DONT_GET_BACK_ETYPES:
    53         elif etype in DONT_GET_BACK_ETYPES:
    52             print 'ignore %s, delete %s' % (etype, entity.eid)
    54             print('ignore %s, delete %s' % (etype, entity.eid))
    53         else:
    55         else:
    54             try:
    56             try:
    55                 entity.complete()
    57                 entity.complete()
    56                 if not host in entity.cwuri:
    58                 if not host in entity.cwuri:
    57                     print 'SKIP foreign entity', entity.cwuri, source.config['base-url']
    59                     print('SKIP foreign entity', entity.cwuri, source.config['base-url'])
    58                     continue
    60                     continue
    59             except Exception:
    61             except Exception:
    60                 print '%s %s much probably deleted, delete it (extid %s)' % (
    62                 print('%s %s much probably deleted, delete it (extid %s)' % (
    61                     etype, entity.eid, entity.cw_metainformation()['extid'])
    63                     etype, entity.eid, entity.cw_metainformation()['extid']))
    62             else:
    64             else:
    63                 print 'get back', etype, entity.eid
    65                 print('get back', etype, entity.eid)
    64                 entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache)
    66                 entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache)
    65                 system_source.add_entity(session, entity)
    67                 system_source.add_entity(session, entity)
    66                 sql("UPDATE entities SET asource=%(asource)s, source='system', extid=%(extid)s "
    68                 sql("UPDATE entities SET asource=%(asource)s, source='system', extid=%(extid)s "
    67                     "WHERE eid=%(eid)s", {'asource': source_name,
    69                     "WHERE eid=%(eid)s", {'asource': source_name,
    68                                           'extid': b64encode(entity.cwuri),
    70                                           'extid': b64encode(entity.cwuri),
    74 # db-check to be run after this script
    76 # db-check to be run after this script
    75 for entities in todelete.itervalues():
    77 for entities in todelete.itervalues():
    76     system_source.delete_info_multi(session, entities, source_name)
    78     system_source.delete_info_multi(session, entities, source_name)
    77 
    79 
    78 
    80 
    79 print '******************** backport mapping **********************************'
    81 print('******************** backport mapping **********************************')
    80 session.disable_hook_categories('cw.sources')
    82 session.disable_hook_categories('cw.sources')
    81 mapping = []
    83 mapping = []
    82 for mappart in rql('Any X,SCH WHERE X cw_schema SCH, X cw_for_source S, S eid %(s)s',
    84 for mappart in rql('Any X,SCH WHERE X cw_schema SCH, X cw_for_source S, S eid %(s)s',
    83                    {'s': source.eid}).entities():
    85                    {'s': source.eid}).entities():
    84     schemaent = mappart.cw_schema[0]
    86     schemaent = mappart.cw_schema[0]
    89             if not source.support_entity(rdef.subject) \
    91             if not source.support_entity(rdef.subject) \
    90                     or not source.support_entity(rdef.object):
    92                     or not source.support_entity(rdef.object):
    91                 continue
    93                 continue
    92             if rdef.subject in DONT_GET_BACK_ETYPES \
    94             if rdef.subject in DONT_GET_BACK_ETYPES \
    93                     and rdef.object in DONT_GET_BACK_ETYPES:
    95                     and rdef.object in DONT_GET_BACK_ETYPES:
    94                 print 'dont map', rdef
    96                 print('dont map', rdef)
    95                 continue
    97                 continue
    96             if rdef.subject in DONT_GET_BACK_ETYPES:
    98             if rdef.subject in DONT_GET_BACK_ETYPES:
    97                 options = u'action=link\nlinkattr=name'
    99                 options = u'action=link\nlinkattr=name'
    98                 roles = 'object',
   100                 roles = 'object',
    99             elif rdef.object in DONT_GET_BACK_ETYPES:
   101             elif rdef.object in DONT_GET_BACK_ETYPES:
   103                 options = u'action=copy'
   105                 options = u'action=copy'
   104                 if rdef.rtype in ('use_environment',):
   106                 if rdef.rtype in ('use_environment',):
   105                     roles = 'object',
   107                     roles = 'object',
   106                 else:
   108                 else:
   107                     roles = 'subject',
   109                     roles = 'subject',
   108             print 'map', rdef, options, roles
   110             print('map', rdef, options, roles)
   109             for role in roles:
   111             for role in roles:
   110                 mapping.append( (
   112                 mapping.append( (
   111                         (str(rdef.subject), str(rdef.rtype), str(rdef.object)),
   113                         (str(rdef.subject), str(rdef.rtype), str(rdef.object)),
   112                         options + '\nrole=%s' % role) )
   114                         options + '\nrole=%s' % role) )
   113     mappart.cw_delete()
   115     mappart.cw_delete()