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,9 +5,7 @@ import csv | ||
5 | import argparse | 5 | import argparse |
6 | import re | 6 | import re |
7 | import string | 7 | import string |
8 | -from sys import exit | ||
9 | from concurrent.futures import ThreadPoolExecutor | 8 | from concurrent.futures import ThreadPoolExecutor |
10 | -import asyncio | ||
11 | 9 | ||
12 | # installed packages | 10 | # installed packages |
13 | import bcrypt | 11 | import bcrypt |
@@ -17,27 +15,6 @@ import sqlalchemy as sa | @@ -17,27 +15,6 @@ import sqlalchemy as sa | ||
17 | from models import Base, Student | 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 | # Parse command line options | 19 | # Parse command line options |
43 | def parse_commandline_arguments(): | 20 | def parse_commandline_arguments(): |
@@ -85,6 +62,8 @@ 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 | def get_students_from_csv(filename): | 67 | def get_students_from_csv(filename): |
89 | try: | 68 | try: |
90 | csvreader = csv.DictReader(open(filename, encoding='iso-8859-1'), delimiter=';', quotechar='"', skipinitialspace=True) | 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,13 +73,21 @@ def get_students_from_csv(filename): | ||
94 | else: | 73 | else: |
95 | students = [{ | 74 | students = [{ |
96 | 'uid': s['N.º'], | 75 | 'uid': s['N.º'], |
97 | - 'name': fix(s['Nome']) | 76 | + 'name': string.capwords(re.sub('\(.*\)', '', s['Nome']).strip()) |
98 | } for s in csvreader] | 77 | } for s in csvreader] |
99 | 78 | ||
100 | return students | 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 | def insert_students_into_db(session, students): | 91 | def insert_students_into_db(session, students): |
105 | try: | 92 | try: |
106 | # --- start db session --- | 93 | # --- start db session --- |
@@ -167,10 +154,8 @@ if __name__=='__main__': | @@ -167,10 +154,8 @@ if __name__=='__main__': | ||
167 | for s in students: | 154 | for s in students: |
168 | s['pw'] = args.pw | 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 | # --- database stuff | 160 | # --- database stuff |
176 | print(f'Using database: ', args.db) | 161 | print(f'Using database: ', args.db) |
@@ -183,7 +168,6 @@ if __name__=='__main__': | @@ -183,7 +168,6 @@ if __name__=='__main__': | ||
183 | print(f'Inserting {len(students)}') | 168 | print(f'Inserting {len(students)}') |
184 | insert_students_into_db(session, students) | 169 | insert_students_into_db(session, students) |
185 | 170 | ||
186 | - # print(args.update) | ||
187 | for s in args.update: | 171 | for s in args.update: |
188 | print(f'Updating password of: {s}') | 172 | print(f'Updating password of: {s}') |
189 | u = session.query(Student).get(s) | 173 | u = session.query(Student).get(s) |