acdr/acdr-ui/tootls/代码生成工具.py

141 lines
5.4 KiB
Python
Raw Normal View History

2024-09-12 03:55:35 +08:00
import os
import shutil
import pymysql
import tkinter as tk
from tkinter import filedialog, messagebox, StringVar
# 执行 SQL 文件
def execute_sql_file(sql_file_path, db_config):
try:
# 连接 MySQL 数据库
connection = pymysql.connect(
host=db_config['host'],
user=db_config['user'],
password=db_config['password'],
database=db_config['database'],
port=int(db_config['port'])
)
cursor = connection.cursor()
with open(sql_file_path, 'r', encoding='utf-8') as file:
sql_script = file.read()
cursor.execute(sql_script)
connection.commit()
messagebox.showinfo("SQL执行成功", f"SQL文件 {sql_file_path} 已成功执行")
except Exception as e:
messagebox.showerror("SQL执行失败", f"执行SQL文件时出错: {e}")
finally:
cursor.close()
connection.close()
# 处理解压后的文件
def handle_extracted_files(temp_extract_dir, output_dir, ui_dir, db_config):
for root_dir, dirs, files in os.walk(temp_extract_dir):
for file in files:
src_file_path = os.path.join(root_dir, file)
relative_path = os.path.relpath(root_dir, temp_extract_dir)
top_level_dir = relative_path.split(os.sep)[0] # 获取一级目录名
if "module" in top_level_dir:
dest_file_path = os.path.join(output_dir, relative_path, file)
os.makedirs(os.path.dirname(dest_file_path), exist_ok=True)
shutil.copy2(src_file_path, dest_file_path)
print(f"复制文件 {src_file_path}{dest_file_path}")
elif "sql" in top_level_dir and file == "sql.sql":
execute_sql_file(src_file_path, db_config)
print(f"执行SQL文件: {src_file_path}")
elif "vue" in top_level_dir:
dest_file_path = os.path.join(ui_dir, relative_path, file)
os.makedirs(os.path.dirname(dest_file_path), exist_ok=True)
shutil.copy2(src_file_path, dest_file_path)
print(f"复制文件 {src_file_path}{dest_file_path}")
# 选择zip文件
def select_zip_file():
zip_file_path.set(filedialog.askopenfilename(filetypes=[("ZIP files", "*.zip")]))
# 选择输出目录
def select_output_dir():
output_dir.set(filedialog.askdirectory())
# 选择UI目录
def select_ui_dir():
ui_dir.set(filedialog.askdirectory())
# 解压并处理文件
def extract_and_process_files():
if not zip_file_path.get() or not output_dir.get() or not ui_dir.get():
messagebox.showwarning("警告", "请确保选择了ZIP文件和目标目录")
return
# 设置数据库连接参数
db_config = {
'host': host_var.get() or 'localhost',
'user': user_var.get() or 'root',
'password': password_var.get() or 'root',
'database': db_var.get() or 'cwet',
'port': port_var.get() or 3306
}
# 解压ZIP文件
temp_extract_dir = os.path.join(output_dir.get(), "temp_extracted")
shutil.unpack_archive(zip_file_path.get(), temp_extract_dir)
# 处理解压后的文件
handle_extracted_files(temp_extract_dir, output_dir.get(), ui_dir.get(), db_config)
# 清理临时文件夹
shutil.rmtree(temp_extract_dir)
messagebox.showinfo("完成", "文件处理完成!")
# 创建主窗口
root = tk.Tk()
root.title("代码生成工具")
# 创建变量
zip_file_path = StringVar()
output_dir = StringVar()
ui_dir = StringVar()
# 数据库连接参数
host_var = StringVar(value='localhost')
user_var = StringVar(value='root')
password_var = StringVar(value='root')
db_var = StringVar(value='cwet')
port_var = StringVar(value='3306')
# GUI布局
tk.Label(root, text="选择ZIP文件:").grid(row=0, column=0, padx=10, pady=5)
tk.Entry(root, textvariable=zip_file_path, width=50).grid(row=0, column=1, padx=10, pady=5)
tk.Button(root, text="选择ZIP文件", command=select_zip_file).grid(row=0, column=2, padx=10, pady=5)
tk.Label(root, text="选择输出目录:").grid(row=1, column=0, padx=10, pady=5)
tk.Entry(root, textvariable=output_dir, width=50).grid(row=1, column=1, padx=10, pady=5)
tk.Button(root, text="选择输出目录", command=select_output_dir).grid(row=1, column=2, padx=10, pady=5)
tk.Label(root, text="选择UI目录:").grid(row=2, column=0, padx=10, pady=5)
tk.Entry(root, textvariable=ui_dir, width=50).grid(row=2, column=1, padx=10, pady=5)
tk.Button(root, text="选择UI目录", command=select_ui_dir).grid(row=2, column=2, padx=10, pady=5)
# 数据库设置
tk.Label(root, text="数据库主机:").grid(row=3, column=0, padx=10, pady=5)
tk.Entry(root, textvariable=host_var).grid(row=3, column=1, padx=10, pady=5)
tk.Label(root, text="用户名:").grid(row=4, column=0, padx=10, pady=5)
tk.Entry(root, textvariable=user_var).grid(row=4, column=1, padx=10, pady=5)
tk.Label(root, text="密码:").grid(row=5, column=0, padx=10, pady=5)
tk.Entry(root, textvariable=password_var).grid(row=5, column=1, padx=10, pady=5)
tk.Label(root, text="数据库名:").grid(row=6, column=0, padx=10, pady=5)
tk.Entry(root, textvariable=db_var).grid(row=6, column=1, padx=10, pady=5)
tk.Label(root, text="端口:").grid(row=7, column=0, padx=10, pady=5)
tk.Entry(root, textvariable=port_var).grid(row=7, column=1, padx=10, pady=5)
tk.Button(root, text="开始处理", command=extract_and_process_files).grid(row=8, column=1, padx=10, pady=20)
# 运行应用
root.mainloop()