-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_sql.py
169 lines (146 loc) · 5.58 KB
/
gen_sql.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# coding=utf-8
import os
import sys
import json
import locale
from pypika import MySQLQuery, Table, Field
try:
locale.setlocale(locale.LC_ALL, "C.UTF-8")
except locale.Error:
print("unsupported locale C.UTF-8")
config_file_name = "config.json"
dump_global_file_name = "dump_global.sql"
dump_global_sec_cfg_file_name = "dump_global_sec_cfg.sql"
modules = Table('modules')
module_files = [
"action_config_schema",
"changelog",
"config_schema",
"default_action_config",
"default_config",
"default_event_config",
"event_config_schema",
"fields_schema",
"info",
"locale",
"static_dependencies",
]
sec_cfg_module_files = [
"secure_config_schema",
"secure_default_config",
]
if len(sys.argv) > 1:
dump_global_file_name = os.path.join(sys.argv[1], dump_global_file_name)
dump_global_sec_cfg_file_name = os.path.join(sys.argv[1], dump_global_sec_cfg_file_name)
def load_json_file(path):
with open(path, mode="r", encoding="utf-8") as file:
return json.load(file)
def load_module(name, version):
get_path = lambda file: "{}/{}/config/{}.json".format(name, version, file)
module_config = dict()
return dict((file, load_json_file(get_path(file))) for file in module_files)
def load_sec_cfg_module(name, version):
get_path = lambda file: "{}/{}/config/{}.json".format(name, version, file)
module_config = dict()
return dict((file, load_json_file(get_path(file))) for file in sec_cfg_module_files)
def write_dump_file(lines, name):
with open(name, mode="w", encoding="utf-8") as dump_file:
dump_file.writelines(lines)
def build_sem_version(v):
return ".".join([str(v[p]) for p in ("major", "minor", "patch")])
def merge_module_config(module, mi):
for cname, cdata in mi["changes"].items():
if cname == "dynamic_dependencies" and cname in module.keys():
module[cname] = cdata
elif cname == "current_event_config" and cname in module.keys():
for ename, edata in cdata.items():
module[cname][ename].update(edata)
elif cname == "current_action_config" and cname in module.keys():
for aname, adata in cdata.items():
module[cname][aname].update(adata)
elif cname == "current_config" and cname in module.keys():
module[cname].update(cdata)
elif cname == "secure_current_config" and cname in module.keys():
module[cname].update(cdata)
def append_delete_query(lines, name, version):
lines.append(
(MySQLQuery\
.from_(modules)\
.delete()\
.where(modules.name == name)\
.where(modules.version == version)\
.get_sql() + ";\n").replace("\\", "\\\\")
)
def append_insert_query(lines, module):
lines.append(
(MySQLQuery\
.into(modules)\
.columns(*module.keys())\
.insert(*module.values())\
.ignore()\
.get_sql() + ";\n").replace("\\", "\\\\")
)
def append_update_query(lines, module, otps, mid, mc):
module_update = \
MySQLQuery\
.update(modules)\
.where(modules.id == mid)
for opt, val in mc.items():
module_update = module_update.set(opt, val)
for opt in otps:
if opt in module.keys():
module_update = module_update.set(opt, module[opt])
lines.append((module_update.get_sql() + ";\n").replace("\\", "\\\\"))
base_idx=50
shift_idx=2
dump_lines = list()
config = load_json_file(config_file_name)
modules_delete = \
MySQLQuery\
.from_(modules)\
.delete()\
.where(modules.id >= base_idx)\
.where(modules.id < base_idx + len(config) + shift_idx)
dump_lines.append("-- CLEAR SYSTEM MODULES FROM GLOBAL TABLE --\n")
dump_lines.append((modules_delete.get_sql() + ";\n\n").replace("\\", "\\\\"))
for idx, c in enumerate(config):
module_id = base_idx + idx
module_name = c["name"]
module_version = build_sem_version(c["version"])
module_ext = {
"id": module_id,
"tenant_id": 1,
"service_type": "vxmonitor",
"state": c["state"],
"last_update": c["last_update"],
}
module = {opt: "{}" for opt in module_files}
module.update(module_ext)
dump_lines.append("-- MODULE {} version {} --\n".format(module_name, module_version))
append_delete_query(dump_lines, module_name, module_version)
append_insert_query(dump_lines, module)
module = load_module(module_name, module_version)
module.update({opt : json.dumps(module[opt], ensure_ascii=False) for opt in module.keys()})
append_update_query(dump_lines, module, module_files, module_id, {
"state": c["state"],
"last_update": c["last_update"],
})
dump_lines.append("\n")
dump_lines.append("-- RESET AUTO_INCREMENT COUNTER FOR MODULES --\n")
dump_lines.append("ALTER TABLE `modules` AUTO_INCREMENT = 1000;")
dump_lines.append("\n")
write_dump_file(dump_lines, dump_global_file_name)
dump_lines = list()
for idx, c in enumerate(config):
module_id = base_idx + idx
module_name = c["name"]
module_version = build_sem_version(c["version"])
dump_lines.append("-- MODULE {} version {} --\n".format(module_name, module_version))
module = load_sec_cfg_module(module_name, module_version)
module.update({opt : json.dumps(module[opt], ensure_ascii=False) for opt in module.keys()})
append_update_query(dump_lines, module, sec_cfg_module_files, module_id, {
"last_update": c["last_update"],
})
dump_lines.append("\n")
write_dump_file(dump_lines, dump_global_sec_cfg_file_name)
print("dump SQL file was generated")