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