clean code

This commit is contained in:
chenxudong 2025-08-08 15:35:45 +08:00
parent 0f2077670d
commit bfb95cc372
7 changed files with 27 additions and 13 deletions

View File

@ -7,9 +7,9 @@ from loguru import logger
mariadb_port = 3417 mariadb_port = 3417
mariadb_passowrd = '1qaz@WSX' mariadb_passowrd = '1qaz@WSX'
mariadb_user='root' mariadb_user = 'root'
mariadb_init_schema = 'em_data_prod' mariadb_init_schema = 'em_data_prod'
comac_db_port=12396 comac_db_port = 12396
class MyCustomError(Exception): class MyCustomError(Exception):
@ -19,6 +19,7 @@ class MyCustomError(Exception):
# 调用基类的构造函数 # 调用基类的构造函数
super().__init__(message) super().__init__(message)
def ensure_dir(directory): def ensure_dir(directory):
if not os.path.exists(directory): if not os.path.exists(directory):
os.makedirs(directory) os.makedirs(directory)
@ -87,8 +88,8 @@ def start_service_if_not_running(service_name):
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):
def delete_old_files(directory, days=2):
if not os.path.exists(directory): if not os.path.exists(directory):
return return
@ -117,4 +118,4 @@ def delete_old_files(directory, days=2):
logger.info(f"\n操作完成!共删除 {deleted_count} 个日志文件。") logger.info(f"\n操作完成!共删除 {deleted_count} 个日志文件。")
except Exception as e: except Exception as e:
logger.info(f"遍历目录时出错: {str(e)}", file=sys.stderr) logger.info(f"遍历目录时出错: {str(e)}", file=sys.stderr)

View File

@ -59,7 +59,8 @@ class InstallMariaDb:
if __name__ == '__main__': if __name__ == '__main__':
current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname(os.path.abspath(__file__)) current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname(
os.path.abspath(__file__))
log_dir = os.path.join(current_dir, "logs") log_dir = os.path.join(current_dir, "logs")
ensure_dir(log_dir) ensure_dir(log_dir)
logger.add( logger.add(

View File

@ -1,4 +1,5 @@
import shutil import shutil
from Common import * from Common import *
current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname( current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname(
@ -8,6 +9,7 @@ app_log_dir = os.path.join(current_dir, "logs")
sql_path = get_resource_path(os.path.join("datas", "init.sql")) sql_path = get_resource_path(os.path.join("datas", "init.sql"))
jar_path = get_resource_path(os.path.join("datas", "electromagnetic.jar")) jar_path = get_resource_path(os.path.join("datas", "electromagnetic.jar"))
def set_sql(): def set_sql():
with open(sql_path, 'r', encoding='utf-8') as file: with open(sql_path, 'r', encoding='utf-8') as file:
lines = file.readlines() lines = file.readlines()
@ -33,12 +35,14 @@ def set_sql():
logger.info("sql设置成功") logger.info("sql设置成功")
pass pass
def set_jar(): def set_jar():
dest_jar_path = os.path.join(current_dir, "electromagnetic.jar") dest_jar_path = os.path.join(current_dir, "electromagnetic.jar")
shutil.copy(jar_path, dest_jar_path) shutil.copy(jar_path, dest_jar_path)
logger.info("jar设置成功") logger.info("jar设置成功")
pass pass
def update_service(cmd): def update_service(cmd):
command = [manage_service_exe, cmd] command = [manage_service_exe, cmd]
result = subprocess.run(command, capture_output=True, text=True) result = subprocess.run(command, capture_output=True, text=True)
@ -69,4 +73,4 @@ if __name__ == '__main__':
update_service("install") update_service("install")
update_service("start") update_service("start")
pass pass

View File

@ -4,6 +4,7 @@ import servicemanager
import win32event import win32event
import win32service import win32service
import win32serviceutil import win32serviceutil
from Common import * from Common import *
current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname( current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname(
@ -14,6 +15,7 @@ java_exe = os.path.join(current_dir, "jdk", "bin", "java.exe")
jar_path = os.path.join(current_dir, "electromagnetic.jar") jar_path = os.path.join(current_dir, "electromagnetic.jar")
app_log_dir = os.path.join(current_dir, "logs") app_log_dir = os.path.join(current_dir, "logs")
class ComacDBService(win32serviceutil.ServiceFramework): class ComacDBService(win32serviceutil.ServiceFramework):
_svc_name_ = service_name # 服务名称 _svc_name_ = service_name # 服务名称
_svc_display_name_ = service_name # 显示名称 _svc_display_name_ = service_name # 显示名称
@ -150,6 +152,7 @@ class ComacDBService(win32serviceutil.ServiceFramework):
# 报告服务已停止 # 报告服务已停止
self.ReportServiceStatus(win32service.SERVICE_STOPPED) self.ReportServiceStatus(win32service.SERVICE_STOPPED)
logger.info("Service stopped successfully") logger.info("Service stopped successfully")
pass pass
@ -176,4 +179,4 @@ if __name__ == '__main__':
pythonClassString=win32serviceutil.GetServiceClassString(ComacDBService), pythonClassString=win32serviceutil.GetServiceClassString(ComacDBService),
serviceName=service_name, serviceName=service_name,
startType=win32service.SERVICE_AUTO_START, startType=win32service.SERVICE_AUTO_START,
) )

View File

@ -4,11 +4,11 @@ import time
import pyzipper import pyzipper
from loguru import logger from loguru import logger
prjRootPath = os.getcwd() + fr"/szsd/data/ele/prj/" prjRootPath = os.getcwd() + fr"/szsd/data/ele/prj/"
srcDataPath = os.getcwd() + rf"/dev.zip" srcDataPath = os.getcwd() + rf"/dev.zip"
passwd = "Comac" passwd = "Comac"
def unzip_file(): def unzip_file():
with pyzipper.AESZipFile(srcDataPath) as zip_file: with pyzipper.AESZipFile(srcDataPath) as zip_file:
try: try:
@ -18,6 +18,7 @@ def unzip_file():
return False return False
pass pass
if __name__ == '__main__': if __name__ == '__main__':
if not os.path.exists(prjRootPath): if not os.path.exists(prjRootPath):
os.makedirs(prjRootPath) os.makedirs(prjRootPath)
@ -27,4 +28,4 @@ if __name__ == '__main__':
else: else:
logger.info("文件设置成功3秒钟后自动退出") logger.info("文件设置成功3秒钟后自动退出")
time.sleep(3) time.sleep(3)
pass pass

View File

@ -1,5 +1,6 @@
sql_path = rf"E:\workspace\szsd\database-win-pkg\src\datas\init.sql" sql_path = rf"E:\workspace\szsd\database-win-pkg\src\datas\init.sql"
def set_replace(): def set_replace():
with open(sql_path, 'r', encoding='utf-8') as file: with open(sql_path, 'r', encoding='utf-8') as file:
lines = file.readlines() lines = file.readlines()
@ -18,5 +19,6 @@ def set_replace():
with open(sql_path, 'w', encoding='utf-8') as file: with open(sql_path, 'w', encoding='utf-8') as file:
file.writelines(new_lines) file.writelines(new_lines)
if __name__ == '__main__': if __name__ == '__main__':
set_replace() set_replace()

View File

@ -1,9 +1,11 @@
import subprocess
import time
import os import os
from loguru import logger
import shutil import shutil
import subprocess
import sys import sys
import time
from loguru import logger
def delete_service(service_name): def delete_service(service_name):
stop_command = ["sc", "stop", service_name] stop_command = ["sc", "stop", service_name]