From fd3470c328c0fd9a6063a83be53159852e4b5dee Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Sun, 9 Jul 2023 10:17:37 +0900 Subject: [PATCH 1/2] support startup script for installation without locking on windows --- .gitignore | 3 ++- main.py | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 38d2ba11bf6..21a85d31694 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ extra_model_paths.yaml venv/ web/extensions/* !web/extensions/logging.js.example -!web/extensions/core/ \ No newline at end of file +!web/extensions/core/ +startup-scripts/ \ No newline at end of file diff --git a/main.py b/main.py index 7156499754d..28b6e5b6215 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,31 @@ +import os +import glob +import importlib.util + +# When the startup script is executed, it should ensure that it has minimal dependencies. +# This startup script can be executed while minimizing the impact of package locking caused by imports in Windows, especially during dependency installation processes. +startup_scripts_path = os.path.join(os.path.realpath(os.path.dirname(__file__)), "startup-scripts") +script_files = glob.glob(os.path.join(startup_scripts_path, "*.py")) + +if os.path.exists(startup_scripts_path): + script_files = os.listdir(startup_scripts_path) + + # Import each script file to execute + for script_file in script_files: + if script_file.endswith(".py"): + script_path = os.path.join(startup_scripts_path, script_file) + module_name = os.path.splitext(script_file)[0] + try: + spec = importlib.util.spec_from_file_location(module_name, script_path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + except Exception as e: + print(f"Failed to execute startup-script: {script_file} / {e}") + + +# Main code import asyncio import itertools -import os import shutil import threading import gc From 2d8c87e453840364e7b785129213a255228e3319 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Sun, 9 Jul 2023 11:03:36 +0900 Subject: [PATCH 2/2] modified: Instead of executing scripts from the startup-scripts directory, I will change it to execute the prestartup_script.py for each custom node. --- main.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 28b6e5b6215..2da78d7a31d 100644 --- a/main.py +++ b/main.py @@ -1,26 +1,33 @@ import os -import glob import importlib.util +import folder_paths -# When the startup script is executed, it should ensure that it has minimal dependencies. -# This startup script can be executed while minimizing the impact of package locking caused by imports in Windows, especially during dependency installation processes. -startup_scripts_path = os.path.join(os.path.realpath(os.path.dirname(__file__)), "startup-scripts") -script_files = glob.glob(os.path.join(startup_scripts_path, "*.py")) - -if os.path.exists(startup_scripts_path): - script_files = os.listdir(startup_scripts_path) - # Import each script file to execute - for script_file in script_files: - if script_file.endswith(".py"): - script_path = os.path.join(startup_scripts_path, script_file) - module_name = os.path.splitext(script_file)[0] +def execute_prestartup_script(): + def execute_script(script_path): + if os.path.exists(script_path): + module_name = os.path.splitext(script_path)[0] try: spec = importlib.util.spec_from_file_location(module_name, script_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) except Exception as e: - print(f"Failed to execute startup-script: {script_file} / {e}") + print(f"Failed to execute startup-script: {script_path} / {e}") + + node_paths = folder_paths.get_folder_paths("custom_nodes") + for custom_node_path in node_paths: + possible_modules = os.listdir(custom_node_path) + + for possible_module in possible_modules: + module_path = os.path.join(custom_node_path, possible_module) + if os.path.isfile(module_path) or module_path.endswith(".disabled") or module_path == "__pycache__": + continue + + script_path = os.path.join(module_path, "prestartup_script.py") + execute_script(script_path) + + +execute_prestartup_script() # Main code @@ -47,7 +54,6 @@ import yaml import execution -import folder_paths import server from server import BinaryEventTypes from nodes import init_custom_nodes