Show More
@@ -1,71 +1,72 | |||
|
1 | 1 | import logging |
|
2 | 2 | import sqlite3 |
|
3 | 3 | |
|
4 | 4 | import os |
|
5 | 5 | import crypt |
|
6 | 6 | from os.path import dirname as dn |
|
7 | 7 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) |
|
8 | 8 | logging.basicConfig(level=logging.DEBUG) |
|
9 | 9 | |
|
10 | 10 | def get_sqlite_conn_cur(): |
|
11 | 11 | conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db')) |
|
12 | 12 | cur = conn.cursor() |
|
13 | 13 | return conn, cur |
|
14 | 14 | |
|
15 | def check_for_db(): | |
|
16 | if os.path.isfile(os.path.join(ROOT, 'hg_app.db')): | |
|
17 | raise Exception('database already exists') | |
|
15 | def check_for_db(override): | |
|
16 | if not override: | |
|
17 | if os.path.isfile(os.path.join(ROOT, 'hg_app.db')): | |
|
18 | raise Exception('database already exists') | |
|
18 | 19 | |
|
19 | def create_tables(): | |
|
20 | def create_tables(override=False): | |
|
20 | 21 | """ |
|
21 | 22 | Create a auth database |
|
22 | 23 | """ |
|
23 | check_for_db() | |
|
24 | check_for_db(override) | |
|
24 | 25 | conn, cur = get_sqlite_conn_cur() |
|
25 | 26 | try: |
|
26 | 27 | logging.info('creating table %s', 'users') |
|
27 | 28 | cur.execute("""DROP TABLE IF EXISTS users """) |
|
28 | 29 | cur.execute("""CREATE TABLE users |
|
29 | 30 | (user_id INTEGER PRIMARY KEY AUTOINCREMENT, |
|
30 | 31 | username TEXT, |
|
31 | 32 | password TEXT, |
|
32 | 33 | active INTEGER, |
|
33 | 34 | admin INTEGER)""") |
|
34 | 35 | logging.info('creating table %s', 'user_logs') |
|
35 | 36 | cur.execute("""DROP TABLE IF EXISTS user_logs """) |
|
36 | 37 | cur.execute("""CREATE TABLE user_logs |
|
37 | 38 | (id INTEGER PRIMARY KEY AUTOINCREMENT, |
|
38 | 39 | user_id INTEGER, |
|
39 | 40 | repository TEXT, |
|
40 | 41 | action TEXT, |
|
41 | 42 | action_date DATETIME)""") |
|
42 | 43 | conn.commit() |
|
43 | 44 | except: |
|
44 | 45 | conn.rollback() |
|
45 | 46 | raise |
|
46 | 47 | |
|
47 | 48 | cur.close() |
|
48 | 49 | |
|
49 | 50 | def admin_prompt(): |
|
50 | 51 | import getpass |
|
51 | 52 | username = raw_input('give username:') |
|
52 | 53 | password = getpass.getpass('Specify admin password:') |
|
53 | 54 | create_user(username, password, True) |
|
54 | 55 | |
|
55 | 56 | def create_user(username, password, admin=False): |
|
56 | 57 | conn, cur = get_sqlite_conn_cur() |
|
57 | 58 | password_crypt = crypt.crypt(password, '6a') |
|
58 | 59 | logging.info('creating user %s', username) |
|
59 | 60 | try: |
|
60 | 61 | cur.execute("""INSERT INTO users values (?,?,?,?,?) """, |
|
61 | 62 | (None, username, password_crypt, 1, admin)) |
|
62 | 63 | conn.commit() |
|
63 | 64 | except: |
|
64 | 65 | conn.rollback() |
|
65 | 66 | raise |
|
66 | 67 | |
|
67 | 68 | if __name__ == '__main__': |
|
68 | create_tables() | |
|
69 | create_tables(True) | |
|
69 | 70 | admin_prompt() |
|
70 | 71 | |
|
71 | 72 |
General Comments 0
You need to be logged in to leave comments.
Login now