更新版本为v1.0.3

This commit is contained in:
chenxudong 2025-07-09 14:49:55 +08:00
parent 4518f218bd
commit c94fe45653
6 changed files with 103 additions and 88 deletions

View File

@ -1,4 +1,5 @@
cd src cd src
pyinstaller --onefile --add-data "install/Common.py:install" --add-data "datas/init.sql:datas" .\install\InstallMariaDB.py pyinstaller --onefile --add-data "install/Common.py:install" --add-data "datas/init.sql:datas" .\install\InstallMariaDB.py
pyinstaller --onefile --add-data "install/Common.py:install" --add-data "datas/nssm.exe:datas" --add-data "datas/electromagnetic.jar:datas" .\install\InstallComacDB.py pyinstaller --onefile --add-data "install/Common.py:install" --add-data "datas/nssm.exe:datas" --add-data "datas/electromagnetic.jar:datas" --add-data "datas/init.sql:datas" .\install\InstallOrUpgradeComacDB.py
pyinstaller --onefile --add-data "install/Common.py:install" --add-data "datas/init.sql:datas" .\install\UpdateSql.py pyinstaller --onefile .\install\Uninstall.py
pyinstaller --onefile .\install\SetFile.py

View File

@ -1,6 +1,7 @@
import os import os
import subprocess import subprocess
import sys import sys
import time
from loguru import logger from loguru import logger
@ -85,3 +86,35 @@ def start_service_if_not_running(service_name):
else: else:
logger.info(f"启动服务 {service_name} 失败: {start_result.stderr}") logger.info(f"启动服务 {service_name} 失败: {start_result.stderr}")
raise MyCustomError(rf"服务 {service_name} 启动失败") raise MyCustomError(rf"服务 {service_name} 启动失败")
def delete_old_files(directory, days=2):
if not os.path.exists(directory):
return
# 计算时间阈值(当前时间 - days天
threshold_time = time.time() - days * 24 * 60 * 60
deleted_count = 0
try:
# 遍历目录中的文件
for filename in os.listdir(directory):
filepath = os.path.join(directory, filename)
# 确保是文件而不是目录
if os.path.isfile(filepath):
try:
# 获取文件创建时间Windows系统
creation_time = os.path.getctime(filepath)
# 检查文件是否超过阈值
if creation_time < threshold_time:
# 删除文件
os.remove(filepath)
deleted_count += 1
logger.info(f"已删除: {filename}")
except Exception as e:
logger.info(f"处理文件 {filename} 时出错: {str(e)}", file=sys.stderr)
logger.info(f"\n操作完成!共删除 {deleted_count} 个日志文件。")
except Exception as e:
logger.info(f"遍历目录时出错: {str(e)}", file=sys.stderr)

View File

