|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import git |
| 5 | +from concurrent.futures import ThreadPoolExecutor |
| 6 | +from gooey import Gooey, GooeyParser |
| 7 | + |
| 8 | +@Gooey( |
| 9 | + program_name="ComfyUI 强制更新工具", # 程序名称 |
| 10 | + language='chinese', |
| 11 | + progress_regex=r"^进度: (\d+)/(\d+)$", # 进度条正则表达式 |
| 12 | + progress_expr="x[0] / x[1] * 100", # 进度条显示格式 |
| 13 | + disable_progress_bar_animation=True, # 禁用进度条动画 |
| 14 | + timing_options={ |
| 15 | + 'show_time_remaining': True, |
| 16 | + }, |
| 17 | + clear_before_run=True, # 运行前清空控制台 |
| 18 | +) |
| 19 | +def main(): |
| 20 | + # 创建 Gooey 参数解析器 |
| 21 | + parser = GooeyParser( |
| 22 | + description="强制更新 ComfyUI 及其自定义节点,且将远程 GitHub 地址替换为国内镜像。\n\n" |
| 23 | + "提示:强制更新仅处理 Git 仓库, 不处理 Python 依赖包,\n" |
| 24 | + "后续可能需要在 Manager 中点击 Try Fix 修复个别节点。" |
| 25 | + ) |
| 26 | + |
| 27 | + # 添加镜像地址参数 |
| 28 | + parser.add_argument( |
| 29 | + 'mirror_url', # 参数名称 |
| 30 | + metavar='镜像站点', # 参数显示名称 |
| 31 | + help='用于访问 GitHub 的镜像/代理站点(例如:https://ghfast.top 或 https://gh-proxy.com)', # 帮助信息 |
| 32 | + default='https://ghfast.top', # 设置默认值 |
| 33 | + ) |
| 34 | + |
| 35 | + # 设置默认目录 |
| 36 | + default_comfyui_dir = os.path.join(os.getcwd(), 'ComfyUI') |
| 37 | + default_custom_nodes_dir = os.path.join(os.getcwd(), 'ComfyUI', 'custom_nodes') |
| 38 | + |
| 39 | + parser.add_argument( |
| 40 | + 'comfyui_dir', # 参数名称 |
| 41 | + metavar='ComfyUI 目录', # 参数显示名称 |
| 42 | + help='选择 ComfyUI 根目录', # 帮助信息 |
| 43 | + widget='DirChooser', # 使用目录选择器 |
| 44 | + default=default_comfyui_dir, # 设置默认值 |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + 'custom_nodes_dir', # 参数名称 |
| 48 | + metavar='自定义节点目录', # 参数显示名称 |
| 49 | + help='选择 custom_nodes 目录', # 帮助信息 |
| 50 | + widget='DirChooser', # 使用目录选择器 |
| 51 | + default=default_custom_nodes_dir, # 设置默认值 |
| 52 | + ) |
| 53 | + args = parser.parse_args() |
| 54 | + |
| 55 | + # 获取镜像地址 |
| 56 | + mirror_url = args.mirror_url |
| 57 | + |
| 58 | + # 获取 custom_nodes 目录下的文件夹总数 |
| 59 | + custom_nodes = [D for D in os.listdir(args.custom_nodes_dir) if os.path.isdir(os.path.join(args.custom_nodes_dir, D))] |
| 60 | + total_tasks = len(custom_nodes) + 1 # 包括 ComfyUI 根目录 |
| 61 | + completed_tasks = 0 |
| 62 | + |
| 63 | + # 更新进度条 |
| 64 | + def update_progress(): |
| 65 | + nonlocal completed_tasks |
| 66 | + completed_tasks += 1 |
| 67 | + print(f"进度: {completed_tasks}/{total_tasks}") # Gooey 会根据正则表达式更新进度条 |
| 68 | + sys.stdout.flush() # 强制刷新输出,确保 Gooey 捕获到进度信息 |
| 69 | + |
| 70 | + # 更新主仓库 |
| 71 | + try: |
| 72 | + print(f"ComfyUI 根目录: {args.comfyui_dir}") |
| 73 | + print(f"custom_nodes 目录: {args.custom_nodes_dir}") |
| 74 | + set_url_and_pull(args.comfyui_dir, mirror_url) |
| 75 | + update_progress() |
| 76 | + except Exception as e: |
| 77 | + print(f"错误: {e}") |
| 78 | + sys.exit(1) # 终止程序 |
| 79 | + |
| 80 | + # 更新 custom_nodes 目录下的所有子目录 |
| 81 | + with ThreadPoolExecutor() as executor: |
| 82 | + futures = [] |
| 83 | + for D in custom_nodes: |
| 84 | + dir_path = os.path.join(args.custom_nodes_dir, D) |
| 85 | + futures.append(executor.submit(set_url_and_pull, dir_path, mirror_url)) |
| 86 | + |
| 87 | + # 等待所有任务完成 |
| 88 | + for future in futures: |
| 89 | + try: |
| 90 | + future.result() # 获取任务结果,如果任务中有异常会抛出 |
| 91 | + update_progress() |
| 92 | + except Exception as e: |
| 93 | + print(f"错误: {e}") |
| 94 | + sys.exit(1) # 终止程序 |
| 95 | + |
| 96 | +def set_url_and_pull(repo_path, mirror_url): |
| 97 | + try: |
| 98 | + repo = git.Repo(repo_path) |
| 99 | + git_remote_url = repo.remotes.origin.url |
| 100 | + |
| 101 | + # 提取文件夹名称 |
| 102 | + repo_foldername = os.path.basename(repo_path) |
| 103 | + |
| 104 | + # 确保 URL 以 .git 结尾 |
| 105 | + if not git_remote_url.endswith('.git'): |
| 106 | + print(f"忽略 {repo_foldername} ,非标准 Git 仓库(仓库地址不以 .git 结尾): {git_remote_url}") |
| 107 | + return |
| 108 | + |
| 109 | + # 1. 直接下载:如果 remote URL 以 mirror_url 开头,则直接 pull |
| 110 | + if git_remote_url.startswith(f'{mirror_url}/'): |
| 111 | + print(f"正在更新: {repo_foldername}") |
| 112 | + repo.git.reset('--hard') |
| 113 | + repo.remotes.origin.pull() |
| 114 | + print(f"更新完成: {repo_foldername}") |
| 115 | + # 2. 添加镜像(代理):如果 remote URL 以 https://github.com/ 开头,则在开头添加 mirror_url |
| 116 | + elif git_remote_url.startswith('https://github.com/'): |
| 117 | + print(f"正在修改 GitHub URL 并更新: {repo_foldername}") |
| 118 | + new_url = f'{mirror_url}/{git_remote_url}' |
| 119 | + repo.git.reset('--hard') |
| 120 | + repo.remotes.origin.set_url(new_url) |
| 121 | + repo.remotes.origin.pull() |
| 122 | + print(f"更新完成: {repo_foldername}") |
| 123 | + # 3. 替换镜像(代理):如果 remote URL 包含 /https://github.com/,则替换镜像站点 |
| 124 | + elif '/https://github.com/' in git_remote_url: |
| 125 | + print(f"正在修改镜像 URL 并更新: {repo_foldername}") |
| 126 | + # 提取 https://github.com/ 之后的部分 |
| 127 | + github_part = git_remote_url.split('/https://github.com/')[1] |
| 128 | + new_url = f'{mirror_url}/https://github.com/{github_part}' |
| 129 | + repo.git.reset('--hard') |
| 130 | + repo.remotes.origin.set_url(new_url) |
| 131 | + repo.remotes.origin.pull() |
| 132 | + print(f"更新完成: {repo_foldername}") |
| 133 | + else: |
| 134 | + print(f"忽略 {repo_foldername},未知 URL 格式的仓库: {git_remote_url}") |
| 135 | + except Exception as e: |
| 136 | + print(f"处理 {repo_foldername} 时出错: {e}") |
| 137 | + raise # 重新抛出异常,让上层捕获并终止程序 |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + main() |
0 commit comments