1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | |
---|
6 | if __name__ == '__main__': |
---|
7 | cur_file = os.path.abspath(__file__) |
---|
8 | django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..')) |
---|
9 | proj_dir = os.path.abspath(os.path.join(django_dir, '..')) |
---|
10 | sys.path.append(django_dir) |
---|
11 | sys.path.append(proj_dir) |
---|
12 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
---|
13 | |
---|
14 | import groups.models |
---|
15 | |
---|
16 | import collections |
---|
17 | import datetime |
---|
18 | |
---|
19 | from django.db import transaction |
---|
20 | |
---|
21 | fields = [ |
---|
22 | # Django field, in order matching the input fields |
---|
23 | 'username', |
---|
24 | 'mit_id', |
---|
25 | 'first_name', |
---|
26 | 'last_name', |
---|
27 | 'account_class', |
---|
28 | ] |
---|
29 | |
---|
30 | def load_dcm(dcm_stream): |
---|
31 | dcm_people = {} |
---|
32 | for line in dcm_stream: |
---|
33 | line = line.strip() |
---|
34 | field_list = line.split("\t") |
---|
35 | field_dict = {} |
---|
36 | for index, field in enumerate(fields): |
---|
37 | field_dict[field] = field_list[index] |
---|
38 | dcm_people[field_dict['username']] = field_dict |
---|
39 | return dcm_people |
---|
40 | |
---|
41 | @transaction.commit_manually |
---|
42 | def load_people(dcm_people): |
---|
43 | django_people = groups.models.AthenaMoiraAccount.objects.all() |
---|
44 | stat_loops = 0 |
---|
45 | stat_django_people = len(django_people) |
---|
46 | stat_dcm_people = len(dcm_people) |
---|
47 | stat_changed = 0 |
---|
48 | stat_mut_ign = 0 |
---|
49 | stat_unchanged = 0 |
---|
50 | stat_del = 0 |
---|
51 | stat_pre_del = 0 |
---|
52 | stat_undel = 0 |
---|
53 | stat_add = 0 |
---|
54 | stat_people = collections.defaultdict(list) |
---|
55 | for django_person in django_people: |
---|
56 | stat_loops += 1 |
---|
57 | if stat_loops % 100 == 0: |
---|
58 | transaction.commit() |
---|
59 | pass |
---|
60 | mutable = django_person.mutable |
---|
61 | if django_person.username in dcm_people: |
---|
62 | # great, they're still in the dump |
---|
63 | changed = False |
---|
64 | dcm_person = dcm_people[django_person.username] |
---|
65 | del dcm_people[django_person.username] |
---|
66 | for key in fields: |
---|
67 | if django_person.__dict__[key] != dcm_person[key]: |
---|
68 | changed = True |
---|
69 | if mutable: |
---|
70 | django_person.__dict__[key] = dcm_person[key] |
---|
71 | if django_person.del_date is not None: |
---|
72 | changed = True |
---|
73 | if mutable: |
---|
74 | django_person.del_date = None |
---|
75 | stat_undel += 1 |
---|
76 | stat_people['undel'].append(django_person.username) |
---|
77 | if changed: |
---|
78 | if mutable: |
---|
79 | django_person.mod_date = datetime.date.today() |
---|
80 | django_person.save() |
---|
81 | stat_changed += 1 |
---|
82 | stat_people['changed'].append(django_person.username) |
---|
83 | else: |
---|
84 | stat_mut_ign += 1 |
---|
85 | stat_people['mut_ign'].append(django_person.username) |
---|
86 | else: |
---|
87 | stat_unchanged += 1 |
---|
88 | else: |
---|
89 | if django_person.del_date is None: |
---|
90 | if mutable: |
---|
91 | django_person.del_date = datetime.date.today() |
---|
92 | stat_del += 1 |
---|
93 | stat_people['del'].append(django_person.username) |
---|
94 | django_person.save() |
---|
95 | else: |
---|
96 | stat_mut_ign += 1 |
---|
97 | stat_people['mut_ign'].append(django_person.username) |
---|
98 | else: |
---|
99 | stat_pre_del += 1 |
---|
100 | for username, dcm_person in dcm_people.items(): |
---|
101 | stat_loops += 1 |
---|
102 | if stat_loops % 100 == 0: |
---|
103 | transaction.commit() |
---|
104 | pass |
---|
105 | django_person = groups.models.AthenaMoiraAccount() |
---|
106 | for key in fields: |
---|
107 | django_person.__dict__[key] = dcm_person[key] |
---|
108 | django_person.add_date = datetime.date.today() |
---|
109 | stat_add += 1 |
---|
110 | stat_people['add'].append(django_person.username) |
---|
111 | django_person.save() |
---|
112 | transaction.commit() |
---|
113 | stats = { |
---|
114 | 'loops': stat_loops, |
---|
115 | 'django_people': stat_django_people, |
---|
116 | 'dcm_people': stat_dcm_people, |
---|
117 | 'changed': stat_changed, |
---|
118 | 'mut_ign': stat_mut_ign, |
---|
119 | 'unchanged': stat_unchanged, |
---|
120 | 'del': stat_del, |
---|
121 | 'pre_del': stat_pre_del, |
---|
122 | 'undel': stat_undel, |
---|
123 | 'add': stat_add, |
---|
124 | } |
---|
125 | return stats, stat_people |
---|
126 | |
---|
127 | |
---|
128 | if __name__ == '__main__': |
---|
129 | print "Phase 1 (DCM parsing): starting at %s" % (datetime.datetime.now(), ) |
---|
130 | dcm_people = load_dcm(sys.stdin) |
---|
131 | print "Phase 1 (DCM parsing): complete at %s" % (datetime.datetime.now(), ) |
---|
132 | print "Phase 2 (Django updating): starting at %s" % (datetime.datetime.now(), ) |
---|
133 | stats, stat_people = load_people(dcm_people) |
---|
134 | print "Phase 2 (Django updating): complete at %s" % (datetime.datetime.now(), ) |
---|
135 | print """ |
---|
136 | Loop iterations: %(loops)6d |
---|
137 | Initial in Django: %(django_people)6d |
---|
138 | People in DCM: %(dcm_people)6d |
---|
139 | Already Deleted: %(pre_del)6d |
---|
140 | Unchanged: %(unchanged)6d |
---|
141 | Changed: %(changed)6d |
---|
142 | Change ignored: %(mut_ign)6d |
---|
143 | Deleted: %(del)6d |
---|
144 | Undeleted: %(undel)6d |
---|
145 | Added: %(add)6d |
---|
146 | """ % stats |
---|
147 | |
---|
148 | for change_type, people in stat_people.items(): |
---|
149 | for person in people: |
---|
150 | print "%12s\t%s" % (change_type, person, ) |
---|
151 | print "" |
---|