@ -4,7 +4,7 @@ from datetime import datetime
from Common import * from Common import *
class InstallComacDb: class InstallOrUpgradeComacDb:
def __init__(self, current_dir): def __init__(self, current_dir):
@ -21,44 +21,27 @@ class InstallComacDb:
self.nssm_exe = get_resource_path(os.path.join('datas', 'nssm.exe')) self.nssm_exe = get_resource_path(os.path.join('datas', 'nssm.exe'))
# Java和JAR文件路径 # Java和JAR文件路径
self.java_path = os.path.join(current_dir, "jdk", "bin", "java.exe") self.java_path = os.path.join(current_dir, "jdk", "bin", "java.exe")
self.sql_path = get_resource_path(os.path.join("datas", "init.sql"))
ensure_dir(self.app_log_dir) ensure_dir(self.app_log_dir)
pass pass
def start_comac_db(self): def start_comac_db(self):
self.__delete_old_files(self.app_log_dir) delete_old_files(self.app_log_dir, 2)
self.__remove_pre_service() self.__remove_pre_service()
self.__set_sql()
self.__register_and_start_service() self.__register_and_start_service()
logger.info("运行完成10秒钟后自动退出") logger.info("运行完成10秒钟后自动退出")
time.sleep(10) time.sleep(10)
pass pass
def __delete_old_files(self, directory, days=2): def __set_sql(self):
# 计算时间阈值(当前时间 - days天 self.__set_replace()
threshold_time = time.time() - days * 24 * 60 * 60 logger.info("设置数据")
deleted_count = 0 command2 = fr'{self.run_dir}\mariadb\bin\mysql --no-defaults -u root -p{mariadb_passowrd} -P {mariadb_port} {mariadb_init_schema} < {self.sql_path}'
try: with os.popen(command2) as stream:
# 遍历目录中的文件 res2 = stream.read()
for filename in os.listdir(directory): logger.info(res2)
filepath = os.path.join(directory, filename) pass
# 确保是文件而不是目录
if os.path.isfile(filepath):
try:
# 获取文件创建时间Windows系统
creation_time = os.path.getctime(filepath)
# 检查文件是否超过阈值
if creation_time < threshold_time:
# 删除文件
os.remove(filepath)
deleted_count += 1
logger.info(f"已删除: {filename}")
except Exception as e:
logger.info(f"处理文件 {filename} 时出错: {str(e)}", file=sys.stderr)
logger.info(f"\n操作完成!共删除 {deleted_count} 个日志文件。")
except Exception as e:
logger.info(f"遍历目录时出错: {str(e)}", file=sys.stderr)
def __register_and_start_service(self): def __register_and_start_service(self):
# 服务配置 # 服务配置
@ -118,7 +101,26 @@ class InstallComacDb:
time.sleep(5) time.sleep(5)
pass pass
def __set_replace(self):
with open(self.sql_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
modified = False
new_lines = []
for line in lines:
if 'CREATE TABLE' in line and 'IF NOT EXISTS' not in line:
new_line = line.replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS ')
new_lines.append(new_line)
modified = True
else:
new_lines.append(line)
# 如果有修改,则写回文件
if modified:
with open(self.sql_path, 'w', encoding='utf-8') as file:
file.writelines(new_lines)
if __name__ == '__main__': if __name__ == '__main__':
current_dir = os.getcwd() current_dir = os.getcwd()
InstallComacDb(current_dir).start_comac_db() InstallOrUpgradeComacDb(current_dir).start_comac_db()
pass pass

30
src/install/SetFile.py Normal file
View File

@ -0,0 +1,30 @@
import os
import time
import pyzipper
from loguru import logger
prjRootPath = os.getcwd() + fr"/szsd/data/ele/prj/"
srcDataPath = os.getcwd() + rf"/dev.zip"
passwd = "Comac"
def unzip_file():
with pyzipper.AESZipFile(srcDataPath) as zip_file:
try:
zip_file.extractall(prjRootPath, pwd=passwd.encode('utf-8'))
return True
except RuntimeError:
return False
pass
if __name__ == '__main__':
if not os.path.exists(prjRootPath):
os.makedirs(prjRootPath)
statuas = unzip_file()
if not statuas:
logger.error("文件解压错误")
else:
logger.info("文件设置成功3秒钟后自动退出")
time.sleep(3)
pass

View File

@ -18,16 +18,13 @@ def delete_service(service_name):
pass pass
def start_uninstall(current_dir): def start_uninstall():
logger.info("开始清理服务") logger.info("开始清理服务")
service_names = {'ComacDatabase', 'ComacMariaDB'} service_names = {'ComacDatabase', 'ComacMariaDB'}
for service in service_names: for service in service_names:
delete_service(service) delete_service(service)
logger.info("开始清理数据") logger.info("服务清理完成请手动删除当前文件夹下的数据5秒钟后自动退出")
for dir in current_dir: time.sleep(5)
shutil.rmtree(dir)
logger.info('清理完成10秒钟后自动退出')
time.sleep(10)
pass pass
@ -38,7 +35,6 @@ if __name__ == '__main__':
# 弹出确认对话框 # 弹出确认对话框
response = messagebox.askyesno("确认卸载", "确定卸载?") response = messagebox.askyesno("确认卸载", "确定卸载?")
if response: if response:
current_dir = os.getcwd() start_uninstall()
start_uninstall(current_dir)
else: else:
logger.info("卸载取消") logger.info("卸载取消")

View File

@ -1,47 +0,0 @@
import shutil
import time
from Common import *
class UpdateSql:
def __init__(self, install_path):
self.root_path = install_path
self.mariadb_data_path = os.path.join(install_path, 'mariadb', 'data')
self.init_sql = get_resource_path(os.path.join('datas', 'init.sql'))
self.__set_replace()
def __set_replace(self):
with open(self.init_sql, 'r', encoding='utf-8') as file:
lines = file.readlines()
modified = False
new_lines = []
for line in lines:
if 'CREATE TABLE' in line and 'IF NOT EXISTS' not in line:
new_line = line.replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS ')
new_lines.append(new_line)
modified = True
else:
new_lines.append(line)
# 如果有修改,则写回文件
if modified:
with open(self.init_sql, 'w', encoding='utf-8') as file:
file.writelines(new_lines)
def init_db(self):
shutil.copy(str(self.init_sql), self.mariadb_data_path)
command2 = fr'{self.root_path}\mariadb\bin\mysql --no-defaults -u root -p{mariadb_passowrd} -P {mariadb_port} {mariadb_init_schema} < {self.mariadb_data_path}'
with os.popen(command2) as stream:
res2 = stream.read()
logger.info(res2)
os.remove(self.init_sql)
pass
if __name__ == '__main__':
current_dir = os.getcwd()
logger.info("开始更新")
UpdateSql(current_dir).init_db()
logger.info("完成更新3秒钟后退出")
time.sleep(3)