Commit 8f63323ec68b7f58304f4ba71664e8c6c75fda20
1 parent
305612ce
Exists in
master
and in
1 other branch
- changed initdb again
Showing
1 changed file
with
7 additions
and
8 deletions
Show diff stats
initdb.py
| ... | ... | @@ -50,7 +50,7 @@ def parse_commandline_arguments(): |
| 50 | 50 | help='users to update') |
| 51 | 51 | |
| 52 | 52 | argparser.add_argument('--pw', |
| 53 | - default='', | |
| 53 | + default=None, | |
| 54 | 54 | type=str, |
| 55 | 55 | help='set password for new and updated users') |
| 56 | 56 | |
| ... | ... | @@ -81,9 +81,9 @@ def get_students_from_csv(filename): |
| 81 | 81 | |
| 82 | 82 | # =========================================================================== |
| 83 | 83 | # replace password by hash for a single student |
| 84 | -def hashpw(student): | |
| 84 | +def hashpw(student, pw=None): | |
| 85 | 85 | print('.', end='', flush=True) |
| 86 | - pw = student.get('pw', student['uid']).encode('utf-8') | |
| 86 | + pw = (pw or student.get('pw', None) or student['uid']).encode('utf-8') | |
| 87 | 87 | student['pw'] = bcrypt.hashpw(pw, bcrypt.gensalt()) |
| 88 | 88 | |
| 89 | 89 | |
| ... | ... | @@ -150,12 +150,11 @@ if __name__=='__main__': |
| 150 | 150 | # --- password hashing |
| 151 | 151 | if students: |
| 152 | 152 | print(f'Generating password hashes (bcrypt).') |
| 153 | - if args.pw: # maybe set default password | |
| 154 | - for s in students: | |
| 155 | - s['pw'] = args.pw | |
| 156 | - | |
| 153 | + hash_func = lambda s: hashpw(s, args.pw) | |
| 157 | 154 | with ThreadPoolExecutor() as executor: |
| 158 | - executor.map(hashpw, students) | |
| 155 | + executor.map(hash_func, students) # hashing in parallel | |
| 156 | + | |
| 157 | + print() | |
| 159 | 158 | |
| 160 | 159 | # --- database stuff |
| 161 | 160 | print(f'Using database: ', args.db) | ... | ... |