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