refactor(pyinstaller): 优化打包配置和日志处理
重构打包配置,使用新的spec文件生成单个exe文件 修复日志处理中可能存在的None值问题 添加clean.py脚本用于清理打包残留文件 更新SKILL.md文档说明打包流程
This commit is contained in:
17
.trae/skills/pyinstaller-one/SKILL.md
Normal file
17
.trae/skills/pyinstaller-one/SKILL.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
---
|
||||||
|
name: pyinstaller-one
|
||||||
|
description: 当执行pyinstaller打包的时候,执行这个技能。
|
||||||
|
---
|
||||||
|
|
||||||
|
## 元数据
|
||||||
|
name: pyinstaller个性化打包
|
||||||
|
description: 打包的时候,要求生成为一个exe文件,使用ico等等
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
此 Skill 用于给有GUI界面的python代码,打包的时候,生成一个统一的要求:生成一个exe文件,去掉控制台窗口,使用本目录下的ico文件作为程序的图标。打包时,如果之前有打包过的文件(dist/build 文件夹),自动覆盖旧文件,不用手动确认,一键打包到底。
|
||||||
|
|
||||||
|
## 打包命令示例
|
||||||
|
pyinstaller --onefile --noconsole --icon=图标文件名.ico --distpath=. --hidden-import=PySide6.Qt6Compat python程序名.py
|
||||||
|
|
||||||
|
## 清除多余文件
|
||||||
|
在windows环境下,执行clean.py
|
||||||
17
.trae/skills/pyinstaller-one/clean.py
Normal file
17
.trae/skills/pyinstaller-one/clean.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import glob
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
# 2. 清理打包残留文件 等价:rd /s /q build + del /f *.spec
|
||||||
|
print("开始清理打包残留文件...")
|
||||||
|
# 删除build文件夹
|
||||||
|
if os.path.exists("build") and os.path.isdir("build"):
|
||||||
|
shutil.rmtree("build")
|
||||||
|
# 删除所有.spec文件
|
||||||
|
spec_files = glob.glob("*.spec")
|
||||||
|
for spec_file in spec_files:
|
||||||
|
if os.path.exists(spec_file):
|
||||||
|
os.remove(spec_file)
|
||||||
|
|
||||||
|
print("✅ 打包完成 + 残留文件清理完毕!exe文件已生成在当前目录")
|
||||||
12
main.py
12
main.py
@@ -274,6 +274,10 @@ def setup_logging(log_path: str, level: str = "INFO"):
|
|||||||
"""配置日志"""
|
"""配置日志"""
|
||||||
logger.remove() # 移除默认的处理器
|
logger.remove() # 移除默认的处理器
|
||||||
|
|
||||||
|
# 确保log_path不为None
|
||||||
|
if log_path is None:
|
||||||
|
log_path = 'guba.log'
|
||||||
|
|
||||||
logger.add(
|
logger.add(
|
||||||
log_path,
|
log_path,
|
||||||
rotation="10 MB",
|
rotation="10 MB",
|
||||||
@@ -283,6 +287,8 @@ def setup_logging(log_path: str, level: str = "INFO"):
|
|||||||
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}"
|
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 仅当sys.stdout可用时才添加控制台日志
|
||||||
|
if sys.stdout is not None:
|
||||||
logger.add(
|
logger.add(
|
||||||
sys.stdout,
|
sys.stdout,
|
||||||
level=level,
|
level=level,
|
||||||
@@ -304,8 +310,10 @@ def main():
|
|||||||
|
|
||||||
# 配置日志
|
# 配置日志
|
||||||
log_config = config.logging_config
|
log_config = config.logging_config
|
||||||
setup_logging(log_config.get('path', 'guba.log'), log_config.get('level', 'INFO'))
|
log_path = log_config.get('path') or 'guba.log'
|
||||||
logger.info(f"日志配置完成: {log_config.get('path', 'guba.log')}, 级别: {log_config.get('level', 'INFO')}")
|
log_level = log_config.get('level') or 'INFO'
|
||||||
|
setup_logging(log_path, log_level)
|
||||||
|
logger.info(f"日志配置完成: {log_path}, 级别: {log_level}")
|
||||||
|
|
||||||
# 初始化组件
|
# 初始化组件
|
||||||
logger.info("初始化数据库...")
|
logger.info("初始化数据库...")
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 531 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 531 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 54 KiB |
Reference in New Issue
Block a user