From 6aa87d27394e3aa5c60c385d997882317a712883 Mon Sep 17 00:00:00 2001 From: chenxudong Date: Thu, 12 Jun 2025 17:28:58 +0800 Subject: [PATCH] reformate code --- src/ComacDBInstall.py | 19 ++++++++++++++----- src/install.py | 23 +++++++++++++++-------- src/start.py | 9 ++++++++- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/ComacDBInstall.py b/src/ComacDBInstall.py index ce7a2ce..999b800 100644 --- a/src/ComacDBInstall.py +++ b/src/ComacDBInstall.py @@ -17,6 +17,7 @@ def ensure_dir(directory): else: logger.info(f"目录 {directory} 已存在") + def get_resource_path(relative_path): """ 获取资源绝对路径,适用于开发环境和PyInstaller打包后 """ if hasattr(sys, '_MEIPASS'): @@ -27,6 +28,7 @@ def get_resource_path(relative_path): base_path = os.path.dirname(os.path.abspath(".")) return os.path.join(base_path, relative_path) + def check_service_exist(service_name): result = subprocess.run(['sc', 'query', service_name], capture_output=True, text=True) @@ -34,6 +36,7 @@ def check_service_exist(service_name): return False return True + def start_service(service_name): result = subprocess.run(['sc', 'query', service_name], capture_output=True, text=True) @@ -55,11 +58,13 @@ def start_service(service_name): logger.info(f"启动服务 {service_name} 失败: {start_result.stderr}") return False + def unzip_file(zip_file_path): new_path = get_resource_path(os.path.join('datas', 'mariadb.zip')) with zipfile.ZipFile(str(new_path), 'r') as zip_ref: zip_ref.extractall(zip_file_path) # 将文件解压到指定路径 + def start_service_if_not_running(service_name): result = subprocess.run(['sc', 'query', service_name], capture_output=True, text=True) @@ -87,6 +92,7 @@ class MyCustomError(Exception): # 调用基类的构造函数 super().__init__(message) + class InstallMariaDb: def __init__(self): @@ -118,8 +124,8 @@ class InstallMariaDb: self.__init_db() logger.info("设置Java运行环境") self.__set_java_env() - logger.info("生成脚本文件") - self.__set_bat() + # logger.info("生成脚本文件") + # self.__set_bat() logger.info("安装完成,10秒后自动退出") time.sleep(10) pass @@ -161,6 +167,7 @@ class InstallMariaDb: with zipfile.ZipFile(str(new_path), 'r') as zip_ref: zip_ref.extractall(self.root_path) # 将文件解压到指定路径 pass + # # def __set_bat(self): # new_path = get_resource_path(os.path.join('datas', 'start.bat')) @@ -168,13 +175,14 @@ class InstallMariaDb: pass + class InstallComacDb: def __init__(self): self.project_dir = get_resource_path(os.path.dirname(Path(__file__).parent.absolute())) self.app_log_dir = 'D:/database/logs' - self.comac_db_running_port = 12396 + self.comac_db_running_port = 12396 self.jar_path = get_resource_path(os.path.join("datas", "electromagnetic.jar")) self.new_java_path = 'D:/database/jdk/bin/java.exe' self.url = f'http://127.0.0.1:{self.comac_db_running_port}/index' @@ -187,6 +195,7 @@ class InstallComacDb: # Java和JAR文件路径 self.java_path = "D:/database/jdk/bin/java.exe" ensure_dir(self.app_log_dir) + pass def start_comac_db(self): @@ -227,7 +236,6 @@ class InstallComacDb: except Exception as e: logger.info(f"遍历目录时出错: {str(e)}", file=sys.stderr) - def __register_and_start_service(self): # 服务配置 # 构建NSSM安装命令 @@ -266,7 +274,8 @@ class InstallComacDb: subprocess.run(rf"net start {self.service_name}") time.sleep(10) + if __name__ == '__main__': InstallMariaDb().start_install_mariadb() InstallComacDb().start_comac_db() - pass \ No newline at end of file + pass diff --git a/src/install.py b/src/install.py index d47b2da..3638865 100644 --- a/src/install.py +++ b/src/install.py @@ -16,8 +16,9 @@ mariadb_data_path = r'D:\database\mariadb\data' init_sql = r'D:/database/mariadb/data/init.sql' mariadb_port = 3417 init_schema = 'em_data_prod' -db_running_port=12396 -db_running_service_name="ComacDataBase" +db_running_port = 12396 +db_running_service_name = "ComacDataBase" + def ensure_dir(directory): if not os.path.exists(directory): @@ -26,11 +27,13 @@ def ensure_dir(directory): else: loguru.logger.info(f"目录 {directory} 已存在") + def unzip_file(): new_path = get_resource_path(os.path.join('datas', 'mariadb.zip')) with zipfile.ZipFile(str(new_path), 'r') as zip_ref: zip_ref.extractall(root_path) # 将文件解压到指定路径 + def register_service(): args1 = root_path + '/mariadb/bin/mysql_install_db.exe' args2 = '--datadir=' + mariadb_data_path @@ -41,12 +44,14 @@ def register_service(): loguru.logger.info(res.stdout) return res + def start_mariadb(): command = 'net start ' + service_name with os.popen(command) as stream: res = stream.read() loguru.logger.info(res) + def init_db(): command2 = fr'{root_path}\mariadb\bin\mysql --no-defaults -u root -p{passowrd} -P {mariadb_port} {init_schema} < {init_sql}' with os.popen(command2) as stream: @@ -55,6 +60,7 @@ def init_db(): os.remove(init_sql) pass + def init_db_user(): new_path = get_resource_path(os.path.join('datas', 'init.sql')) shutil.copy(str(new_path), mariadb_data_path) @@ -63,6 +69,7 @@ def init_db_user(): res = stream.read() loguru.logger.info(res) + def check_service(): """检查服务是否存在及运行状态""" try: @@ -82,6 +89,7 @@ def check_service(): loguru.logger.info(f"检测服务时出错: {str(e)}") return False + def get_resource_path(relative_path): """ 获取资源绝对路径,适用于开发环境和PyInstaller打包后 """ if hasattr(sys, '_MEIPASS'): @@ -92,16 +100,19 @@ def get_resource_path(relative_path): base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) + def set_java_env(): new_path = get_resource_path(os.path.join('datas', 'jdk.zip')) with zipfile.ZipFile(str(new_path), 'r') as zip_ref: zip_ref.extractall(root_path) # 将文件解压到指定路径 pass + def set_bat(): new_path = get_resource_path(os.path.join('datas', 'start.bat')) shutil.copy(str(new_path), root_path) + def start_install(): status = check_service() if status: @@ -126,6 +137,7 @@ def start_install(): loguru.logger.info("安装完成,10秒后自动退出") time.sleep(10) + def check_service_exists(query_service_name): try: output = subprocess.check_output(['sc', 'query', query_service_name], stderr=subprocess.STDOUT) @@ -135,16 +147,13 @@ def check_service_exists(query_service_name): return False raise -def start_app(): +def start_app(): # 首先检查有没有数据库运行的服务名,如果没有,则需要创建, - pass - - def kill_process_by_port(run_port): # 执行netstat命令获取端口占用信息 cmd_netstat = ['netstat', '-ano', '|', 'findstr', fr':{run_port}'] @@ -163,8 +172,6 @@ def kill_process_by_port(run_port): loguru.logger.info(f"端口 {run_port} 未被占用") - - if __name__ == '__main__': elevate.elevate() start_install() diff --git a/src/start.py b/src/start.py index dacb64f..4f33ef7 100644 --- a/src/start.py +++ b/src/start.py @@ -18,6 +18,7 @@ port = 12396 jar_path = os.path.join("datas", "electromagnetic.jar") url = f'http://127.0.0.1:{port}/index' + def ensure_dir(directory): if not os.path.exists(directory): os.makedirs(directory) @@ -25,6 +26,7 @@ def ensure_dir(directory): else: logger.info(f"目录 {directory} 已存在") + def get_resource_path(relative_path): """ 获取资源绝对路径,适用于开发环境和PyInstaller打包后 """ if hasattr(sys, '_MEIPASS'): @@ -35,6 +37,7 @@ def get_resource_path(relative_path): base_path = os.path.dirname(os.path.abspath(".")) return os.path.join(base_path, relative_path) + def delete_old_files(directory, days=2): # 计算时间阈值(当前时间 - days天) threshold_time = time.time() - days * 24 * 60 * 60 @@ -64,10 +67,12 @@ def delete_old_files(directory, days=2): except Exception as e: logger.info(f"遍历目录时出错: {str(e)}", file=sys.stderr) + def format_time(timestamp): """将时间戳格式化为可读字符串""" return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S') + def start(): try: ensure_dir(app_log_dir) @@ -93,6 +98,7 @@ def start(): logger.info(f"启动失败: {str(e)}") sys.exit(1) + def kill_process_by_port(run_port): # 执行netstat命令获取端口占用信息 cmd_netstat = ['netstat', '-ano', '|', 'findstr', fr':{run_port}'] @@ -117,6 +123,7 @@ def open_web(): webview.create_window('数据库管理系统', url=url) webview.start(debug=True) + if __name__ == '__main__': kill_process_by_port(port) @@ -134,4 +141,4 @@ if __name__ == '__main__': else: logger.warning(f"请打开浏览器,输入 {url} 访问使用,使用期间,请不用关闭此窗口!") while True: - pass \ No newline at end of file + pass