-
Notifications
You must be signed in to change notification settings - Fork 54
/
setup.py
87 lines (68 loc) · 2.27 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# coding: utf-8
import re
import sys
import getopt
import MySQLdb
from pony.orm import db_session
from collipa import config
@db_session
def init_node():
from collipa.models import Node
if not Node.get(id=1):
Node(name=u'根节点', urlname='root',
description=u'一切的根源').save()
def convert(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
def merge():
m = MySQLdb.connect(host=config.db_host, user=config.db_user,
passwd=config.db_pass, db=config.db_name)
c = m.cursor()
c.execute(r'show tables')
old_table_names = [x[0] for x in c]
try:
for old_table_name in old_table_names:
table_name = old_table_name.lower()
sql = r'RENAME TABLE %s TO %s' % (old_table_name, table_name)
print(sql)
c.execute(sql)
c.close()
m.commit()
m.close()
except Exception as e:
print(type(e).__name__)
print(e)
raise
def main(argv):
try:
opts, args = getopt.getopt(argv, "", ["install", "init",
"iwanttodropdatabase", 'merge'])
except getopt.GetoptError:
print("参数错误")
sys.exit(2)
for opt, val in opts:
if opt == '--merge':
merge()
print('merge 成功!')
if opt == "--init":
m = MySQLdb.connect(host=config.db_host, user=config.db_user,
passwd=config.db_pass)
c = m.cursor()
# create database
try:
c.execute("create database %s" % config.db_name)
c.execute("grant all privileges on %s.* to '%s'@'localhost' identified by '%s'" %
(config.db_name, config.db_user, config.db_pass))
c.execute("flush privileges")
c.close()
m.commit()
m.close()
except Exception:
pass
# create tables
from collipa.models import db
db.generate_mapping(create_tables=True)
init_node()
print("数据库表初始化成功")
if __name__ == "__main__":
main(sys.argv[1:])