Show More
@@ -1,63 +1,67 b'' | |||||
1 | import logging |
|
1 | import logging | |
2 | from os.path import dirname as dn |
|
2 | from os.path import dirname as dn | |
3 | from sqlalchemy.engine import create_engine |
|
3 | from sqlalchemy.engine import create_engine | |
4 | import os |
|
4 | import os | |
|
5 | import sys | |||
|
6 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) | |||
|
7 | sys.path.append(ROOT) | |||
|
8 | ||||
5 | from pylons_app.model.db import Users |
|
9 | from pylons_app.model.db import Users | |
6 | from pylons_app.model.meta import Session |
|
10 | from pylons_app.model.meta import Session | |
7 |
|
11 | |||
8 | from pylons_app.lib.auth import get_crypt_password |
|
12 | from pylons_app.lib.auth import get_crypt_password | |
9 | from pylons_app.model import init_model |
|
13 | from pylons_app.model import init_model | |
10 |
|
14 | |||
11 | ROOT = dn(dn(dn(os.path.realpath(__file__)))) |
|
15 | ||
12 | logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s') |
|
16 | logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s') | |
13 | from pylons_app.model.meta import Base |
|
17 | from pylons_app.model.meta import Base | |
14 |
|
18 | |||
15 | class DbManage(object): |
|
19 | class DbManage(object): | |
16 | def __init__(self): |
|
20 | def __init__(self): | |
17 | dburi = 'sqlite:////%s' % os.path.join(ROOT, 'hg_app.db') |
|
21 | dburi = 'sqlite:////%s' % os.path.join(ROOT, 'hg_app.db') | |
18 | engine = create_engine(dburi) |
|
22 | engine = create_engine(dburi) | |
19 | init_model(engine) |
|
23 | init_model(engine) | |
20 | self.sa = Session() |
|
24 | self.sa = Session() | |
21 |
|
25 | |||
22 | def check_for_db(self, override): |
|
26 | def check_for_db(self, override): | |
23 | if not override: |
|
27 | if not override: | |
24 | if os.path.isfile(os.path.join(ROOT, 'hg_app.db')): |
|
28 | if os.path.isfile(os.path.join(ROOT, 'hg_app.db')): | |
25 | raise Exception('database already exists') |
|
29 | raise Exception('database already exists') | |
26 |
|
30 | |||
27 | def create_tables(self, override=False): |
|
31 | def create_tables(self, override=False): | |
28 | """ |
|
32 | """ | |
29 | Create a auth database |
|
33 | Create a auth database | |
30 | """ |
|
34 | """ | |
31 | self.check_for_db(override) |
|
35 | self.check_for_db(override) | |
32 |
|
36 | |||
33 | Base.metadata.create_all(checkfirst=override) |
|
37 | Base.metadata.create_all(checkfirst=override) | |
34 | logging.info('Created tables') |
|
38 | logging.info('Created tables') | |
35 |
|
39 | |||
36 | def admin_prompt(self): |
|
40 | def admin_prompt(self): | |
37 | import getpass |
|
41 | import getpass | |
38 | username = raw_input('give admin username:') |
|
42 | username = raw_input('give admin username:') | |
39 | password = getpass.getpass('Specify admin password:') |
|
43 | password = getpass.getpass('Specify admin password:') | |
40 | self.create_user(username, password, True) |
|
44 | self.create_user(username, password, True) | |
41 |
|
45 | |||
42 | def create_user(self, username, password, admin=False): |
|
46 | def create_user(self, username, password, admin=False): | |
43 | logging.info('creating user %s', username) |
|
47 | logging.info('creating user %s', username) | |
44 |
|
48 | |||
45 | new_user = Users() |
|
49 | new_user = Users() | |
46 | new_user.username = username |
|
50 | new_user.username = username | |
47 | new_user.password = get_crypt_password(password) |
|
51 | new_user.password = get_crypt_password(password) | |
48 | new_user.admin = admin |
|
52 | new_user.admin = admin | |
49 | new_user.active = True |
|
53 | new_user.active = True | |
50 |
|
54 | |||
51 | try: |
|
55 | try: | |
52 | self.sa.add(new_user) |
|
56 | self.sa.add(new_user) | |
53 | self.sa.commit() |
|
57 | self.sa.commit() | |
54 | except: |
|
58 | except: | |
55 | self.sa.rollback() |
|
59 | self.sa.rollback() | |
56 | raise |
|
60 | raise | |
57 |
|
61 | |||
58 | if __name__ == '__main__': |
|
62 | if __name__ == '__main__': | |
59 | dbmanage = DbManage() |
|
63 | dbmanage = DbManage() | |
60 | dbmanage.create_tables(override=True) |
|
64 | dbmanage.create_tables(override=True) | |
61 | dbmanage.admin_prompt() |
|
65 | dbmanage.admin_prompt() | |
62 |
|
66 | |||
63 |
|
67 |
General Comments 0
You need to be logged in to leave comments.
Login now