enhance: cm-cli supports updating comfyui

This commit is contained in:
Dr.Lt.Data 2024-04-25 00:31:35 +09:00
parent 4e1697d2aa
commit a3d858dc1a
3 changed files with 71 additions and 46 deletions

View File

@ -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)
@ -425,6 +435,8 @@ def for_each_nodes(act, allow_all=True):
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 nodes if x.lower() not in ['comfy', 'comfyui', 'all']]
total = len(nodes)
i = 1
for x in nodes:
@ -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':

View File

@ -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']:

View File

@ -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)