feat: 增加项目一键整理、打包及自动上传夸克/百度网盘功能

This commit is contained in:
王鹏
2026-04-15 10:24:38 +08:00
parent 245f9cdf41
commit a6d79d9a14
9 changed files with 933 additions and 19 deletions

View File

@@ -1483,6 +1483,99 @@ class ProjectScreenshotAutomation:
except Exception as e:
self.log(f"截图收尾工作失败: {str(e)}")
def organize_and_zip_project(self, override_name: Optional[str] = None) -> Optional[str]:
"""
整理项目并打包成压缩包
将桌面路径下的code文件夹下的第一个子文件夹名称改成项目名称并打包
Args:
override_name: 如果提供,则使用此名称作为项目名称,否则从路径提取
Returns:
压缩包路径
"""
try:
self.log("=" * 50)
self.log("开始整理项目并打包...")
# 1. 确定项目名称
if override_name:
project_name = override_name
self.log(f"使用提供的项目名称: {project_name}")
elif not self.config.project_path:
self.log("未配置项目路径,且未提供覆盖名称,无法提取项目名称")
return None
else:
project_name = os.path.basename(self.config.project_path.rstrip('/\\'))
self.log(f"从路径提取项目名称: {project_name}")
# 去掉前后的空格
project_name = project_name.strip()
# 移除非法文件名字符
project_name = re.sub(r'[\\/:*?"<>|]', '_', project_name)
self.log(f"最终使用的项目名称: {project_name}")
# 2. 找到 code 文件夹
code_dir = os.path.join(self.config.desktop_path, "code")
if not os.path.exists(code_dir):
self.log(f"未找到 code 文件夹: {code_dir}")
return None
# 3. 找到 code 下的第一个子文件夹
subdirs = [d for d in os.listdir(code_dir) if os.path.isdir(os.path.join(code_dir, d))]
if not subdirs:
self.log(f"code 文件夹下没有子文件夹: {code_dir}")
return None
# 排除掉一些可能的非项目文件夹
# subdirs = [d for d in subdirs if d not in ['.git', '__pycache__']]
first_subdir = subdirs[0]
first_subdir_path = os.path.join(code_dir, first_subdir)
# 4. 重命名
new_subdir_path = os.path.join(code_dir, project_name)
# 如果新路径和旧路径不同,则重命名
if first_subdir != project_name:
if os.path.exists(new_subdir_path):
self.log(f"目标文件夹已存在,删除旧文件夹: {new_subdir_path}")
shutil.rmtree(new_subdir_path)
try:
os.rename(first_subdir_path, new_subdir_path)
self.log(f"重命名文件夹: {first_subdir} -> {project_name}")
except Exception as e:
self.log(f"重命名失败: {str(e)}")
return None
else:
self.log(f"文件夹已经是项目名称,无需重命名: {first_subdir}")
# 5. 打包成压缩包
zip_target_path = os.path.join(self.config.desktop_path, project_name)
self.log(f"开始打包到桌面: {project_name}.zip")
# make_archive(base_name, format, root_dir, base_dir)
# base_name 是输出文件的路径(不含后缀)
# root_dir 是要打包的根目录
# base_dir 是相对于 root_dir 的要打包的目录(包含在压缩包内的顶级目录)
try:
zip_file = shutil.make_archive(
base_name=zip_target_path,
format='zip',
root_dir=code_dir,
base_dir=project_name
)
self.log(f"打包完成: {zip_file}")
return zip_file
except Exception as e:
self.log(f"打包失败: {str(e)}")
return None
except Exception as e:
self.log(f"整理打包失败: {str(e)}")
return None
def run_full_flow(self) -> bool:
"""