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 from collections import defaultdict |
8 from collections import defaultdict |
7 from logilab.common.shellutils import generate_password |
9 from logilab.common.shellutils import generate_password |
8 |
10 |
9 try: |
11 try: |
12 except ValueError: |
14 except ValueError: |
13 print('you should specify the source name as script argument (i.e. after --' |
15 print('you should specify the source name as script argument (i.e. after --' |
14 ' on the command line)') |
16 ' on the command line)') |
15 sys.exit(1) |
17 sys.exit(1) |
16 except KeyError: |
18 except KeyError: |
17 print '%s is not an active source' % source_name |
19 print('%s is not an active source' % source_name) |
18 sys.exit(1) |
20 sys.exit(1) |
19 |
21 |
20 # check source is reachable before doing anything |
22 # check source is reachable before doing anything |
21 if not source.get_connection().cnx: |
23 if not source.get_connection().cnx: |
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 |
29 |
31 |
30 from datetime import datetime |
32 from datetime import datetime |
31 from cubicweb.server.edition import EditedEntity |
33 from cubicweb.server.edition import EditedEntity |
32 |
34 |
33 |
35 |
34 print '******************** backport entity content ***************************' |
36 print('******************** backport entity content ***************************') |
35 |
37 |
36 todelete = defaultdict(list) |
38 todelete = defaultdict(list) |
37 extids = set() |
39 extids = set() |
38 duplicates = [] |
40 duplicates = [] |
39 for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities(): |
41 for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities(): |
40 etype = entity.cw_etype |
42 etype = entity.cw_etype |
41 if not source.support_entity(etype): |
43 if not source.support_entity(etype): |
42 print "source doesn't support %s, delete %s" % (etype, entity.eid) |
44 print("source doesn't support %s, delete %s" % (etype, entity.eid)) |
43 todelete[etype].append(entity) |
45 todelete[etype].append(entity) |
44 continue |
46 continue |
45 try: |
47 try: |
46 entity.complete() |
48 entity.complete() |
47 except Exception: |
49 except Exception: |
48 print '%s %s much probably deleted, delete it (extid %s)' % ( |
50 print('%s %s much probably deleted, delete it (extid %s)' % ( |
49 etype, entity.eid, entity.cw_metainformation()['extid']) |
51 etype, entity.eid, entity.cw_metainformation()['extid'])) |
50 todelete[etype].append(entity) |
52 todelete[etype].append(entity) |
51 continue |
53 continue |
52 print 'get back', etype, entity.eid |
54 print('get back', etype, entity.eid) |
53 entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache) |
55 entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache) |
54 if not entity.creation_date: |
56 if not entity.creation_date: |
55 entity.cw_edited['creation_date'] = datetime.now() |
57 entity.cw_edited['creation_date'] = datetime.now() |
56 if not entity.modification_date: |
58 if not entity.modification_date: |
57 entity.cw_edited['modification_date'] = datetime.now() |
59 entity.cw_edited['modification_date'] = datetime.now() |
59 entity.cw_edited['upassword'] = generate_password() |
61 entity.cw_edited['upassword'] = generate_password() |
60 extid = entity.cw_metainformation()['extid'] |
62 extid = entity.cw_metainformation()['extid'] |
61 if not entity.cwuri: |
63 if not entity.cwuri: |
62 entity.cw_edited['cwuri'] = '%s/?dn=%s' % ( |
64 entity.cw_edited['cwuri'] = '%s/?dn=%s' % ( |
63 source.urls[0], extid.decode('utf-8', 'ignore')) |
65 source.urls[0], extid.decode('utf-8', 'ignore')) |
64 print entity.cw_edited |
66 print(entity.cw_edited) |
65 if extid in extids: |
67 if extid in extids: |
66 duplicates.append(extid) |
68 duplicates.append(extid) |
67 continue |
69 continue |
68 extids.add(extid) |
70 extids.add(extid) |
69 system_source.add_entity(session, entity) |
71 system_source.add_entity(session, entity) |
71 "WHERE eid=%(eid)s", {'eid': entity.eid}) |
73 "WHERE eid=%(eid)s", {'eid': entity.eid}) |
72 |
74 |
73 # only cleanup entities table, remaining stuff should be cleaned by a c-c |
75 # only cleanup entities table, remaining stuff should be cleaned by a c-c |
74 # db-check to be run after this script |
76 # db-check to be run after this script |
75 if duplicates: |
77 if duplicates: |
76 print 'found %s duplicate entries' % len(duplicates) |
78 print('found %s duplicate entries' % len(duplicates)) |
77 from pprint import pprint |
79 from pprint import pprint |
78 pprint(duplicates) |
80 pprint(duplicates) |
79 |
81 |
80 print len(todelete), 'entities will be deleted' |
82 print(len(todelete), 'entities will be deleted') |
81 for etype, entities in todelete.iteritems(): |
83 for etype, entities in todelete.iteritems(): |
82 print 'deleting', etype, [e.login for e in entities] |
84 print('deleting', etype, [e.login for e in entities]) |
83 system_source.delete_info_multi(session, entities, source_name) |
85 system_source.delete_info_multi(session, entities, source_name) |
84 |
86 |
85 |
87 |
86 |
88 |
87 source_ent = rql('CWSource S WHERE S eid %(s)s', {'s': source.eid}).get_entity(0, 0) |
89 source_ent = rql('CWSource S WHERE S eid %(s)s', {'s': source.eid}).get_entity(0, 0) |
88 source_ent.cw_set(type=u"ldapfeed", parser=u"ldapfeed") |
90 source_ent.cw_set(type=u"ldapfeed", parser=u"ldapfeed") |
89 |
91 |
90 |
92 |
91 if raw_input('Commit?') in 'yY': |
93 if raw_input('Commit?') in 'yY': |
92 print 'committing' |
94 print('committing') |
93 commit() |
95 commit() |
94 else: |
96 else: |
95 rollback() |
97 rollback() |
96 print 'rolled back' |
98 print('rolled back') |
97 |
99 |