sync revisited
[myplc.git] / plc-orphan-accounts.py
1 #!/usr/bin/env plcsh
2
3 # searches and displays any local orphan account (not attached to a site)
4 # remote accounts with identical emails are displayed as well
5
6 import time
7
8 # sort filters look broken
9 def sort_email (p1,p2):
10     if p1['email'] == p2['email']: return 0
11     if p1['email'] < p2['email'] : return -1
12     return 1
13
14 def get_orphans ():
15     orphans = [p for p in GetPersons({'peer_id':None,'-SORT':'email'}) if not p['site_ids'] ]
16     orphans.sort(sort_email)
17     return orphans
18
19 def list_person (margin,p):
20     print margin,'%6d'%p['person_id'], time.asctime(time.gmtime(p['date_created'])),
21     if not p['peer_id']: print 'LOCAL',
22     else: print 'pr=',p['peer_id'],
23     if p['enabled']: print 'ENB',
24     else: print 'DIS',
25     print p['email']
26
27 def get_related(email):
28     return GetPersons ({'email':email,'~peer_id':None})
29
30 def header (message):
31     print '--------------------'
32     print GetPeerName(),
33     print time.asctime(time.gmtime())
34     print 'Listing orphan accounts and any similar remote'
35     print '--------------------'
36
37 def main_orphans ():
38     orphans = get_orphans()
39     header ('Listing  %d  local accounts with no site - and similar remote accounts'%len(orphans))
40     index=1
41     for p in orphans:
42         list_person ("%3d"%index,p)
43         for related in get_related(p['email']):
44             list_person("dup",related)
45         index+=1
46     
47 def main_duplicates():
48
49     header ('Listing all duplicate accounts')
50     locals = GetPersons({'peer_id':None,'-SORT':'email'})
51     locals.sort(sort_email)
52     index=1
53     for local in locals:
54         remotes=GetPersons({'email':local['email'],'~peer_id':None})
55         if remotes:
56             list_person('%3d'%index,local)
57             for remote in remotes:
58                 list_person('dup',remote)
59         
60             index+=1
61 def main():
62     main_orphans()
63     main_duplicates()
64
65 if __name__ == '__main__':
66     main()