diff --git a/initdb.py b/initdb.py index 91bfbc9..50424d6 100755 --- a/initdb.py +++ b/initdb.py @@ -5,9 +5,7 @@ import csv import argparse import re import string -from sys import exit from concurrent.futures import ThreadPoolExecutor -import asyncio # installed packages import bcrypt @@ -17,27 +15,6 @@ import sqlalchemy as sa from models import Base, Student -# replace password by hash for a single student -def hashpw(student): - pw = student.get('pw', student['uid']).encode('utf-8') - print('.', end='', flush=True) - hashed_pw = bcrypt.hashpw(pw, bcrypt.gensalt()) - student['pw'] = hashed_pw - - -async def hash_all_passwords(executor, students): - loop = asyncio.get_event_loop() - tasks = [loop.run_in_executor(executor, hashpw, s) for s in students] - await asyncio.wait(tasks) # block until all tasks are done - print() - -# =========================================================================== -# SIIUE names have alien strings like "(TE)" and are sometimes capitalized -# We remove them so that students dont keep asking what it means -def fix(name): - return string.capwords(re.sub('\(.*\)', '', name).strip()) - - # =========================================================================== # Parse command line options def parse_commandline_arguments(): @@ -85,6 +62,8 @@ def parse_commandline_arguments(): # =========================================================================== +# SIIUE names have alien strings like "(TE)" and are sometimes capitalized +# We remove them so that students dont keep asking what it means def get_students_from_csv(filename): try: csvreader = csv.DictReader(open(filename, encoding='iso-8859-1'), delimiter=';', quotechar='"', skipinitialspace=True) @@ -94,13 +73,21 @@ def get_students_from_csv(filename): else: students = [{ 'uid': s['N.ยบ'], - 'name': fix(s['Nome']) + 'name': string.capwords(re.sub('\(.*\)', '', s['Nome']).strip()) } for s in csvreader] return students # =========================================================================== +# replace password by hash for a single student +def hashpw(student): + print('.', end='', flush=True) + pw = student.get('pw', student['uid']).encode('utf-8') + student['pw'] = bcrypt.hashpw(pw, bcrypt.gensalt()) + + +# =========================================================================== def insert_students_into_db(session, students): try: # --- start db session --- @@ -167,10 +154,8 @@ if __name__=='__main__': for s in students: s['pw'] = args.pw - executor = ThreadPoolExecutor() - event_loop = asyncio.get_event_loop() - event_loop.run_until_complete(hash_all_passwords(executor, students)) - event_loop.close() + with ThreadPoolExecutor() as executor: + executor.map(hashpw, students) # --- database stuff print(f'Using database: ', args.db) @@ -183,7 +168,6 @@ if __name__=='__main__': print(f'Inserting {len(students)}') insert_students_into_db(session, students) - # print(args.update) for s in args.update: print(f'Updating password of: {s}') u = session.query(Student).get(s) -- libgit2 0.21.2