优化应用安装逻辑
This commit is contained in:
parent
916ec1b69c
commit
e9fafbc969
@ -120,7 +120,7 @@ def analyze_package(temp_dir: str) -> Dict[str, Any]:
|
||||
|
||||
@handle_errors
|
||||
def install_files(temp_dir: str, app_name: str, package_info: Dict[str, Any], is_backend: bool) -> Dict[str, Any]:
|
||||
"""安装文件到指定目录"""
|
||||
"""安装文件到指定目录 - 直接复制整个目录结构,更简单高效"""
|
||||
apps_dir, _ = get_app_directories()
|
||||
app_dir = apps_dir / app_name
|
||||
|
||||
@ -129,60 +129,60 @@ def install_files(temp_dir: str, app_name: str, package_info: Dict[str, Any], is
|
||||
shutil.rmtree(app_dir)
|
||||
app_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 创建app_name和frontend子目录
|
||||
backend_dir = app_dir / app_name
|
||||
frontend_dir = app_dir / "frontend"
|
||||
backend_dir.mkdir(parents=True, exist_ok=True)
|
||||
frontend_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 获取根目录
|
||||
root_dir = package_info.get('root_dir', temp_dir)
|
||||
copied_files = []
|
||||
|
||||
# 复制后端文件
|
||||
# 直接复制整个目录结构
|
||||
if package_info.get('has_backend', False):
|
||||
for file_path in package_info.get('backend_files', []):
|
||||
# file_path 是相对于 root_dir 的路径
|
||||
# 例如:jin/hooks.py
|
||||
# root_dir 是 tmp/app_install_xxx/jin
|
||||
# 所以 src_path = tmp/app_install_xxx/jin/jin/hooks.py
|
||||
src_path = os.path.join(root_dir, file_path)
|
||||
|
||||
# dst_path 需要去掉 app_name 前缀
|
||||
if file_path.startswith(f"{app_name}/"):
|
||||
dst_path = backend_dir / file_path[len(app_name) + 1:]
|
||||
else:
|
||||
dst_path = backend_dir / file_path
|
||||
|
||||
if os.path.exists(src_path):
|
||||
dst_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(src_path, dst_path)
|
||||
copied_files.append(str(dst_path))
|
||||
else:
|
||||
log_error(f"源文件不存在: {src_path}, root_dir={root_dir}, file_path={file_path}")
|
||||
backend_dir = app_dir / app_name
|
||||
backend_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 检查 root_dir 下是否有 app_name 子目录(解压后可能有两层app_name)
|
||||
inner_app_dir = os.path.join(root_dir, app_name)
|
||||
if os.path.exists(inner_app_dir) and os.path.isdir(inner_app_dir):
|
||||
# 如果存在内层 app_name 目录,复制它
|
||||
log_info(f"复制内层应用目录: {inner_app_dir} -> {backend_dir}")
|
||||
for item in os.listdir(inner_app_dir):
|
||||
src = os.path.join(inner_app_dir, item)
|
||||
dst = backend_dir / item
|
||||
if os.path.isdir(src):
|
||||
shutil.copytree(src, dst, dirs_exist_ok=True)
|
||||
else:
|
||||
shutil.copy2(src, dst)
|
||||
else:
|
||||
# 否则直接复制 root_dir
|
||||
log_info(f"复制应用目录: {root_dir} -> {backend_dir}")
|
||||
for item in os.listdir(root_dir):
|
||||
# 跳过不需要的文件
|
||||
if item in ['__pycache__', '.git']:
|
||||
continue
|
||||
src = os.path.join(root_dir, item)
|
||||
dst = backend_dir / item
|
||||
if os.path.isdir(src):
|
||||
shutil.copytree(src, dst, dirs_exist_ok=True)
|
||||
else:
|
||||
shutil.copy2(src, dst)
|
||||
|
||||
# 复制前端文件
|
||||
# 复制前端目录
|
||||
if package_info.get('has_frontend', False):
|
||||
for file_path in package_info.get('frontend_files', []):
|
||||
src_path = os.path.join(root_dir, file_path)
|
||||
|
||||
# dst_path 需要去掉 app_name 前缀
|
||||
if file_path.startswith(f"{app_name}/"):
|
||||
dst_path = frontend_dir / file_path[len(app_name) + 1:]
|
||||
else:
|
||||
dst_path = frontend_dir / file_path
|
||||
|
||||
if os.path.exists(src_path):
|
||||
dst_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(src_path, dst_path)
|
||||
copied_files.append(str(dst_path))
|
||||
else:
|
||||
log_error(f"源文件不存在: {src_path}, root_dir={root_dir}, file_path={file_path}")
|
||||
frontend_dir = app_dir / "frontend"
|
||||
frontend_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
frontend_src = os.path.join(root_dir, "frontend")
|
||||
if os.path.exists(frontend_src):
|
||||
log_info(f"复制前端目录: {frontend_src} -> {frontend_dir}")
|
||||
for item in os.listdir(frontend_src):
|
||||
src = os.path.join(frontend_src, item)
|
||||
dst = frontend_dir / item
|
||||
if os.path.isdir(src):
|
||||
shutil.copytree(src, dst, dirs_exist_ok=True)
|
||||
else:
|
||||
shutil.copy2(src, dst)
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'app_dir': str(app_dir),
|
||||
'copied_files': copied_files
|
||||
'copied_files': ['...'] # 目录复制不记录单个文件
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user