81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
#!/usr/bin/python
|
|
|
|
import os
|
|
import sys
|
|
import time
|
|
import json
|
|
import redis
|
|
import pymysql
|
|
import datetime
|
|
import pprint
|
|
from string import Template
|
|
|
|
def readCombineRule():
|
|
json_data = json.loads(open('../tools_conf/combine_rule.json', 'r').read())
|
|
return json_data
|
|
|
|
def readDBConf():
|
|
json_data = json.loads(open('../tools_conf/db_conf.json', 'r').read())
|
|
assert len(json_data) > 1
|
|
return json_data
|
|
|
|
def readGlobalRedisConf():
|
|
json_data = json.loads(open('../tools_conf/global_redis.json', 'r').read())
|
|
return json_data
|
|
|
|
def _checkTables(rule_conf, rows):
|
|
assert(len(rule_conf) == len(rows))
|
|
for row in rows:
|
|
if row[0] not in rule_conf:
|
|
print(row[0])
|
|
assert False
|
|
|
|
def _checkColumns(table_name, a_columns, b_columans):
|
|
assert len(a_columns) > 0 and len(a_columns) == len(b_columans)
|
|
for a_col in a_columns:
|
|
found = False
|
|
for b_col in b_columans:
|
|
if a_col == b_col:
|
|
found = True
|
|
break
|
|
#end for b_col
|
|
assert found
|
|
|
|
def checkDB(db_conf, rule_conf):
|
|
table_columns = {}
|
|
rolename_hash = {}
|
|
for conf in db_conf:
|
|
conn = pymysql.connect(host = conf['db_host'],
|
|
port = conf['db_port'],
|
|
user = conf['db_user'],
|
|
passwd = conf['db_passwd'],
|
|
db = conf['db_database'],
|
|
charset = 'utf8'
|
|
)
|
|
assert conn
|
|
cursor = conn.cursor()
|
|
cursor.execute('SHOW TABLES;')
|
|
rows = cursor.fetchall()
|
|
_checkTables(rule_conf, rows)
|
|
|
|
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
|
|
for rule in rule_conf.values():
|
|
cursor.execute('SHOW COLUMNS FROM %s;' % (rule['table_name']) )
|
|
rows = cursor.fetchall()
|
|
if rule['table_name'] in table_columns:
|
|
_checkColumns(rule['table_name'], table_columns[rule['table_name']], rows)
|
|
else:
|
|
table_columns[rule['table_name']] = rows
|
|
|
|
|
|
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
|
|
cursor.execute('SELECT * FROM role;')
|
|
rows = cursor.fetchall()
|
|
for row in rows:
|
|
assert row['name'] not in rolename_hash
|
|
rolename_hash[row['name']] = row
|
|
# pprint.pprint(row)
|
|
|
|
conn.close()
|