[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', |
---|
| 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): |
---|
[d241a05] | 43 | django_people = groups.models.AthenaMoiraAccount.objects.all() |
---|
[7db8f3b] | 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 |
---|
[7f2776e] | 54 | stat_people = collections.defaultdict(list) |
---|
[7db8f3b] | 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 |
---|
[7f2776e] | 76 | stat_people['undel'].append(django_person.username) |
---|
[7db8f3b] | 77 | if changed: |
---|
| 78 | if mutable: |
---|
| 79 | django_person.mod_date = datetime.date.today() |
---|
| 80 | django_person.save() |
---|
| 81 | stat_changed += 1 |
---|
[7f2776e] | 82 | stat_people['changed'].append(django_person.username) |
---|
[7db8f3b] | 83 | else: |
---|
| 84 | stat_mut_ign += 1 |
---|
[7f2776e] | 85 | stat_people['mut_ign'].append(django_person.username) |
---|
[7db8f3b] | 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 |
---|
[7f2776e] | 93 | stat_people['del'].append(django_person.username) |
---|
[7db8f3b] | 94 | django_person.save() |
---|
| 95 | else: |
---|
| 96 | stat_mut_ign += 1 |
---|
[7f2776e] | 97 | stat_people['mut_ign'].append(django_person.username) |
---|
[7db8f3b] | 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 |
---|
[d241a05] | 105 | django_person = groups.models.AthenaMoiraAccount() |
---|
[7db8f3b] | 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 |
---|
[7f2776e] | 110 | stat_people['add'].append(django_person.username) |
---|
[7db8f3b] | 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 | } |
---|
[7f2776e] | 125 | return stats, stat_people |
---|
[7db8f3b] | 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(), ) |
---|
[7f2776e] | 133 | stats, stat_people = load_people(dcm_people) |
---|
[7db8f3b] | 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 |
---|
[db8a5bc] | 139 | Already Deleted: %(pre_del)6d |
---|
| 140 | Unchanged: %(unchanged)6d |
---|
[7db8f3b] | 141 | Changed: %(changed)6d |
---|
| 142 | Change ignored: %(mut_ign)6d |
---|
| 143 | Deleted: %(del)6d |
---|
| 144 | Undeleted: %(undel)6d |
---|
[2538f75] | 145 | Added: %(add)6d |
---|
| 146 | """ % stats |
---|
[7f2776e] | 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 "" |
---|