Commit 834adb0745edd0562096caab6ed9aabc05dee1fc
1 parent
d895857f
Exists in
master
and in
1 other branch
- changed initdb.py default password to be equal to the student id
Showing
2 changed files
with
51 additions
and
48 deletions
Show diff stats
README.md
| ... | ... | @@ -6,11 +6,11 @@ |
| 6 | 6 | We will need to install python3.6 with sqlite3 support. |
| 7 | 7 | This can be done using the system package management, downloaded from [http://www.python.org](), or compiled from sources. |
| 8 | 8 | |
| 9 | -- Installing from the system package management: | |
| 9 | +- Installing from the system package manager: | |
| 10 | 10 | - OSX: `port install python36` |
| 11 | 11 | - FreeBSD: `pkg install python36 py36-sqlite3` |
| 12 | 12 | - Linux: `apt-get install ???` (not available yet?) |
| 13 | -- Installing from sources: | |
| 13 | +- Installing from source: | |
| 14 | 14 | - Download from [http://www.python.org]() |
| 15 | 15 | - `unxz Python-3.6.tar.xz` |
| 16 | 16 | - `tar xvf Python-3.6.tar` | ... | ... |
initdb.py
| ... | ... | @@ -4,13 +4,13 @@ import csv |
| 4 | 4 | import argparse |
| 5 | 5 | import re |
| 6 | 6 | import string |
| 7 | -import sys | |
| 7 | +from sys import exit | |
| 8 | 8 | |
| 9 | 9 | import bcrypt |
| 10 | 10 | from sqlalchemy import create_engine |
| 11 | 11 | from sqlalchemy.orm import sessionmaker |
| 12 | 12 | |
| 13 | -from models import Base, Student #, Answer | |
| 13 | +from models import Base, Student | |
| 14 | 14 | |
| 15 | 15 | # SIIUE names have alien strings like "(TE)" and are sometimes capitalized |
| 16 | 16 | # We remove them so that students dont keep asking what it means |
| ... | ... | @@ -26,62 +26,65 @@ argparser.add_argument('--pw', default='', type=str, help='default password') |
| 26 | 26 | argparser.add_argument('csvfile', nargs='?', type=str, default='', help='CSV filename') |
| 27 | 27 | args = argparser.parse_args() |
| 28 | 28 | |
| 29 | -# =========================================================================== | |
| 30 | -hashed_pw = bcrypt.hashpw(args.pw.encode('utf-8'), bcrypt.gensalt()) | |
| 31 | - | |
| 32 | -engine = create_engine('sqlite:///{}'.format(args.db), echo=False) | |
| 29 | +# =======================================================x==================== | |
| 30 | +engine = create_engine(f'sqlite:///{args.db}', echo=False) | |
| 33 | 31 | Base.metadata.create_all(engine) # Criate schema if needed |
| 34 | 32 | Session = sessionmaker(bind=engine) |
| 35 | 33 | |
| 36 | -# --- start session --- | |
| 37 | -try: | |
| 38 | - session = Session() | |
| 34 | +if args.csvfile: | |
| 35 | + # add students from csv file if available | |
| 36 | + try: | |
| 37 | + csvreader = csv.DictReader(open(args.csvfile, encoding='iso-8859-1'), delimiter=';', quotechar='"', skipinitialspace=True) | |
| 38 | + except EnvironmentError: | |
| 39 | + print(f'Error: CSV file "{args.csvfile}" not found.') | |
| 40 | + exit(1) | |
| 39 | 41 | |
| 40 | - # add administrator | |
| 41 | - session.add(Student(id='0', name='Professor', password=hashed_pw)) | |
| 42 | - | |
| 43 | - # add students | |
| 44 | - if args.csvfile: | |
| 45 | - # from csv file if available | |
| 46 | - try: | |
| 47 | - csvreader = csv.DictReader(open(args.csvfile, encoding='iso-8859-1'), delimiter=';', quotechar='"', skipinitialspace=True) | |
| 48 | - except EnvironmentError: | |
| 49 | - print('Error: CSV file "{0}" not found.'.format(args.csvfile)) | |
| 50 | - session.rollback() | |
| 51 | - sys.exit(1) | |
| 52 | - else: | |
| 53 | - session.add_all([Student(id=r['N.º'], name=fix(r['Nome']), password=hashed_pw) for r in csvreader]) | |
| 54 | - elif args.demo: | |
| 55 | - # add a few fake students | |
| 56 | - fakes = [ | |
| 57 | - ['1915', 'Alan Turing'], | |
| 58 | - ['1938', 'Donald Knuth'], | |
| 59 | - ['1815', 'Ada Lovelace'], | |
| 60 | - ['1969', 'Linus Torvalds'], | |
| 61 | - ['1955', 'Tim Burners-Lee'], | |
| 62 | - ['1916', 'Claude Shannon'], | |
| 63 | - ['1903', 'John von Neumann'], | |
| 64 | - ] | |
| 65 | - session.add_all([Student(id=i, name=name, password=hashed_pw) for i,name in fakes]) | |
| 42 | + students = {s['N.º']: fix(s['Nome']) for s in csvreader} | |
| 66 | 43 | |
| 67 | - session.commit() | |
| 44 | +elif args.demo: | |
| 45 | + # add a few fake students | |
| 46 | + students = { | |
| 47 | + '1915': 'Alan Turing', | |
| 48 | + '1938': 'Donald Knuth', | |
| 49 | + '1815': 'Ada Lovelace', | |
| 50 | + '1969': 'Linus Torvalds', | |
| 51 | + '1955': 'Tim Burners-Lee', | |
| 52 | + '1916': 'Claude Shannon', | |
| 53 | + '1903': 'John von Neumann', | |
| 54 | + } | |
| 68 | 55 | |
| 69 | -except Exception: | |
| 70 | - print('Error: Database already exists.') | |
| 71 | - session.rollback() | |
| 72 | - sys.exit(1) | |
| 56 | +# add administrator | |
| 57 | +students['0'] = 'Professor' | |
| 58 | + | |
| 59 | + | |
| 60 | +print('Generating bcrypt password hashes takes time. Please be patient.') | |
| 61 | +try: | |
| 62 | + # --- start db session --- | |
| 63 | + session = Session() | |
| 64 | + | |
| 65 | + for num, name in students.items(): | |
| 66 | + print('.', end='', flush=True) | |
| 67 | + pw = (args.pw or num).encode('utf-8') | |
| 68 | + session.add(Student(id=num, name=name, password=bcrypt.hashpw(pw, bcrypt.gensalt()))) | |
| 69 | + print() | |
| 73 | 70 | |
| 74 | -else: | |
| 75 | 71 | n = session.query(Student).count() |
| 76 | - print('New database created: {0}\n{1} user(s) inserted:'.format(args.db, n)) | |
| 72 | + print(f'New database created: {args.db}\n{n} user(s) inserted:') | |
| 77 | 73 | |
| 78 | 74 | users = session.query(Student).order_by(Student.id).all() |
| 79 | - print(' {0:8} - {1} (administrator)'.format(users[0].id, users[0].name)) | |
| 75 | + print(f' {users[0].id:8} - {users[0].name} (administrator)') | |
| 80 | 76 | if n > 1: |
| 81 | - print(' {0:8} - {1}'.format(users[1].id, users[1].name)) | |
| 77 | + print(f' {users[1].id:8} - {users[1].name}') | |
| 82 | 78 | if n > 3: |
| 83 | 79 | print(' ... ...') |
| 84 | 80 | if n > 2: |
| 85 | - print(' {0:8} - {1}'.format(users[-1].id, users[-1].name)) | |
| 81 | + print(f' {users[-1].id:8} - {users[-1].name}') | |
| 86 | 82 | |
| 87 | -# --- end session --- | |
| 83 | +except Exception as e: | |
| 84 | + print(f'Error: Database "{args.db}" already exists?') | |
| 85 | + session.rollback() | |
| 86 | + exit(1) | |
| 87 | + | |
| 88 | +else: | |
| 89 | + # --- end session --- | |
| 90 | + session.commit() | ... | ... |