修复更新代码库的bug

This commit is contained in:
Robofish 2025-10-13 01:46:39 +08:00
parent 563ede007c
commit d3aabce4f5
5 changed files with 54 additions and 45 deletions

View File

@ -743,7 +743,7 @@ class bsp_gpio(QWidget):
self.project_path = project_path
self.available_list = self._get_all_gpio_list()
# 加载描述
describe_path = os.path.join(os.path.dirname(__file__), "../../assets/User_code/bsp/describe.csv")
describe_path = os.path.join(CodeGenerator.get_assets_dir("User_code/bsp"), "describe.csv")
self.descriptions = load_descriptions(describe_path)
self._init_ui()
self._load_config()
@ -982,7 +982,7 @@ class bsp_pwm(QWidget):
self.project_path = project_path
self.available_list = self._get_pwm_channels()
# 加载描述
describe_path = os.path.join(os.path.dirname(__file__), "../../assets/User_code/bsp/describe.csv")
describe_path = os.path.join(CodeGenerator.get_assets_dir("User_code/bsp"), "describe.csv")
self.descriptions = load_descriptions(describe_path)
self._init_ui()
self._load_config()
@ -1236,7 +1236,7 @@ class bsp(QWidget):
def generate_bsp(project_path, pages):
"""生成所有BSP代码"""
# 自动添加 bsp.h
src_bsp_h = os.path.join(os.path.dirname(__file__), "../../assets/User_code/bsp/bsp.h")
src_bsp_h = os.path.join(CodeGenerator.get_assets_dir("User_code/bsp"), "bsp.h")
dst_bsp_h = os.path.join(project_path, "User/bsp/bsp.h")
os.makedirs(os.path.dirname(dst_bsp_h), exist_ok=True)
if os.path.exists(src_bsp_h):

View File

@ -318,7 +318,8 @@ class component(QWidget):
components_to_generate.add(dep_name)
# 为没有对应页面但需要生成的依赖组件创建临时页面
user_code_dir = os.path.join(os.path.dirname(__file__), "../../assets/User_code")
from ..tools.code_generator import CodeGenerator
user_code_dir = CodeGenerator.get_assets_dir("User_code")
for comp_name in components_to_generate:
if comp_name not in component_pages:
# 创建临时组件页面

View File

@ -262,43 +262,31 @@ class DataInterface(QWidget):
self.stacked_layout.setCurrentWidget(self.home_page)
def update_user_template(self):
url = "http://gitea.qutrobot.top/robofish/MRobot/archive/User_code.zip"
local_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../assets/User_code")
try:
resp = requests.get(url, timeout=30)
resp.raise_for_status()
z = zipfile.ZipFile(io.BytesIO(resp.content))
if os.path.exists(local_dir):
shutil.rmtree(local_dir)
for member in z.namelist():
rel_path = os.path.relpath(member, z.namelist()[0])
if rel_path == ".":
continue
target_path = os.path.join(local_dir, rel_path)
if member.endswith('/'):
os.makedirs(target_path, exist_ok=True)
else:
os.makedirs(os.path.dirname(target_path), exist_ok=True)
with open(target_path, "wb") as f:
f.write(z.read(member))
from app.tools.update_code import update_code
def info_callback(parent):
InfoBar.success(
title="更新成功",
content="用户模板已更新到最新版本!",
parent=self,
parent=parent,
duration=2000
)
except Exception as e:
def error_callback(parent, msg):
InfoBar.error(
title="更新失败",
content=f"用户模板更新失败: {e}",
parent=self,
content=f"用户模板更新失败: {msg}",
parent=parent,
duration=3000
)
update_code(parent=self, info_callback=info_callback, error_callback=error_callback)
def show_user_code_files(self):
from app.tools.code_generator import CodeGenerator
file_tree = self.codegen_page.file_tree
file_tree.clear()
base_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../assets/User_code")
base_dir = CodeGenerator.get_assets_dir("User_code")
user_dir = os.path.join(self.project_path, "User")
sub_dirs = ["bsp", "component", "device", "module"]
@ -412,7 +400,8 @@ class DataInterface(QWidget):
def generate_code(self):
import shutil
base_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../assets/User_code")
from app.tools.code_generator import CodeGenerator
base_dir = CodeGenerator.get_assets_dir("User_code")
user_dir = os.path.join(self.project_path, "User")
copied = []
files = self.get_checked_files()
@ -563,8 +552,8 @@ class DataInterface(QWidget):
)
def generate_task_code(self, task_list):
base_dir = os.path.dirname(os.path.abspath(__file__))
template_dir = os.path.join(base_dir, "../assets/User_code/task")
from app.tools.code_generator import CodeGenerator
template_dir = CodeGenerator.get_assets_dir("User_code/task")
output_dir = os.path.join(self.project_path, "User", "task")
os.makedirs(output_dir, exist_ok=True)

