Files
jtydhr88-ComfyUI-LayerDivider/__init__.py
T
2024-05-13 23:09:35 -04:00

67 lines
2.2 KiB
Python

import __main__
import os
import subprocess
import threading
import sys
import locale
import traceback
def handle_stream(stream, prefix):
stream.reconfigure(encoding=locale.getpreferredencoding(), errors='replace')
for msg in stream:
if prefix == '[!]' and ('it/s]' in msg or 's/it]' in msg) and ('%|' in msg or 'it [' in msg):
if msg.startswith('100%'):
print('\r' + msg, end="", file=sys.stderr),
else:
print('\r' + msg[:-1], end="", file=sys.stderr),
else:
if prefix == '[!]':
print(prefix, msg, end="", file=sys.stderr)
else:
print(prefix, msg, end="")
def run_script(cmd, cwd='.'):
if len(cmd) > 0 and cmd[0].startswith("#"):
print(f"[ComfyUI-LayerDivider] Unexpected behavior: `{cmd}`")
return 0
process = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1)
stdout_thread = threading.Thread(target=handle_stream, args=(process.stdout, ""))
stderr_thread = threading.Thread(target=handle_stream, args=(process.stderr, "[!]"))
stdout_thread.start()
stderr_thread.start()
stdout_thread.join()
stderr_thread.join()
return process.wait()
try:
from .layer_divider_node import NODE_CLASS_MAPPINGS
except:
my_path = os.path.dirname(__file__)
requirements_path = os.path.join(my_path, "requirements.txt")
print(f"## LayerDivider: installing dependencies")
run_script([sys.executable, '-s', '-m', 'pip', 'install', '-r', requirements_path])
try:
from .layer_divider_node import NODE_CLASS_MAPPINGS
except:
print(f"## [ERROR] LayerDivider: Attempting to reinstall dependencies using an alternative method.")
run_script([sys.executable, '-s', '-m', 'pip', 'install', '--user', '-r', requirements_path])
try:
from .nodes import NODE_CLASS_MAPPINGS
except:
print(f"## [ERROR] LayerDivider: Failed to install package in the correct Python environment.")
traceback.print_exc()
print(f"## LayerDivider: installing dependencies done.")
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']