diff --git a/build.txt b/build.txt index ec7ce52..7592990 100644 --- a/build.txt +++ b/build.txt @@ -1,4 +1,5 @@ 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/nssm.exe:datas" --add-data "datas/electromagnetic.jar:datas" .\install\InstallComacDB.py -pyinstaller --onefile --add-data "install/Common.py:install" --add-data "datas/init.sql:datas" .\install\UpdateSql.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 .\install\Uninstall.py +pyinstaller --onefile .\install\SetFile.py \ No newline at end of file diff --git a/src/install/Common.py b/src/install/Common.py index bfa2d19..4b3b048 100644 --- a/src/install/Common.py +++ b/src/install/Common.py @@ -1,6 +1,7 @@ import os import subprocess import sys +import time from loguru import logger @@ -84,4 +85,36 @@ def start_service_if_not_running(service_name): logger.info(f"服务 {service_name} 启动成功") else: logger.info(f"启动服务 {service_name} 失败: {start_result.stderr}") - raise MyCustomError(rf"服务 {service_name} 启动失败") \ No newline at end of file + 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) \ No newline at end of file diff --git a/src/install/InstallComacDB.py b/src/install/InstallOrUpgradeComacDB.py similarity index 72% rename from src/install/InstallComacDB.py rename to src/install/InstallOrUpgradeComacDB.py index df6660f..1f219dd 100644 --- a/src/install/InstallComacDB.py +++ b/src/install/InstallOrUpgradeComacDB.py @@ -4,7 +4,7 @@ from datetime import datetime from Common import * -class InstallComacDb: +class InstallOrUpgradeComacDb: def __init__(self, current_dir): @@ -21,44 +21,27 @@ class InstallComacDb: self.nssm_exe = get_resource_path(os.path.join('datas', 'nssm.exe')) # Java和JAR文件路径 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) pass 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.__set_sql() self.__register_and_start_service() logger.info("运行完成,10秒钟后自动退出") time.sleep(10) pass - def __delete_old_files(self, directory, days=2): - # 计算时间阈值(当前时间 - 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) + def __set_sql(self): + self.__set_replace() + logger.info("设置数据") + command2 = fr'{self.run_dir}\mariadb\bin\mysql --no-defaults -u root -p{mariadb_passowrd} -P {mariadb_port} {mariadb_init_schema} < {self.sql_path}' + with os.popen(command2) as stream: + res2 = stream.read() + logger.info(res2) + pass def __register_and_start_service(self): # 服务配置 @@ -118,7 +101,26 @@ class InstallComacDb: time.sleep(5) 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__': current_dir = os.getcwd() - InstallComacDb(current_dir).start_comac_db() + InstallOrUpgradeComacDb(current_dir).start_comac_db() pass \ No newline at end of file diff --git a/src/install/SetFile.py b/src/install/SetFile.py new file mode 100644 index 0000000..61a44db --- /dev/null +++ b/src/install/SetFile.py @@ -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 \ No newline at end of file diff --git a/src/install/Uninstall.py b/src/install/Uninstall.py index 130679b..234c07a 100644 --- a/src/install/Uninstall.py +++ b/src/install/Uninstall.py @@ -18,16 +18,13 @@ def delete_service(service_name): pass -def start_uninstall(current_dir): +def start_uninstall(): logger.info("开始清理服务") service_names = {'ComacDatabase', 'ComacMariaDB'} for service in service_names: delete_service(service) - logger.info("开始清理数据") - for dir in current_dir: - shutil.rmtree(dir) - logger.info('清理完成,10秒钟后自动退出') - time.sleep(10) + logger.info("服务清理完成,请手动删除当前文件夹下的数据,5秒钟后自动退出") + time.sleep(5) pass @@ -38,7 +35,6 @@ if __name__ == '__main__': # 弹出确认对话框 response = messagebox.askyesno("确认卸载", "确定卸载?") if response: - current_dir = os.getcwd() - start_uninstall(current_dir) + start_uninstall() else: logger.info("卸载取消") diff --git a/src/install/UpdateSql.py b/src/install/UpdateSql.py deleted file mode 100644 index a29e4f9..0000000 --- a/src/install/UpdateSql.py +++ /dev/null @@ -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) -