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