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