View File

@ -63,16 +63,8 @@ class CodeGenerator:
@staticmethod
def get_template_dir():
"""获取模板目录路径,兼容打包环境"""
if getattr(sys, 'frozen', False):
# 打包后的环境
base_path = sys._MEIPASS
template_dir = os.path.join(base_path, "assets", "User_code", "bsp")
else:
# 开发环境
current_dir = os.path.dirname(os.path.abspath(__file__))
while os.path.basename(current_dir) != 'MRobot' and current_dir != '/':
current_dir = os.path.dirname(current_dir)
template_dir = os.path.join(current_dir, "assets", "User_code", "bsp")
# 使用统一的get_assets_dir方法来获取路径
template_dir = CodeGenerator.get_assets_dir("User_code/bsp")
print(f"模板目录路径: {template_dir}")
if not os.path.exists(template_dir):
@ -89,15 +81,23 @@ class CodeGenerator:
str: 完整的assets路径
"""
if getattr(sys, 'frozen', False):
# 打包后的环境
base_path = sys._MEIPASS
# 打包后的环境 - 使用可执行文件所在目录而不是临时目录
exe_dir = os.path.dirname(sys.executable)
assets_dir = os.path.join(exe_dir, "assets")
print(f"打包环境:尝试使用路径: {assets_dir}")
# 如果exe_dir/assets不存在尝试使用sys._MEIPASS作为后备
if not os.path.exists(assets_dir) and hasattr(sys, '_MEIPASS'):
base_path = getattr(sys, '_MEIPASS')
assets_dir = os.path.join(base_path, "assets")
print(f"后备路径: {assets_dir}")
else:
# 开发环境
current_dir = os.path.dirname(os.path.abspath(__file__))
while os.path.basename(current_dir) != 'MRobot' and current_dir != '/':
current_dir = os.path.dirname(current_dir)
assets_dir = os.path.join(current_dir, "assets")
print(f"开发环境:使用路径: {assets_dir}")
if sub_path:
full_path = os.path.join(assets_dir, sub_path)

View File

@ -1,4 +1,5 @@
import os
import sys
import requests
import zipfile
import io
@ -8,7 +9,25 @@ import time
def update_code(parent=None, info_callback=None, error_callback=None):
url = "http://gitea.qutrobot.top/robofish/MRobot/archive/User_code.zip"
local_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../assets/User_code")
# 使用与CodeGenerator.get_assets_dir相同的逻辑确定assets目录
if getattr(sys, 'frozen', False):
# 打包后的环境 - 使用可执行文件所在目录
exe_dir = os.path.dirname(sys.executable)
assets_dir = os.path.join(exe_dir, "assets")
print(f"更新代码:打包环境,使用路径: {assets_dir}")
# 如果exe_dir/assets不存在尝试使用相对路径作为后备
if not os.path.exists(assets_dir):
assets_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../assets")
print(f"更新代码:后备路径: {assets_dir}")
else:
# 开发环境
assets_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../assets")
print(f"更新代码:开发环境,使用路径: {assets_dir}")
local_dir = os.path.join(assets_dir, "User_code")
print(f"更新代码:最终目标目录: {local_dir}")
try:
# 下载远程代码库