From a3d858dc1a1dc585a2dc3529847dae2f9e41077c Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Thu, 25 Apr 2024 00:31:35 +0900 Subject: [PATCH] enhance: cm-cli supports updating comfyui --- cm-cli.py | 25 ++++++++++++++++++---- glob/manager_core.py | 47 +++++++++++++++++++++++++++++++++++++++++- glob/manager_server.py | 45 ++++------------------------------------ 3 files changed, 71 insertions(+), 46 deletions(-) diff --git a/cm-cli.py b/cm-cli.py index 30a256b0..a09ca68d 100644 --- a/cm-cli.py +++ b/cm-cli.py @@ -325,6 +325,16 @@ def update_node(node_name, is_all=False, cnt_msg=''): print(f"ERROR: An error occurred while uninstalling '{node_name}'.") +def update_comfyui(): + res = core.update_path(comfy_path, instant_execution=True) + if res == 'fail': + print("Updating ComfyUI has failed.") + elif res == 'updated': + print("ComfyUI is updated.") + else: + print("ComfyUI is already up to date.") + + def enable_node(node_name, is_all=False, cnt_msg=''): if node_name == 'ComfyUI-Manager': return @@ -401,9 +411,9 @@ def show_snapshot(simple_mode=False): def show_snapshot_list(simple_mode=False): - path = os.path.join(comfyui_manager_path, 'snapshots') + snapshot_path = os.path.join(comfyui_manager_path, 'snapshots') - files = os.listdir(path) + files = os.listdir(snapshot_path) json_files = [x for x in files if x.endswith('.json')] for x in sorted(json_files): print(x) @@ -423,7 +433,9 @@ def for_each_nodes(act, allow_all=True): is_all = False if allow_all and 'all' in nodes: is_all = True - nodes = [x for x in custom_node_map.keys() if os.path.exists(os.path.join(custom_nodes_path, x)) or os.path.exists(os.path.join(custom_nodes_path, x)+'.disabled')] + nodes = [x for x in custom_node_map.keys() if os.path.exists(os.path.join(custom_nodes_path, x)) or os.path.exists(os.path.join(custom_nodes_path, x) + '.disabled')] + + nodes = [x for x in nodes if x.lower() not in ['comfy', 'comfyui', 'all']] total = len(nodes) i = 1 @@ -433,7 +445,7 @@ def for_each_nodes(act, allow_all=True): except Exception as e: print(f"ERROR: {e}") traceback.print_exc() - i+=1 + i += 1 op = sys.argv[1] @@ -449,6 +461,11 @@ elif op == 'uninstall': for_each_nodes(uninstall_node) elif op == 'update': + for x in nodes: + if x.lower() in ['comfyui', 'comfy', 'all']: + update_comfyui() + break + for_each_nodes(update_node, allow_all=True) elif op == 'disable': diff --git a/glob/manager_core.py b/glob/manager_core.py index e79c4995..75c849de 100644 --- a/glob/manager_core.py +++ b/glob/manager_core.py @@ -21,7 +21,7 @@ sys.path.append(glob_path) import cm_global from manager_util import * -version = [2, 22, 2] +version = [2, 22, 3] version_str = f"V{version[0]}.{version[1]}" + (f'.{version[2]}' if len(version) > 2 else '') comfyui_manager_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) @@ -809,6 +809,51 @@ def gitclone_update(files, instant_execution=False, skip_script=False, msg_prefi return True +def update_path(repo_path, instant_execution=False): + if not os.path.exists(os.path.join(repo_path, '.git')): + return "fail" + + # version check + repo = git.Repo(repo_path) + + if repo.head.is_detached: + switch_to_default_branch(repo) + + current_branch = repo.active_branch + branch_name = current_branch.name + + if current_branch.tracking_branch() is None: + print(f"[ComfyUI-Manager] There is no tracking branch ({current_branch})") + remote_name = 'origin' + else: + remote_name = current_branch.tracking_branch().remote_name + remote = repo.remote(name=remote_name) + + try: + remote.fetch() + except Exception as e: + if 'detected dubious' in str(e): + print(f"[ComfyUI-Manager] Try fixing 'dubious repository' error on 'ComfyUI' repository") + safedir_path = comfy_path.replace('\\', '/') + subprocess.run(['git', 'config', '--global', '--add', 'safe.directory', safedir_path]) + try: + remote.fetch() + except Exception: + print(f"\n[ComfyUI-Manager] Failed to fixing repository setup. Please execute this command on cmd: \n" + f"-----------------------------------------------------------------------------------------\n" + f'git config --global --add safe.directory "{safedir_path}"\n' + f"-----------------------------------------------------------------------------------------\n") + + commit_hash = repo.head.commit.hexsha + remote_commit_hash = repo.refs[f'{remote_name}/{branch_name}'].object.hexsha + + if commit_hash != remote_commit_hash: + git_pull(repo_path) + execute_install_script("ComfyUI", repo_path, instant_execution=instant_execution) + return "updated" + else: + return "skipped" + def lookup_customnode_by_url(data, target): for x in data['custom_nodes']: if target in x['files']: diff --git a/glob/manager_server.py b/glob/manager_server.py index 0437f6f4..c61e889b 100644 --- a/glob/manager_server.py +++ b/glob/manager_server.py @@ -862,50 +862,13 @@ async def update_comfyui(request): try: repo_path = os.path.dirname(folder_paths.__file__) - - if not os.path.exists(os.path.join(repo_path, '.git')): + res = core.update_path(repo_path) + if res == "fail": print(f"ComfyUI update fail: The installed ComfyUI does not have a Git repository.") return web.Response(status=400) - - # version check - repo = git.Repo(repo_path) - - if repo.head.is_detached: - core.switch_to_default_branch(repo) - - current_branch = repo.active_branch - branch_name = current_branch.name - - if current_branch.tracking_branch() is None: - print(f"[ComfyUI-Manager] There is no tracking branch ({current_branch})") - remote_name = 'origin' - else: - remote_name = current_branch.tracking_branch().remote_name - remote = repo.remote(name=remote_name) - - try: - remote.fetch() - except Exception as e: - if 'detected dubious' in str(e): - print(f"[ComfyUI-Manager] Try fixing 'dubious repository' error on 'ComfyUI' repository") - safedir_path = core.comfy_path.replace('\\', '/') - subprocess.run(['git', 'config', '--global', '--add', 'safe.directory', safedir_path]) - try: - remote.fetch() - except Exception: - print(f"\n[ComfyUI-Manager] Failed to fixing repository setup. Please execute this command on cmd: \n" - f"-----------------------------------------------------------------------------------------\n" - f'git config --global --add safe.directory "{safedir_path}"\n' - f"-----------------------------------------------------------------------------------------\n") - - commit_hash = repo.head.commit.hexsha - remote_commit_hash = repo.refs[f'{remote_name}/{branch_name}'].object.hexsha - - if commit_hash != remote_commit_hash: - core.git_pull(repo_path) - core.execute_install_script("ComfyUI", repo_path) + elif res == "updated": return web.Response(status=201) - else: + else: # skipped return web.Response(status=200) except Exception as e: print(f"ComfyUI update fail: {e}", file=sys.stderr)