mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-01-11 06:40:48 +08:00
When comfy_execution is touched, this is also a good chance to inject __init__.py files
This commit is contained in:
parent
df9abc1424
commit
9b04919330
@ -3,25 +3,29 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
# mitigations for comfyui workspace chaos
|
# mitigations for comfyui workspace chaos
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def auto_patch_workspace_and_restart():
|
def auto_patch_workspace_and_restart(workspace_dir: Optional[str] = None, skip_restart=False):
|
||||||
"""
|
"""
|
||||||
Detects a specific workspace structure, creates necessary __init__.py files
|
Detects a specific workspace structure, creates necessary __init__.py files
|
||||||
to make directories proper Python packages, and then restarts the application.
|
to make directories proper Python packages, and then restarts the application.
|
||||||
If the workspace is a Git repository, it locally ignores the created files.
|
If the workspace is a Git repository, it locally ignores the created files.
|
||||||
"""
|
"""
|
||||||
try:
|
if workspace_dir is None:
|
||||||
main_frame = next(f for f in reversed(inspect.stack()) if f.frame.f_globals.get('__name__') == '__main__')
|
try:
|
||||||
main_file_path = main_frame.filename
|
main_frame = next(f for f in reversed(inspect.stack()) if f.frame.f_globals.get('__name__') == '__main__')
|
||||||
workspace_dir = os.path.dirname(os.path.abspath(main_file_path))
|
main_file_path = main_frame.filename
|
||||||
except (StopIteration, AttributeError):
|
workspace_dir = os.path.dirname(os.path.abspath(main_file_path))
|
||||||
return
|
except (StopIteration, AttributeError):
|
||||||
|
logger.debug("workspace_dir was none and comfyui's main.py is not being run, skipping injection of __init__.py files")
|
||||||
|
return
|
||||||
|
|
||||||
if not os.path.isfile(os.path.join(workspace_dir, 'nodes.py')):
|
if not os.path.isfile(os.path.join(workspace_dir, 'nodes.py')):
|
||||||
|
logger.debug(f"did not find a nodes.py inside the workspace_dir {workspace_dir}, skipping injection of __init__.py files")
|
||||||
return
|
return
|
||||||
|
|
||||||
git_is_available = False
|
git_is_available = False
|
||||||
@ -65,14 +69,14 @@ def auto_patch_workspace_and_restart():
|
|||||||
for dirpath in sorted(list(dirs_to_initialize)):
|
for dirpath in sorted(list(dirs_to_initialize)):
|
||||||
init_py_path = os.path.join(dirpath, '__init__.py')
|
init_py_path = os.path.join(dirpath, '__init__.py')
|
||||||
if not os.path.exists(init_py_path):
|
if not os.path.exists(init_py_path):
|
||||||
logger.debug(f" Initializing package: {dirpath}")
|
logger.debug(f"initializing package: {dirpath}")
|
||||||
try:
|
try:
|
||||||
with open(init_py_path, 'w') as f:
|
with open(init_py_path, 'w') as f:
|
||||||
pass
|
pass
|
||||||
patched_any_file = True
|
patched_any_file = True
|
||||||
files_to_ignore.append(init_py_path)
|
files_to_ignore.append(init_py_path)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
logger.debug(f"Warning: Could not create {init_py_path}. Error: {e}")
|
logger.debug(f"could not create {init_py_path}. Error: {e}")
|
||||||
|
|
||||||
if git_is_available and files_to_ignore:
|
if git_is_available and files_to_ignore:
|
||||||
try:
|
try:
|
||||||
@ -90,12 +94,11 @@ def auto_patch_workspace_and_restart():
|
|||||||
if relative_path not in existing_lines:
|
if relative_path not in existing_lines:
|
||||||
f_append.write(f"\n{relative_path}")
|
f_append.write(f"\n{relative_path}")
|
||||||
existing_lines.add(relative_path)
|
existing_lines.add(relative_path)
|
||||||
logger.debug(f" Ignoring via .git/info/exclude: {relative_path}")
|
logger.debug(f"ignoring via .git/info/exclude: {relative_path}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug(f"Warning: Could not update Git exclude file. Error: {e}")
|
logger.debug(f"could not update Git exclude file. Error: {e}")
|
||||||
|
|
||||||
if patched_any_file:
|
if not skip_restart and patched_any_file:
|
||||||
logger.debug("Found and initialized Python package directories in your workspace. This is a one-time operation to enable proper imports. Now restarting...")
|
logger.info("Found and initialized Python package directories in your workspace. This is a one-time operation to enable proper imports. Now restarting...")
|
||||||
os.execv(sys.executable, [sys.executable] + sys.argv)
|
os.execv(sys.executable, [sys.executable] + sys.argv)
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,4 @@
|
|||||||
|
# There are some circumstances where comfy/__init__.py may already exist, so let's patch it here too
|
||||||
|
from comfy_compatibility.workspace import auto_patch_workspace_and_restart
|
||||||
|
|
||||||
|
auto_patch_workspace_and_restart()
|
||||||
Loading…
Reference in New Issue
Block a user