When comfy_execution is touched, this is also a good chance to inject __init__.py files

This commit is contained in:
doctorpangloss 2025-09-04 14:02:10 -07:00
parent df9abc1424
commit 9b04919330
2 changed files with 21 additions and 14 deletions

View File

@ -3,25 +3,29 @@ import logging
import os
import shutil
import sys
from typing import Optional
# mitigations for comfyui workspace chaos
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
to make directories proper Python packages, and then restarts the application.
If the workspace is a Git repository, it locally ignores the created files.
"""
try:
main_frame = next(f for f in reversed(inspect.stack()) if f.frame.f_globals.get('__name__') == '__main__')
main_file_path = main_frame.filename
workspace_dir = os.path.dirname(os.path.abspath(main_file_path))
except (StopIteration, AttributeError):
return
if workspace_dir is None:
try:
main_frame = next(f for f in reversed(inspect.stack()) if f.frame.f_globals.get('__name__') == '__main__')
main_file_path = main_frame.filename
workspace_dir = os.path.dirname(os.path.abspath(main_file_path))
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')):
logger.debug(f"did not find a nodes.py inside the workspace_dir {workspace_dir}, skipping injection of __init__.py files")
return
git_is_available = False
@ -65,14 +69,14 @@ def auto_patch_workspace_and_restart():
for dirpath in sorted(list(dirs_to_initialize)):
init_py_path = os.path.join(dirpath, '__init__.py')
if not os.path.exists(init_py_path):
logger.debug(f" Initializing package: {dirpath}")
logger.debug(f"initializing package: {dirpath}")
try:
with open(init_py_path, 'w') as f:
pass
patched_any_file = True
files_to_ignore.append(init_py_path)
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:
try:
@ -90,12 +94,11 @@ def auto_patch_workspace_and_restart():
if relative_path not in existing_lines:
f_append.write(f"\n{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:
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:
logger.debug("Found and initialized Python package directories in your workspace. This is a one-time operation to enable proper imports. Now restarting...")
if not skip_restart and patched_any_file:
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)

View File

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