Mercurial > kallithea
annotate pylons_app/lib/db_manage.py @ 66:163464441e0d
updated db manage script for creating interactive admin account and db
author | Marcin Kuzminski <marcin@python-blog.com> |
---|---|
date | Sat, 10 Apr 2010 19:07:13 +0200 |
parents | 736078908f37 |
children | ffd9ff6e2f33 |
rev | line source |
---|---|
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
1 import logging |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
2 import sqlite3 |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
3 |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
4 import os |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
5 import crypt |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
6 from os.path import dirname as dn |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
7 ROOT = dn(dn(dn(os.path.realpath(__file__)))) |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
8 logging.basicConfig(level=logging.DEBUG) |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
9 |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
10 def get_sqlite_conn_cur(): |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
11 conn = sqlite3.connect(os.path.join(ROOT, 'hg_app.db')) |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
12 cur = conn.cursor() |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
13 return conn, cur |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
14 |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
15 def check_for_db(): |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
16 if os.path.isfile(os.path.join(ROOT, 'hg_app.db')): |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
17 raise Exception('database already exists') |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
18 |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
19 def create_tables(): |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
20 """ |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
21 Create a auth database |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
22 """ |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
23 check_for_db() |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
24 conn, cur = get_sqlite_conn_cur() |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
25 try: |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
26 logging.info('creating table %s', 'users') |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
27 cur.execute("""DROP TABLE IF EXISTS users """) |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
28 cur.execute("""CREATE TABLE users |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
29 (user_id INTEGER PRIMARY KEY AUTOINCREMENT, |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
30 username TEXT, |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
31 password TEXT, |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
32 active INTEGER, |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
33 admin INTEGER)""") |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
34 logging.info('creating table %s', 'user_loggings') |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
35 cur.execute("""DROP TABLE IF EXISTS user_loggings """) |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
36 cur.execute("""CREATE TABLE user_loggings |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
37 (id INTEGER PRIMARY KEY AUTOINCREMENT, |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
38 user_id INTEGER, |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
39 repository TEXT, |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
40 action TEXT, |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
41 action_date DATETIME)""") |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
42 conn.commit() |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
43 except: |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
44 conn.rollback() |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
45 raise |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
46 |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
47 cur.close() |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
48 |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
49 def admin_prompt(): |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
50 import getpass |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
51 username = raw_input('give username:') |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
52 password = getpass.getpass('Specify admin password:') |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
53 create_user(username, password, True) |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
54 |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
55 def create_user(username, password, admin=False): |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
56 conn, cur = get_sqlite_conn_cur() |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
57 password_crypt = crypt.crypt(password, '6a') |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
58 logging.info('creating user %s', username) |
59
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
59 try: |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
60 cur.execute("""INSERT INTO users values (?,?,?,?,?) """, |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
61 (None, username, password_crypt, 1, admin)) |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
62 conn.commit() |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
63 except: |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
64 conn.rollback() |
736078908f37
added db_manage script
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
65 raise |
66
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
66 |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
67 if __name__ == '__main__': |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
68 create_tables() |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
69 admin_prompt() |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
70 |
163464441e0d
updated db manage script for creating interactive admin account and db
Marcin Kuzminski <marcin@python-blog.com>
parents:
59
diff
changeset
|
71 |