Commit 305612ce0b26292dd36032fe3f08d483321c7fe7
1 parent
9eecad27
Exists in
master
and in
1 other branch
- simplified several things in initdb
Showing
1 changed file
with
13 additions
and
29 deletions
Show diff stats
initdb.py
| ... | ... | @@ -5,9 +5,7 @@ import csv |
| 5 | 5 | import argparse |
| 6 | 6 | import re |
| 7 | 7 | import string |
| 8 | -from sys import exit | |
| 9 | 8 | from concurrent.futures import ThreadPoolExecutor |
| 10 | -import asyncio | |
| 11 | 9 | |
| 12 | 10 | # installed packages |
| 13 | 11 | import bcrypt |
| ... | ... | @@ -17,27 +15,6 @@ import sqlalchemy as sa |
| 17 | 15 | from models import Base, Student |
| 18 | 16 | |
| 19 | 17 | |
| 20 | -# replace password by hash for a single student | |
| 21 | -def hashpw(student): | |
| 22 | - pw = student.get('pw', student['uid']).encode('utf-8') | |
| 23 | - print('.', end='', flush=True) | |
| 24 | - hashed_pw = bcrypt.hashpw(pw, bcrypt.gensalt()) | |
| 25 | - student['pw'] = hashed_pw | |
| 26 | - | |
| 27 | - | |
| 28 | -async def hash_all_passwords(executor, students): | |
| 29 | - loop = asyncio.get_event_loop() | |
| 30 | - tasks = [loop.run_in_executor(executor, hashpw, s) for s in students] | |
| 31 | - await asyncio.wait(tasks) # block until all tasks are done | |
| 32 | - print() | |
| 33 | - | |
| 34 | -# =========================================================================== | |
| 35 | -# SIIUE names have alien strings like "(TE)" and are sometimes capitalized | |
| 36 | -# We remove them so that students dont keep asking what it means | |
| 37 | -def fix(name): | |
| 38 | - return string.capwords(re.sub('\(.*\)', '', name).strip()) | |
| 39 | - | |
| 40 | - | |
| 41 | 18 | # =========================================================================== |
| 42 | 19 | # Parse command line options |
| 43 | 20 | def parse_commandline_arguments(): |
| ... | ... | @@ -85,6 +62,8 @@ def parse_commandline_arguments(): |
| 85 | 62 | |
| 86 | 63 | |
| 87 | 64 | # =========================================================================== |
| 65 | +# SIIUE names have alien strings like "(TE)" and are sometimes capitalized | |
| 66 | +# We remove them so that students dont keep asking what it means | |
| 88 | 67 | def get_students_from_csv(filename): |
| 89 | 68 | try: |
| 90 | 69 | csvreader = csv.DictReader(open(filename, encoding='iso-8859-1'), delimiter=';', quotechar='"', skipinitialspace=True) |
| ... | ... | @@ -94,13 +73,21 @@ def get_students_from_csv(filename): |
| 94 | 73 | else: |
| 95 | 74 | students = [{ |
| 96 | 75 | 'uid': s['N.º'], |
| 97 | - 'name': fix(s['Nome']) | |
| 76 | + 'name': string.capwords(re.sub('\(.*\)', '', s['Nome']).strip()) | |
| 98 | 77 | } for s in csvreader] |
| 99 | 78 | |
| 100 | 79 | return students |
| 101 | 80 | |
| 102 | 81 | |
| 103 | 82 | # =========================================================================== |
| 83 | +# replace password by hash for a single student | |
| 84 | +def hashpw(student): | |
| 85 | + print('.', end='', flush=True) | |
| 86 | + pw = student.get('pw', student['uid']).encode('utf-8') | |
| 87 | + student['pw'] = bcrypt.hashpw(pw, bcrypt.gensalt()) | |
| 88 | + | |
| 89 | + | |
| 90 | +# =========================================================================== | |
| 104 | 91 | def insert_students_into_db(session, students): |
| 105 | 92 | try: |
| 106 | 93 | # --- start db session --- |
| ... | ... | @@ -167,10 +154,8 @@ if __name__=='__main__': |
| 167 | 154 | for s in students: |
| 168 | 155 | s['pw'] = args.pw |
| 169 | 156 | |
| 170 | - executor = ThreadPoolExecutor() | |
| 171 | - event_loop = asyncio.get_event_loop() | |
| 172 | - event_loop.run_until_complete(hash_all_passwords(executor, students)) | |
| 173 | - event_loop.close() | |
| 157 | + with ThreadPoolExecutor() as executor: | |
| 158 | + executor.map(hashpw, students) | |
| 174 | 159 | |
| 175 | 160 | # --- database stuff |
| 176 | 161 | print(f'Using database: ', args.db) |
| ... | ... | @@ -183,7 +168,6 @@ if __name__=='__main__': |
| 183 | 168 | print(f'Inserting {len(students)}') |
| 184 | 169 | insert_students_into_db(session, students) |
| 185 | 170 | |
| 186 | - # print(args.update) | |
| 187 | 171 | for s in args.update: |
| 188 | 172 | print(f'Updating password of: {s}') |
| 189 | 173 | u = session.query(Student).get(s) | ... | ... |