toolkits 0.2.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- toolkits/3des/3des.py +93 -0
- toolkits/3des/__init__.py +0 -0
- toolkits/__init__.py +2 -0
- toolkits/basic/__init__.py +0 -0
- toolkits/basic/list_helper.py +26 -0
- toolkits/config/__init__.py +0 -0
- toolkits/config/config_demo.py +43 -0
- toolkits/databases/__init__.py +0 -0
- toolkits/databases/database_client_util.py +143 -0
- toolkits/databases/es_client.py +88 -0
- toolkits/databases/hive_client.py +72 -0
- toolkits/databases/hive_cmd.py +113 -0
- toolkits/databases/hive_helper.py +220 -0
- toolkits/databases/redis_mgmt.py +95 -0
- toolkits/databases/sql_helper.py +291 -0
- toolkits/databases/sqlalchemy_helper.py +71 -0
- toolkits/databases/status_check.py +162 -0
- toolkits/db_query_demo.py +72 -0
- toolkits/libs_core/__init__.py +0 -0
- toolkits/libs_core/config_groups_helper.py +60 -0
- toolkits/libs_core/config_helper.py +22 -0
- toolkits/libs_core/env_prepare.py +145 -0
- toolkits/libs_core/load_module.py +46 -0
- toolkits/libs_core/mysql_helper.py +151 -0
- toolkits/network/__init__.py +0 -0
- toolkits/network/ip_helper.py +32 -0
- toolkits/network/pdi_helper.py +206 -0
- toolkits/network/send_mail.py +105 -0
- toolkits/system/__init__.py +0 -0
- toolkits/system/aes_cipher.py +44 -0
- toolkits/system/basic_utils.py +20 -0
- toolkits/system/collections_helper.py +72 -0
- toolkits/system/crpyt_helper.py +39 -0
- toolkits/system/dict2xml.py +416 -0
- toolkits/system/dict_helper.py +29 -0
- toolkits/system/excel_helper.py +101 -0
- toolkits/system/file_helper.py +52 -0
- toolkits/system/load_module.py +47 -0
- toolkits/system/priority_tasks.py +199 -0
- toolkits/system/process_monitor/__init__.py +0 -0
- toolkits/system/process_monitor/process_monitor.py +349 -0
- toolkits/system/shell_helper.py +263 -0
- toolkits/system/str_helper.py +187 -0
- toolkits/system/tasks_deamon/__init__.py +0 -0
- toolkits/system/tasks_deamon/tasks_controller.py +70 -0
- toolkits/system/tasks_deamon/tasks_multiprocessing.py +134 -0
- toolkits/system/tasks_deamon/tasks_process.py +137 -0
- toolkits/system/test_shell_helper.py +2 -0
- toolkits/system/time_helper.py +175 -0
- toolkits/system/win32_env.py +49 -0
- toolkits/tookits_app.py +17 -0
- toolkits/tookits_cli.py +126 -0
- toolkits-0.2.7.dist-info/METADATA +35 -0
- toolkits-0.2.7.dist-info/RECORD +56 -0
- toolkits-0.2.7.dist-info/WHEEL +4 -0
- toolkits-0.2.7.dist-info/entry_points.txt +5 -0
@@ -0,0 +1,162 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
import json
|
3
|
+
import sys
|
4
|
+
import fire
|
5
|
+
from log4python.Log4python import log
|
6
|
+
from toolkits.databases.database_client_util import DatabaseClientUtil
|
7
|
+
from toolkits.databases.sqlalchemy_helper import SqlAlchemyHelper
|
8
|
+
import importlib
|
9
|
+
importlib.reload(sys)
|
10
|
+
logger = log("StatusCheck")
|
11
|
+
|
12
|
+
|
13
|
+
class StatusCheck:
|
14
|
+
def __init__(self,
|
15
|
+
database_config,
|
16
|
+
task_info_table="etl_tasks",
|
17
|
+
field_task_name="task_name",
|
18
|
+
field_id_name="task_id",
|
19
|
+
field_status_name="task_status",
|
20
|
+
status_on_value="1",
|
21
|
+
status_off_value="0",
|
22
|
+
redis_keys_prefix="TasksScheduleCheck_",
|
23
|
+
redis_db_num=1
|
24
|
+
):
|
25
|
+
self.tasks_add_list = []
|
26
|
+
self.tasks_edit_list = []
|
27
|
+
self.tasks_delete_list = []
|
28
|
+
self.__tasks_list_redis = {}
|
29
|
+
self.__tasks_list_db = []
|
30
|
+
|
31
|
+
self.__status_on_value = status_on_value
|
32
|
+
self.__status_off_value = status_off_value
|
33
|
+
|
34
|
+
self.__task_info_table = task_info_table
|
35
|
+
self.__task_name = field_task_name
|
36
|
+
self.__task_id_name = field_id_name
|
37
|
+
self.__tasks_keys_prefix = redis_keys_prefix
|
38
|
+
self.__fetch_tasks_sql = "select * from %s where `%s` = '%s' " % (self.__task_info_table,
|
39
|
+
field_status_name,
|
40
|
+
self.__status_on_value
|
41
|
+
)
|
42
|
+
|
43
|
+
self.__tasks_keys_pattern = "%s*" % self.__tasks_keys_prefix
|
44
|
+
self.__database_init = DatabaseClientUtil(database_config)
|
45
|
+
self.__mysql_conn = self.__database_init.get_mysql_client()
|
46
|
+
self.__redis_cli = self.__database_init.get_redis_client(db_num=redis_db_num)
|
47
|
+
self.__sql_helper = SqlAlchemyHelper()
|
48
|
+
|
49
|
+
self.__check_status()
|
50
|
+
|
51
|
+
def get_tasks_list(self):
|
52
|
+
return self.__get_tasks_list_from_db()
|
53
|
+
|
54
|
+
def __add_redis_task(self, task_info):
|
55
|
+
key_id = "%s%s" % (self.__tasks_keys_prefix, task_info[self.__task_id_name])
|
56
|
+
self.__redis_cli.set(key_id, json.dumps(task_info, ensure_ascii=False))
|
57
|
+
|
58
|
+
def __del_redis_task(self, task_info):
|
59
|
+
key_id = "%s%s" % (self.__tasks_keys_prefix, task_info[self.__task_id_name])
|
60
|
+
self.__redis_cli.delete(key_id)
|
61
|
+
|
62
|
+
def __get_tasks_list_from_redis(self):
|
63
|
+
tasks_dict = {}
|
64
|
+
keys = self.__redis_cli.keys(self.__tasks_keys_pattern)
|
65
|
+
|
66
|
+
keys_final = keys
|
67
|
+
if type(keys) is str:
|
68
|
+
keys_final = [keys]
|
69
|
+
|
70
|
+
if type(keys_final) is list:
|
71
|
+
for key_item in keys_final:
|
72
|
+
task_data = self.__redis_cli.get(key_item)
|
73
|
+
task_info = json.loads(task_data)
|
74
|
+
tasks_dict[task_info[self.__task_id_name]] = task_info
|
75
|
+
else:
|
76
|
+
logger.error("Error:Keys type was wrong [%s]" % str(keys))
|
77
|
+
|
78
|
+
return tasks_dict
|
79
|
+
|
80
|
+
def __get_tasks_list_from_db(self):
|
81
|
+
ret = self.__mysql_conn.execute(self.__fetch_tasks_sql)
|
82
|
+
tasks_list = self.__sql_helper.rows2list(ret)
|
83
|
+
return tasks_list
|
84
|
+
|
85
|
+
def get_task_info_by_task_id(self, task_id=None):
|
86
|
+
if task_id:
|
87
|
+
sql = "%s and %s='%s'" % (self.__fetch_tasks_sql, self.__task_id_name, task_id)
|
88
|
+
else:
|
89
|
+
sql = self.__fetch_tasks_sql
|
90
|
+
logger.debug("SqlQuery:[%s]" % sql)
|
91
|
+
ret = self.__mysql_conn.execute(sql)
|
92
|
+
|
93
|
+
rows_data = []
|
94
|
+
if ret:
|
95
|
+
rows_data = self.__sql_helper.rows2list(ret)
|
96
|
+
task_info = {}
|
97
|
+
if len(rows_data) >= 1:
|
98
|
+
task_info = rows_data[0]
|
99
|
+
return task_info
|
100
|
+
|
101
|
+
def __change_task_status(self, tasks_add_list, tasks_edit_list, tasks_delete_list):
|
102
|
+
for item in tasks_add_list:
|
103
|
+
logger.debug("AddTask:[%s]" % json.dumps(item, ensure_ascii=False))
|
104
|
+
self.__add_redis_task(item)
|
105
|
+
|
106
|
+
for item_edit in tasks_edit_list:
|
107
|
+
self.__add_redis_task(item_edit)
|
108
|
+
logger.debug("EditTask:[%s]" % json.dumps(item_edit, ensure_ascii=False))
|
109
|
+
|
110
|
+
for task_item in tasks_delete_list:
|
111
|
+
self.__del_redis_task(task_item)
|
112
|
+
logger.debug("DeleteTask:[%s]" % json.dumps(task_item, ensure_ascii=False))
|
113
|
+
|
114
|
+
def __check_tasks_status_change(self):
|
115
|
+
tasks_add_list = []
|
116
|
+
tasks_edit_list = []
|
117
|
+
tasks_delete_list = []
|
118
|
+
task_id_list = []
|
119
|
+
|
120
|
+
for item_task in self.__tasks_list_db:
|
121
|
+
task_id = item_task[self.__task_id_name]
|
122
|
+
task_id_list.append(task_id)
|
123
|
+
|
124
|
+
if task_id in self.__tasks_list_redis:
|
125
|
+
tasks_edit_list.append(item_task)
|
126
|
+
else:
|
127
|
+
tasks_add_list.append(item_task)
|
128
|
+
|
129
|
+
deleted_id_list = list(set(self.__tasks_list_redis.keys()) - set(task_id_list))
|
130
|
+
for delete_id in deleted_id_list:
|
131
|
+
tasks_delete_list.append(self.__tasks_list_redis[delete_id])
|
132
|
+
|
133
|
+
return tasks_add_list, tasks_edit_list, tasks_delete_list
|
134
|
+
|
135
|
+
def __check_status(self):
|
136
|
+
self.__tasks_list_redis = self.__get_tasks_list_from_redis()
|
137
|
+
self.__tasks_list_db = self.__get_tasks_list_from_db()
|
138
|
+
|
139
|
+
self.tasks_add_list, self.tasks_edit_list, self.tasks_delete_list = self.__check_tasks_status_change()
|
140
|
+
self.__change_task_status(self.tasks_add_list, self.tasks_edit_list, self.tasks_delete_list)
|
141
|
+
|
142
|
+
def check_tasks_status_change(self, task_field_name):
|
143
|
+
tasks_change_list = []
|
144
|
+
for item_task in self.tasks_edit_list:
|
145
|
+
task_id = item_task[self.__task_id_name]
|
146
|
+
|
147
|
+
if task_field_name in item_task:
|
148
|
+
task_field_val = item_task[task_field_name]
|
149
|
+
|
150
|
+
if task_id in self.__tasks_list_redis and task_field_name in self.__tasks_list_redis[task_id]:
|
151
|
+
if task_field_val != self.__tasks_list_redis[task_id][task_field_name]:
|
152
|
+
tasks_change_list.append(item_task)
|
153
|
+
else:
|
154
|
+
logger.error("Error:task_field_name-[%s]" % task_field_name)
|
155
|
+
else:
|
156
|
+
logger.error("Error: field_name:[%s], task_info [%s]" % (task_field_name, json.dumps(item_task)))
|
157
|
+
return tasks_change_list
|
158
|
+
|
159
|
+
|
160
|
+
if __name__ == '__main__':
|
161
|
+
fire.Fire(StatusCheck)
|
162
|
+
exit()
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
import json
|
3
|
+
import traceback
|
4
|
+
|
5
|
+
import fire
|
6
|
+
from log4python.Log4python import log
|
7
|
+
from toolkits.system.basic_utils import get_script_directory
|
8
|
+
|
9
|
+
from toolkits.libs_core.env_prepare import EnvPrepare
|
10
|
+
from toolkits.libs_core.mysql_helper import MysqlHelper
|
11
|
+
|
12
|
+
logger = log("DbQueryDemo")
|
13
|
+
|
14
|
+
|
15
|
+
class DbQueryDemo:
|
16
|
+
def __init__(self, config_path=None):
|
17
|
+
self.__base_path = get_script_directory()
|
18
|
+
self.__config_path = config_path
|
19
|
+
if config_path is None:
|
20
|
+
if tookitsApp.config_file_path is None:
|
21
|
+
self.__env = EnvPrepare(tookitsApp.app_name)
|
22
|
+
self.__env.check_env()
|
23
|
+
self.__config_path = self.__env.get_config_path("config.py")
|
24
|
+
self.__env.check_config_ready(self.__config_path, "请先初始化配置文件、在数据库创建相应的数据表")
|
25
|
+
else:
|
26
|
+
self.__config_path = tookitsApp.config_file_path
|
27
|
+
|
28
|
+
self.__env = EnvPrepare(tookitsApp.app_name, self.__config_path)
|
29
|
+
self.__mysql_config = self.__env.get_config('mysql_info_online_alarm')
|
30
|
+
self.sql_helper = MysqlHelper(self.__mysql_config)
|
31
|
+
|
32
|
+
def query_sql(self, query_where) -> list:
|
33
|
+
list_api = self.sql_helper.query("SELECT x.* FROM sec_admin.api_sec_output_send x WHERE bot_process in (%s)" % query_where)
|
34
|
+
# logger.debug("Result:[%s]" % json.dumps(list_api))
|
35
|
+
logger.debug("Len: %s" % str(len(list_api)))
|
36
|
+
return list_api
|
37
|
+
|
38
|
+
@staticmethod
|
39
|
+
def __read_all_data(file_path):
|
40
|
+
fp = open(file_path)
|
41
|
+
data_list = fp.readlines()
|
42
|
+
fp.close()
|
43
|
+
ns_list = []
|
44
|
+
for item in data_list:
|
45
|
+
ns_list.append(str(item).strip())
|
46
|
+
return ns_list
|
47
|
+
|
48
|
+
def worker(self, file_path, batch_size=10):
|
49
|
+
data_list = self.__read_all_data(file_path)
|
50
|
+
final_list = []
|
51
|
+
for i in range(0, len(data_list), batch_size):
|
52
|
+
# 获取当前批次的数据
|
53
|
+
batch_data = data_list[i:i + batch_size]
|
54
|
+
sql_where = "'%s'" % "', '".join(batch_data)
|
55
|
+
|
56
|
+
kylin_list = self.query_sql(sql_where)
|
57
|
+
final_list.extend(kylin_list)
|
58
|
+
|
59
|
+
fp = open("%s.csv" % file_path, "w+")
|
60
|
+
fp.write("source,host,name_space")
|
61
|
+
for item in list(set(final_list)):
|
62
|
+
fp.write("%s,%s,%s\r\n" % (item['source'], item['host'], item['name_space']))
|
63
|
+
fp.close()
|
64
|
+
|
65
|
+
|
66
|
+
if __name__ == '__main__':
|
67
|
+
try:
|
68
|
+
fire.Fire(DbQueryDemo)
|
69
|
+
except Exception as ex:
|
70
|
+
logger.error("Error: %s" % ex)
|
71
|
+
logger.error(traceback.format_exc())
|
72
|
+
|
File without changes
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
|
4
|
+
class ConfigGroupsHelper:
|
5
|
+
"""
|
6
|
+
ConfigGroupsHelper
|
7
|
+
"""
|
8
|
+
def __init__(self, config_groups, config_groups_type="list"):
|
9
|
+
self.__group_config = {}
|
10
|
+
if config_groups_type == "list":
|
11
|
+
self.__init_config_list(config_groups)
|
12
|
+
elif config_groups_type == "dict":
|
13
|
+
self.__init_config_dict(config_groups)
|
14
|
+
|
15
|
+
def __init_config_dict(self, config_groups_dict, talk_groups=None):
|
16
|
+
init_groups = {}
|
17
|
+
chat_groups = dict(config_groups_dict)
|
18
|
+
if talk_groups:
|
19
|
+
if type(talk_groups) is dict:
|
20
|
+
chat_groups = {
|
21
|
+
"default": talk_groups
|
22
|
+
}
|
23
|
+
|
24
|
+
if chat_groups:
|
25
|
+
for item in chat_groups.keys():
|
26
|
+
init_groups[item] = chat_groups[item]
|
27
|
+
|
28
|
+
self.__group_config = init_groups
|
29
|
+
|
30
|
+
def __init_config_list(self, config_groups, talk_groups=None):
|
31
|
+
init_groups = {}
|
32
|
+
chat_groups = config_groups
|
33
|
+
if talk_groups:
|
34
|
+
if type(talk_groups) is dict:
|
35
|
+
chat_groups = [talk_groups]
|
36
|
+
else:
|
37
|
+
chat_groups = talk_groups
|
38
|
+
|
39
|
+
if chat_groups:
|
40
|
+
for item in chat_groups:
|
41
|
+
init_groups[item['name']] = item
|
42
|
+
|
43
|
+
self.__group_config = init_groups
|
44
|
+
|
45
|
+
def get_config_by_name(self, config_name):
|
46
|
+
config = None
|
47
|
+
if config_name not in self.__group_config.keys():
|
48
|
+
err_msg = "配置名称不存在,请检查!!"
|
49
|
+
print(err_msg)
|
50
|
+
exit(1)
|
51
|
+
else:
|
52
|
+
config = self.__group_config[config_name]
|
53
|
+
return config
|
54
|
+
|
55
|
+
def list_config_groups(self, config_name=""):
|
56
|
+
groups = self.__group_config.keys()
|
57
|
+
print("%s-配置组:\n" % config_name)
|
58
|
+
for item_group in groups:
|
59
|
+
desc = self.__group_config[item_group]['desc']
|
60
|
+
print("\tName: %s\tDesc:[%s]" % (item_group, desc))
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
from .load_module import LoadModule
|
3
|
+
|
4
|
+
|
5
|
+
class ConfigHelpers:
|
6
|
+
def __init__(self):
|
7
|
+
pass
|
8
|
+
|
9
|
+
@staticmethod
|
10
|
+
def load_config(config_file):
|
11
|
+
app = LoadModule()
|
12
|
+
config_info = app.load_from_file(config_file)
|
13
|
+
return config_info
|
14
|
+
|
15
|
+
@staticmethod
|
16
|
+
def get_config_by_name(config_data, config_name):
|
17
|
+
config_info = None
|
18
|
+
if config_data:
|
19
|
+
config_info = config_data.__dict__[config_name]
|
20
|
+
|
21
|
+
return config_info
|
22
|
+
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
import os
|
3
|
+
import platform
|
4
|
+
import sys
|
5
|
+
import traceback
|
6
|
+
|
7
|
+
import fire
|
8
|
+
import jmespath
|
9
|
+
from log4python.Log4python import log
|
10
|
+
from toolkits.system.basic_utils import get_script_directory
|
11
|
+
from toolkits.system.shell_helper import exec_shell
|
12
|
+
from unipath import Path
|
13
|
+
|
14
|
+
from .config_helper import ConfigHelpers
|
15
|
+
from .mysql_helper import MysqlHelper
|
16
|
+
|
17
|
+
logger = log("EnvPrepare")
|
18
|
+
|
19
|
+
|
20
|
+
class EnvPrepare:
|
21
|
+
config_file_path = None
|
22
|
+
|
23
|
+
def __init__(self, app_name, config_full_path=None, config_global_name="config_global"):
|
24
|
+
self.__base_path = get_script_directory()
|
25
|
+
self.__config_global_name = config_global_name
|
26
|
+
self.__user_home_path = os.path.expanduser("~/.config")
|
27
|
+
self.__app_name = app_name
|
28
|
+
self.__config_full_path = None
|
29
|
+
self.__config_path = "%s/%s/" % (self.__get_workdir(), self.__app_name)
|
30
|
+
if config_full_path:
|
31
|
+
if Path(config_full_path).exists():
|
32
|
+
self.__config_full_path = config_full_path
|
33
|
+
else:
|
34
|
+
logger.error("Config was not exist:[%s]" % config_full_path)
|
35
|
+
|
36
|
+
self.__tip_message = "Initialized: finished, please change the default's config to " \
|
37
|
+
"adapt your environment! "
|
38
|
+
self.__default_path_tip = "Default config path: %s" % self.__config_path
|
39
|
+
self.__dict_config = {}
|
40
|
+
|
41
|
+
def __get_workdir(self):
|
42
|
+
app_data = self.__user_home_path
|
43
|
+
if platform.system().lower() == 'windows': # print("windows")
|
44
|
+
app_data = "%s\\.config" % os.getenv("APPDATA")
|
45
|
+
elif platform.system().lower() == 'linux': # print("linux")
|
46
|
+
app_data = self.__user_home_path
|
47
|
+
return app_data
|
48
|
+
|
49
|
+
def check_env(self):
|
50
|
+
# check env-config
|
51
|
+
if not Path(self.__config_path).exists():
|
52
|
+
Path(self.__config_path).mkdir(parents=True)
|
53
|
+
src_path = "%s/../config/" % self.__base_path
|
54
|
+
self.init_default_config("directory", src_path, self.__config_path)
|
55
|
+
print("%s\n%s" % (self.__tip_message, self.__default_path_tip))
|
56
|
+
logger.error("%s\n%s" % (self.__tip_message, self.__default_path_tip))
|
57
|
+
|
58
|
+
def check_config_ready(self, config_path, error_message):
|
59
|
+
if not Path(config_path).exists():
|
60
|
+
msg = error_message + "\n" + self.__default_path_tip
|
61
|
+
print(msg)
|
62
|
+
logger.error(msg)
|
63
|
+
# raise Exception(msg)
|
64
|
+
exit(-1)
|
65
|
+
|
66
|
+
def get_config_path(self, config_file_name):
|
67
|
+
return "%s/%s" % (self.__config_path, config_file_name)
|
68
|
+
|
69
|
+
def get_config_from_file(self, config_path: str, config_key: str, force_reload: bool = False) -> dict:
|
70
|
+
if config_path not in self.__dict_config or force_reload is True:
|
71
|
+
config_data = ConfigHelpers.load_config(config_path)
|
72
|
+
self.__dict_config[config_path] = config_data
|
73
|
+
else:
|
74
|
+
config_data = self.__dict_config[config_path]
|
75
|
+
|
76
|
+
return ConfigHelpers.get_config_by_name(config_data, config_key)
|
77
|
+
|
78
|
+
def __get_default_config_file(self):
|
79
|
+
running_app = os.path.split(os.path.realpath(sys.argv[0]))[1]
|
80
|
+
if running_app[-3:] == ".py" or running_app[-4:] == ".pyc":
|
81
|
+
path_working = os.path.split(os.path.realpath(sys.argv[0]))[0]
|
82
|
+
else:
|
83
|
+
path_working = os.getcwd()
|
84
|
+
|
85
|
+
config_path_working = "%s/config.py" % path_working
|
86
|
+
if not Path(config_path_working).exists():
|
87
|
+
config_path_working = "%s/%s/config.py" % (self.__user_home_path, self.__app_name)
|
88
|
+
return config_path_working
|
89
|
+
|
90
|
+
def get_default_config(self, config_key: str, force_reload: bool = False) -> dict:
|
91
|
+
if EnvPrepare.config_file_path:
|
92
|
+
config_path_working = EnvPrepare.config_file_path
|
93
|
+
else:
|
94
|
+
config_path_working = self.__get_default_config_file()
|
95
|
+
logger.debug("config_path_working: %s", config_path_working)
|
96
|
+
config_data = None
|
97
|
+
if Path(config_path_working).exists():
|
98
|
+
config_data = self.get_config_from_file(config_path_working, config_key, force_reload)
|
99
|
+
return config_data
|
100
|
+
|
101
|
+
def get_config(self, config_key: str, force_reload: bool = False):
|
102
|
+
if self.__config_full_path:
|
103
|
+
config_data = self.get_config_from_file(self.__config_full_path, self.__config_global_name, force_reload)
|
104
|
+
else:
|
105
|
+
config_data = self.get_default_config(self.__config_global_name, force_reload)
|
106
|
+
return jmespath.search(config_key, config_data)
|
107
|
+
|
108
|
+
@staticmethod
|
109
|
+
def init_default_config(init_type, src_path, dest_path):
|
110
|
+
init_status = False
|
111
|
+
if init_type == 'file':
|
112
|
+
if platform.system().lower() == 'windows':
|
113
|
+
cmd_copy = "XCOPY /S /-Y %s %s " % (str(src_path).replace("/", "\\"), str(dest_path).replace("/", "\\"))
|
114
|
+
elif platform.system().lower() == 'linux':
|
115
|
+
cmd_copy = "cp -arf %s %s " % (src_path, dest_path)
|
116
|
+
elif init_type == 'directory':
|
117
|
+
if platform.system().lower() == 'windows':
|
118
|
+
cmd_copy = "xcopy /S /-Y %s %s " % (str(src_path).replace("/", "\\"), str(dest_path).replace("/", "\\"))
|
119
|
+
elif platform.system().lower() == 'linux':
|
120
|
+
cmd_copy = "cp -arf %s/* %s/ " % (src_path, dest_path)
|
121
|
+
else:
|
122
|
+
return init_status
|
123
|
+
|
124
|
+
logger.info("CopyCMD: %s" % cmd_copy)
|
125
|
+
ret = exec_shell(cmd_copy)
|
126
|
+
if str(ret['exit_code']) == "0":
|
127
|
+
init_status = True
|
128
|
+
return init_status
|
129
|
+
|
130
|
+
@staticmethod
|
131
|
+
def init_db(database_config, sql_execute):
|
132
|
+
try:
|
133
|
+
mysql_conn = MysqlHelper(database_config)
|
134
|
+
mysql_conn.execute(sql_execute)
|
135
|
+
except Exception as ex:
|
136
|
+
logger.error("Error: %s" % ex)
|
137
|
+
logger.error(traceback.format_exc())
|
138
|
+
|
139
|
+
|
140
|
+
if __name__ == '__main__':
|
141
|
+
try:
|
142
|
+
fire.Fire(EnvPrepare)
|
143
|
+
except Exception as ex:
|
144
|
+
logger.error("Error: %s" % ex)
|
145
|
+
logger.error(traceback.format_exc())
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
import imp
|
3
|
+
import os
|
4
|
+
import traceback
|
5
|
+
import uuid
|
6
|
+
|
7
|
+
from log4python.Log4python import log
|
8
|
+
|
9
|
+
logger = log("LoadModule")
|
10
|
+
|
11
|
+
|
12
|
+
class LoadModule(object):
|
13
|
+
def __init__(self):
|
14
|
+
pass
|
15
|
+
|
16
|
+
@staticmethod
|
17
|
+
def load_from_file(file_path):
|
18
|
+
mod_name, file_ext = os.path.splitext(os.path.split(file_path)[-1])
|
19
|
+
py_mod = None
|
20
|
+
if file_ext.lower() == '.py':
|
21
|
+
py_mod = imp.load_source(mod_name, file_path)
|
22
|
+
elif file_ext.lower() == '.pyc':
|
23
|
+
py_mod = imp.load_compiled(mod_name, file_path)
|
24
|
+
|
25
|
+
return py_mod
|
26
|
+
|
27
|
+
@staticmethod
|
28
|
+
def __write_tmp_py(file_path, content):
|
29
|
+
fp = open(file_path, "w+")
|
30
|
+
fp.write(content)
|
31
|
+
fp.close()
|
32
|
+
|
33
|
+
def load_from_string(self, func_string, module_name=None):
|
34
|
+
modules = None
|
35
|
+
try:
|
36
|
+
str_random = str(uuid.uuid4()).replace("-", "")
|
37
|
+
if module_name:
|
38
|
+
file_path = "/tmp/dynamic_%s_%s.py" % (module_name, str_random)
|
39
|
+
else:
|
40
|
+
file_path = "/tmp/dynamic_%s.py" % str_random
|
41
|
+
self.__write_tmp_py(file_path, func_string)
|
42
|
+
modules = self.load_from_file(file_path)
|
43
|
+
except Exception as ex:
|
44
|
+
logger.debug("Error: %s" % ex)
|
45
|
+
logger.debug(traceback.format_exc())
|
46
|
+
return modules
|
@@ -0,0 +1,151 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
import importlib
|
3
|
+
import sys
|
4
|
+
import traceback
|
5
|
+
try:
|
6
|
+
from urllib import parse
|
7
|
+
except Exception as ex:
|
8
|
+
import urllib as parse
|
9
|
+
|
10
|
+
from log4python.Log4python import log
|
11
|
+
from sqlalchemy import Engine
|
12
|
+
from sqlalchemy import create_engine
|
13
|
+
from sqlalchemy.sql import text
|
14
|
+
|
15
|
+
importlib.reload(sys)
|
16
|
+
logger = log("MysqlHelper")
|
17
|
+
import pymysql
|
18
|
+
|
19
|
+
pymysql.version_info = (1, 4, 13, "final", 0)
|
20
|
+
pymysql.install_as_MySQLdb()
|
21
|
+
|
22
|
+
|
23
|
+
class MysqlHelper:
|
24
|
+
def __init__(self, database_config=None):
|
25
|
+
self.mysql_config = database_config
|
26
|
+
self.__engine = None
|
27
|
+
|
28
|
+
def get_mysql_client(self, config_user=None) -> Engine:
|
29
|
+
'''
|
30
|
+
mysql_db = 'mysql://root:***@10.89.189.48:8027/log_etl'
|
31
|
+
'''
|
32
|
+
engine = None
|
33
|
+
try:
|
34
|
+
if self.__engine is None:
|
35
|
+
if config_user:
|
36
|
+
config_init = config_user
|
37
|
+
else:
|
38
|
+
config_init = self.mysql_config
|
39
|
+
mysql_db = 'mysql://%s:%s@%s:%s/%s?charset=utf8' % (config_init['user_name'],
|
40
|
+
parse.quote_plus(config_init['password']),
|
41
|
+
config_init['host'],
|
42
|
+
config_init['port'],
|
43
|
+
config_init['db_name']
|
44
|
+
)
|
45
|
+
engine = create_engine(mysql_db, echo=False, pool_recycle=3600, pool_pre_ping=True)
|
46
|
+
self.__engine = engine
|
47
|
+
else:
|
48
|
+
engine = self.__engine
|
49
|
+
except Exception as ex:
|
50
|
+
logger.error(traceback.format_exc())
|
51
|
+
logger.error('deal_msg error: %s' % ex)
|
52
|
+
return engine
|
53
|
+
|
54
|
+
def execute(self, sql):
|
55
|
+
try:
|
56
|
+
# 创建数据库引擎
|
57
|
+
engine = self.get_mysql_client()
|
58
|
+
# 定义SQL查询语句
|
59
|
+
execute_stmt = text(sql)
|
60
|
+
with engine.connect() as connection:
|
61
|
+
try:
|
62
|
+
result = connection.execute(execute_stmt)
|
63
|
+
connection.commit()
|
64
|
+
except Exception as e:
|
65
|
+
logger.error(traceback.format_exc())
|
66
|
+
logger.error('deal_msg error: %s' % e)
|
67
|
+
connection.rollback()
|
68
|
+
except Exception as ex:
|
69
|
+
logger.error(traceback.format_exc())
|
70
|
+
logger.error('deal_msg error: %s' % ex)
|
71
|
+
|
72
|
+
def query(self, sql) -> list[dict]:
|
73
|
+
try:
|
74
|
+
# 创建数据库引擎
|
75
|
+
engine = self.get_mysql_client()
|
76
|
+
|
77
|
+
# 定义SQL查询语句
|
78
|
+
select_stmt = text(sql)
|
79
|
+
|
80
|
+
# 使用连接执行查询
|
81
|
+
result_list = []
|
82
|
+
with engine.connect() as connection:
|
83
|
+
result = connection.execute(select_stmt)
|
84
|
+
|
85
|
+
# 将查询结果转换为字典列表
|
86
|
+
# 使用fetchall()获取所有结果行,每行是一个元组
|
87
|
+
# 使用namedtuple来获取列名和数据的对应关系
|
88
|
+
columns = result.keys()
|
89
|
+
result_list = [dict(zip(columns, row)) for row in result]
|
90
|
+
except Exception as ex:
|
91
|
+
logger.error(traceback.format_exc())
|
92
|
+
logger.error('deal_msg error: %s' % ex)
|
93
|
+
return result_list
|
94
|
+
|
95
|
+
@staticmethod
|
96
|
+
def get_insert_schema(filed_names_list):
|
97
|
+
schema_final = ""
|
98
|
+
val_data = ""
|
99
|
+
for item in filed_names_list:
|
100
|
+
schema_final = schema_final + "%s, " % item.strip()
|
101
|
+
val_data = val_data + ":%s, " % item.strip()
|
102
|
+
schema_final = schema_final.strip(" ,")
|
103
|
+
val_data = val_data.strip(" ,")
|
104
|
+
return schema_final, val_data
|
105
|
+
|
106
|
+
def bulk_insert(self, table_name: str, columns: list, data_list: list, batch_size=20000, columns_on_duplicate=[]):
|
107
|
+
"""
|
108
|
+
执行分批批量插入操作的通用函数。
|
109
|
+
|
110
|
+
:param columns_on_duplicate: 唯一键冲突时覆盖
|
111
|
+
:param table_name: 要插入数据的表名
|
112
|
+
:param columns: 表的列名列表
|
113
|
+
:param data_list: 要插入的数据列表,其中每个元素是一个包含列值的元组
|
114
|
+
:param batch_size: 每批插入的数据量
|
115
|
+
"""
|
116
|
+
# 创建数据库引擎
|
117
|
+
engine = self.get_mysql_client()
|
118
|
+
|
119
|
+
on_duplicate_sql = ""
|
120
|
+
if columns_on_duplicate:
|
121
|
+
tmp_list = []
|
122
|
+
for item in columns_on_duplicate:
|
123
|
+
tmp_list.append("%s=VALUES(%s)" % (item, item))
|
124
|
+
if tmp_list:
|
125
|
+
on_duplicate_sql = "ON DUPLICATE KEY UPDATE %s" % ", ".join(tmp_list)
|
126
|
+
|
127
|
+
# 准备批量插入的SQL语句模板
|
128
|
+
schema_final, placeholders = self.get_insert_schema(columns)
|
129
|
+
insert_stmt = text(f"INSERT INTO {table_name} ({schema_final}) VALUES ({placeholders}) {on_duplicate_sql}")
|
130
|
+
logger.info("BulkInsert:[%s]" % str(insert_stmt))
|
131
|
+
|
132
|
+
try:
|
133
|
+
# 使用连接执行分批批量插入
|
134
|
+
with engine.connect() as connection:
|
135
|
+
try:
|
136
|
+
for i in range(0, len(data_list), batch_size):
|
137
|
+
# 获取当前批次的数据
|
138
|
+
batch_data = data_list[i:i + batch_size]
|
139
|
+
# 执行批量插入
|
140
|
+
connection.execute(insert_stmt, batch_data)
|
141
|
+
logger.info(f"Batch inserted {len(batch_data)} records.")
|
142
|
+
|
143
|
+
connection.commit()
|
144
|
+
logger.debug(f"Successfully inserted all data into {table_name},inserted {len(batch_data)} records.")
|
145
|
+
except Exception as e:
|
146
|
+
logger.error(traceback.format_exc())
|
147
|
+
logger.error('deal_msg error: %s' % e)
|
148
|
+
connection.rollback()
|
149
|
+
except Exception as ex:
|
150
|
+
logger.error(traceback.format_exc())
|
151
|
+
logger.error('deal_msg error: %s' % ex)
|
File without changes
|