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