[7db8f3b] | 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 | |
---|
[7f2776e] | 16 | import collections |
---|
[7db8f3b] | 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', |
---|
[89165c1] | 28 | 'affiliation_basic', |
---|
| 29 | 'affiliation_detailed', |
---|
[7db8f3b] | 30 | ] |
---|
| 31 | |
---|
| 32 | def load_dcm(dcm_stream): |
---|
| 33 | dcm_people = {} |
---|
| 34 | for line in dcm_stream: |
---|
| 35 | line = line.strip() |
---|
| 36 | field_list = line.split("\t") |
---|
| 37 | field_dict = {} |
---|
| 38 | for index, field in enumerate(fields): |
---|
| 39 | field_dict[field] = field_list[index] |
---|
| 40 | dcm_people[field_dict['username']] = field_dict |
---|
| 41 | return dcm_people |
---|
| 42 | |
---|
| 43 | @transaction.commit_manually |
---|
| 44 | def load_people(dcm_people): |
---|
[d241a05] | 45 | django_people = groups.models.AthenaMoiraAccount.objects.all() |
---|
[7db8f3b] | 46 | stat_loops = 0 |
---|
| 47 | stat_django_people = len(django_people) |
---|
| 48 | stat_dcm_people = len(dcm_people) |
---|
| 49 | stat_changed = 0 |
---|
| 50 | stat_mut_ign = 0 |
---|
| 51 | stat_unchanged = 0 |
---|
| 52 | stat_del = 0 |
---|
| 53 | stat_pre_del = 0 |
---|
| 54 | stat_undel = 0 |
---|
| 55 | stat_add = 0 |
---|
[7f2776e] | 56 | stat_people = collections.defaultdict(list) |
---|
[7db8f3b] | 57 | for django_person in django_people: |
---|
| 58 | stat_loops += 1 |
---|
| 59 | if stat_loops % 100 == 0: |
---|
| 60 | transaction.commit() |
---|
| 61 | pass |
---|
| 62 | mutable = django_person.mutable |
---|
| 63 | if django_person.username in dcm_people: |
---|
| 64 | # great, they're still in the dump |
---|
| 65 | changed = False |
---|
[161ce5f] | 66 | changes = [] |
---|
[7db8f3b] | 67 | dcm_person = dcm_people[django_person.username] |
---|
| 68 | del dcm_people[django_person.username] |
---|
[161ce5f] | 69 | |
---|
| 70 | # Check for changes: first fields, then deletions |
---|
[7db8f3b] | 71 | for key in fields: |
---|
| 72 | if django_person.__dict__[key] != dcm_person[key]: |
---|
| 73 | changed = True |
---|
[161ce5f] | 74 | if key == 'mit_id': |
---|
| 75 | changes.append((key, '[redacted]', '[redacted]', )) |
---|
| 76 | else: |
---|
| 77 | changes.append((key, django_person.__dict__[key], dcm_person[key])) |
---|
[7db8f3b] | 78 | if mutable: |
---|
| 79 | django_person.__dict__[key] = dcm_person[key] |
---|
| 80 | if django_person.del_date is not None: |
---|
| 81 | changed = True |
---|
| 82 | if mutable: |
---|
| 83 | django_person.del_date = None |
---|
| 84 | stat_undel += 1 |
---|
[161ce5f] | 85 | changes.append(('[account]', '[deleted]', '[undeleted]', )) |
---|
| 86 | stat_people['undel'].append((django_person.username, changes)) |
---|
| 87 | |
---|
[7db8f3b] | 88 | if changed: |
---|
[161ce5f] | 89 | stat_name = '' |
---|
[7db8f3b] | 90 | if mutable: |
---|
| 91 | django_person.mod_date = datetime.date.today() |
---|
| 92 | django_person.save() |
---|
| 93 | stat_changed += 1 |
---|
[161ce5f] | 94 | stat_name = 'changed' |
---|
[7db8f3b] | 95 | else: |
---|
| 96 | stat_mut_ign += 1 |
---|
[161ce5f] | 97 | stat_name = 'mut_ign' |
---|
| 98 | stat_people[stat_name].append((django_person.username, changes)) |
---|
[7db8f3b] | 99 | else: |
---|
| 100 | stat_unchanged += 1 |
---|
[161ce5f] | 101 | |
---|
[7db8f3b] | 102 | else: |
---|
[161ce5f] | 103 | # They're not in the dump |
---|
[7db8f3b] | 104 | if django_person.del_date is None: |
---|
[161ce5f] | 105 | stat_name = '' |
---|
[7db8f3b] | 106 | if mutable: |
---|
| 107 | django_person.del_date = datetime.date.today() |
---|
| 108 | stat_del += 1 |
---|
[161ce5f] | 109 | stat_name = 'del' |
---|
[7db8f3b] | 110 | django_person.save() |
---|
| 111 | else: |
---|
| 112 | stat_mut_ign += 1 |
---|
[161ce5f] | 113 | stat_name = 'mut_ign' |
---|
| 114 | changes = [('account_class', django_person.account_class, '[deleted]')] |
---|
| 115 | stat_people[stat_name].append((django_person.username, changes)) |
---|
[7db8f3b] | 116 | else: |
---|
| 117 | stat_pre_del += 1 |
---|
[161ce5f] | 118 | |
---|
| 119 | transaction.commit() |
---|
| 120 | |
---|
| 121 | # Import new people from the DCM |
---|
[7db8f3b] | 122 | for username, dcm_person in dcm_people.items(): |
---|
| 123 | stat_loops += 1 |
---|
| 124 | if stat_loops % 100 == 0: |
---|
| 125 | transaction.commit() |
---|
| 126 | pass |
---|
[d241a05] | 127 | django_person = groups.models.AthenaMoiraAccount() |
---|
[7db8f3b] | 128 | for key in fields: |
---|
| 129 | django_person.__dict__[key] = dcm_person[key] |
---|
| 130 | django_person.add_date = datetime.date.today() |
---|
| 131 | stat_add += 1 |
---|
[161ce5f] | 132 | changes = [('account_class', '[missing]', dcm_person['account_class'], )] |
---|
| 133 | stat_people['add'].append((django_person.username, changes)) |
---|
[7db8f3b] | 134 | django_person.save() |
---|
| 135 | transaction.commit() |
---|
[161ce5f] | 136 | |
---|
[7db8f3b] | 137 | stats = { |
---|
| 138 | 'loops': stat_loops, |
---|
| 139 | 'django_people': stat_django_people, |
---|
| 140 | 'dcm_people': stat_dcm_people, |
---|
| 141 | 'changed': stat_changed, |
---|
| 142 | 'mut_ign': stat_mut_ign, |
---|
| 143 | 'unchanged': stat_unchanged, |
---|
| 144 | 'del': stat_del, |
---|
| 145 | 'pre_del': stat_pre_del, |
---|
| 146 | 'undel': stat_undel, |
---|
| 147 | 'add': stat_add, |
---|
| 148 | } |
---|
[7f2776e] | 149 | return stats, stat_people |
---|
[7db8f3b] | 150 | |
---|
| 151 | |
---|
| 152 | if __name__ == '__main__': |
---|
| 153 | print "Phase 1 (DCM parsing): starting at %s" % (datetime.datetime.now(), ) |
---|
| 154 | dcm_people = load_dcm(sys.stdin) |
---|
| 155 | print "Phase 1 (DCM parsing): complete at %s" % (datetime.datetime.now(), ) |
---|
| 156 | print "Phase 2 (Django updating): starting at %s" % (datetime.datetime.now(), ) |
---|
[7f2776e] | 157 | stats, stat_people = load_people(dcm_people) |
---|
[7db8f3b] | 158 | print "Phase 2 (Django updating): complete at %s" % (datetime.datetime.now(), ) |
---|
| 159 | print """ |
---|
| 160 | Loop iterations: %(loops)6d |
---|
| 161 | Initial in Django: %(django_people)6d |
---|
| 162 | People in DCM: %(dcm_people)6d |
---|
[db8a5bc] | 163 | Already Deleted: %(pre_del)6d |
---|
| 164 | Unchanged: %(unchanged)6d |
---|
[7db8f3b] | 165 | Changed: %(changed)6d |
---|
| 166 | Change ignored: %(mut_ign)6d |
---|
| 167 | Deleted: %(del)6d |
---|
| 168 | Undeleted: %(undel)6d |
---|
[2538f75] | 169 | Added: %(add)6d |
---|
| 170 | """ % stats |
---|
[7f2776e] | 171 | |
---|
| 172 | for change_type, people in stat_people.items(): |
---|
[161ce5f] | 173 | for person, changes in people: |
---|
| 174 | print "%12s\t%12s\t%s" % (change_type, person, changes, ) |
---|
[7f2776e] | 175 | print "" |
---|