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