From 9d20369bd3ec20c7c7f5217ad913f023c7f2703f Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Mon, 15 May 2023 17:17:31 +0900 Subject: [PATCH] feat: disable/enable --- __init__.py | 114 +++++++++++++++++++++++++++++++++++++++--- js/comfyui-manager.js | 69 +++++++++++++++++++++++-- 2 files changed, 172 insertions(+), 11 deletions(-) diff --git a/__init__.py b/__init__.py index f2e24a11..2c3f15b0 100644 --- a/__init__.py +++ b/__init__.py @@ -9,7 +9,7 @@ sys.path.append('../..') from torchvision.datasets.utils import download_url # ensure .js -print("### Loading: ComfyUI-Manager (V0.2)") +print("### Loading: ComfyUI-Manager (V0.3)") comfy_path = os.path.dirname(folder_paths.__file__) custom_nodes_path = os.path.join(comfy_path, 'custom_nodes') @@ -135,7 +135,10 @@ def check_a_custom_node_installed(item): item['installed'] = 'True' except: item['installed'] = 'True' - + + elif os.path.exists(dir_path+".disabled"): + item['installed'] = 'Disabled' + else: item['installed'] = 'False' @@ -145,6 +148,8 @@ def check_a_custom_node_installed(item): file_path = os.path.join(base_path, dir_name) if os.path.exists(file_path): item['installed'] = 'True' + elif os.path.exists(file_path + ".disabled"): + item['installed'] = 'Disabled' else: item['installed'] = 'False' @@ -288,19 +293,52 @@ def copy_install(files, js_path_name=None): def copy_uninstall(files, js_path_name=None): for url in files: dir_name = os.path.basename(url) - base_path = custom_nodes_path if url.endswith('.py') else js_path + base_path = custom_nodes_path if url.endswith('.py') else os.path.join(js_path, js_path_name) file_path = os.path.join(base_path, dir_name) try: - os.remove(file_path) + if os.path.exists(file_path): + os.remove(file_path) + elif os.path.exists(file_path+".disabled"): + os.remove(file_path+".disabled") except Exception as e: - print(f"UnInstall(copy) error: {url} / {e}") + print(f"Uninstall(copy) error: {url} / {e}") return False print("Uninstallation was successful.") return True +def copy_set_active(files, is_disable, js_path_name=None): + if is_disable: + action_name = "Disable" + else: + action_name = "Enable" + + for url in files: + dir_name = os.path.basename(url) + base_path = custom_nodes_path if url.endswith('.py') else os.path.join(js_path, js_path_name) + file_path = os.path.join(base_path, dir_name) + + try: + if is_disable: + current_name = file_path + new_name = file_path + ".disabled" + else: + current_name = file_path + ".disabled" + new_name = file_path + + os.rename(current_name, new_name) + + except Exception as e: + print(f"{action_name}(copy) error: {url} / {e}") + + return False + + print(f"{action_name} was successful.") + return True + + def gitclone_install(files): print(f"install: {files}") for url in files: @@ -359,8 +397,11 @@ def gitclone_uninstall(files): if dir_path == '/' or dir_path[1:] == ":/" or dir_path == '': print(f"Uninstall(git-clone) error: invalid path '{dir_path}' for '{url}'") return False - - shutil.rmtree(dir_path) + + if os.path.exists(dir_path): + shutil.rmtree(dir_path) + elif os.path.exists(dir_path+".disabled"): + shutil.rmtree(dir_path+".disabled") except Exception as e: print(f"Uninstall(git-clone) error: {url} / {e}") return False @@ -369,8 +410,43 @@ def gitclone_uninstall(files): return True +def gitclone_set_active(files, is_disable): + import os + + if is_disable: + action_name = "Disable" + else: + action_name = "Enable" + + print(f"{action_name}: {files}") + for url in files: + try: + dir_name = os.path.splitext(os.path.basename(url))[0].replace(".git", "") + dir_path = os.path.join(custom_nodes_path, dir_name) + + # safey check + if dir_path == '/' or dir_path[1:] == ":/" or dir_path == '': + print(f"{action_name}(git-clone) error: invalid path '{dir_path}' for '{url}'") + return False + + if is_disable: + current_path = dir_path + new_path = dir_path + ".disabled" + else: + current_path = dir_path + ".disabled" + new_path = dir_path + + os.rename(current_path, new_path) + + except Exception as e: + print(f"{action_name}(git-clone) error: {url} / {e}") + return False + + print(f"{action_name} was successful.") + return True + + def gitclone_update(files): - import shutil import os print(f"uninstall: {files}") @@ -456,6 +532,28 @@ async def install_custom_node(request): return web.Response(status=400) +@server.PromptServer.instance.routes.post("/customnode/toggle_active") +async def install_custom_node(request): + json_data = await request.json() + + install_type = json_data['install_type'] + is_disabled = json_data['installed'] == "Disabled" + + print(f"Update custom node '{json_data['title']}'") + + res = False + + if install_type == "git-clone": + res = gitclone_set_active(json_data['files'], not is_disabled) + elif install_type == "copy": + res = copy_set_active(json_data['files'], not is_disabled) + + if res: + return web.json_response({}, content_type='application/json') + + return web.Response(status=400) + + @server.PromptServer.instance.routes.post("/model/install") async def install_model(request): json_data = await request.json() diff --git a/js/comfyui-manager.js b/js/comfyui-manager.js index 420a4937..34e786c6 100644 --- a/js/comfyui-manager.js +++ b/js/comfyui-manager.js @@ -216,20 +216,40 @@ class CustomNodesInstaller extends ComfyDialog { var installBtn = document.createElement('button'); var installBtn2 = null; + var installBtn3 = null; this.install_buttons.push(installBtn); switch(data.installed) { + case 'Disabled': + installBtn3 = document.createElement('button'); + installBtn3.innerHTML = 'Enable'; + installBtn3.style.backgroundColor = 'blue'; + this.install_buttons.push(installBtn3); + + installBtn.innerHTML = 'Uninstall'; + installBtn.style.backgroundColor = 'red'; + break; case 'Update': installBtn2 = document.createElement('button'); installBtn2.innerHTML = 'Update'; installBtn2.style.backgroundColor = 'blue'; this.install_buttons.push(installBtn2); + installBtn3 = document.createElement('button'); + installBtn3.innerHTML = 'Disable'; + installBtn3.style.backgroundColor = 'MediumSlateBlue'; + this.install_buttons.push(installBtn3); + installBtn.innerHTML = 'Uninstall'; installBtn.style.backgroundColor = 'red'; break; case 'True': + installBtn3 = document.createElement('button'); + installBtn3.innerHTML = 'Disable'; + installBtn3.style.backgroundColor = 'MediumSlateBlue'; + this.install_buttons.push(installBtn3); + installBtn.innerHTML = 'Uninstall'; installBtn.style.backgroundColor = 'red'; break; @@ -239,10 +259,11 @@ class CustomNodesInstaller extends ComfyDialog { break; default: installBtn.innerHTML = 'Try Install'; - installBtn.style.backgroundColor = 'silver'; + installBtn.style.backgroundColor = 'Gray'; } if(installBtn2 != null) { + installBtn2.style.width = "120px"; installBtn2.addEventListener('click', function() { install_custom_node(data, CustomNodesInstaller.instance, 'update'); }); @@ -250,6 +271,16 @@ class CustomNodesInstaller extends ComfyDialog { data5.appendChild(installBtn2); } + if(installBtn3 != null) { + installBtn3.style.width = "120px"; + installBtn3.addEventListener('click', function() { + install_custom_node(data, CustomNodesInstaller.instance, 'toggle_active'); + }); + + data5.appendChild(installBtn3); + } + + installBtn.style.width = "120px"; installBtn.addEventListener('click', function() { if(this.innerHTML == 'Uninstall') { if (confirm(`Are you sure uninstall ${data.title}?`)) { @@ -441,20 +472,40 @@ class AlternativesInstaller extends ComfyDialog { if(data.custom_node) { var installBtn = document.createElement('button'); var installBtn2 = null; + var installBtn3 = null; this.install_buttons.push(installBtn); switch(data.custom_node.installed) { + case 'Disabled': + installBtn3 = document.createElement('button'); + installBtn3.innerHTML = 'Enable'; + installBtn3.style.backgroundColor = 'blue'; + this.install_buttons.push(installBtn3); + + installBtn.innerHTML = 'Uninstall'; + installBtn.style.backgroundColor = 'red'; + break; case 'Update': installBtn2 = document.createElement('button'); installBtn2.innerHTML = 'Update'; installBtn2.style.backgroundColor = 'blue'; this.install_buttons.push(installBtn2); - + + installBtn3 = document.createElement('button'); + installBtn3.innerHTML = 'Disable'; + installBtn3.style.backgroundColor = 'MediumSlateBlue'; + this.install_buttons.push(installBtn3); + installBtn.innerHTML = 'Uninstall'; installBtn.style.backgroundColor = 'red'; break; case 'True': + installBtn3 = document.createElement('button'); + installBtn3.innerHTML = 'Disable'; + installBtn3.style.backgroundColor = 'MediumSlateBlue'; + this.install_buttons.push(installBtn3); + installBtn.innerHTML = 'Uninstall'; installBtn.style.backgroundColor = 'red'; break; @@ -464,10 +515,11 @@ class AlternativesInstaller extends ComfyDialog { break; default: installBtn.innerHTML = 'Try Install'; - installBtn.style.backgroundColor = 'silver'; + installBtn.style.backgroundColor = 'Gray'; } if(installBtn2 != null) { + installBtn2.style.width = "120px"; installBtn2.addEventListener('click', function() { install_custom_node(data.custom_node, AlternativesInstaller.instance, 'update'); }); @@ -475,6 +527,17 @@ class AlternativesInstaller extends ComfyDialog { data6.appendChild(installBtn2); } + if(installBtn3 != null) { + installBtn3.style.width = "120px"; + installBtn3.addEventListener('click', function() { + install_custom_node(data, CustomNodesInstaller.instance, 'toggle_active'); + }); + + data6.appendChild(installBtn3); + } + + + installBtn.style.width = "120px"; installBtn.addEventListener('click', function() { if(this.innerHTML == 'Uninstall') { if (confirm(`Are you sure uninstall ${data.title}?`)) {