forked from ltdrdata/ComfyUI-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprestartup_script.py
165 lines (122 loc) · 5.02 KB
/
prestartup_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import datetime
import os
import subprocess
import sys
import atexit
import threading
import re
message_collapses = []
def register_message_collapse(f):
global message_collapses
message_collapses.append(f)
sys.__comfyui_manager_register_message_collapse = register_message_collapse
try:
if '--port' in sys.argv:
port_index = sys.argv.index('--port')
if port_index + 1 < len(sys.argv):
port = int(sys.argv[port_index + 1])
postfix = f"_{port}"
else:
postfix = ""
# Logger setup
if os.path.exists(f"comfyui{postfix}.log"):
if os.path.exists(f"comfyui{postfix}.prev.log"):
if os.path.exists(f"comfyui{postfix}.prev2.log"):
os.remove(f"comfyui{postfix}.prev2.log")
os.rename(f"comfyui{postfix}.prev.log", f"comfyui{postfix}.prev2.log")
os.rename(f"comfyui{postfix}.log", f"comfyui{postfix}.prev.log")
original_stdout = sys.stdout
original_stderr = sys.stderr
tqdm = r'\d+%.*\[(.*?)\]'
log_file = open(f"comfyui{postfix}.log", "w", encoding="utf-8")
log_lock = threading.Lock()
class Logger:
def __init__(self, is_stdout):
self.is_stdout = is_stdout
def fileno(self):
try:
if self.is_stdout:
return original_stdout.fileno()
else:
return original_stderr.fileno()
except AttributeError:
# Handle error
raise ValueError("The object does not have a fileno method")
def write(self, message):
if any(f(message) for f in message_collapses):
return
if not self.is_stdout:
match = re.search(tqdm, message)
if match:
message = re.sub(r'([#|])\d', r'\1▌', message)
message = re.sub('#', '█', message)
if '100%' in message:
self.sync_write(message)
else:
original_stderr.write(message)
original_stderr.flush()
else:
self.sync_write(message)
else:
self.sync_write(message)
def sync_write(self, message):
with log_lock:
log_file.write(message)
log_file.flush()
if self.is_stdout:
original_stdout.write(message)
original_stdout.flush()
else:
original_stderr.write(message)
original_stderr.flush()
def flush(self):
log_file.flush()
if self.is_stdout:
original_stdout.flush()
else:
original_stderr.flush()
def handle_stream(stream, prefix):
for line in stream:
print(prefix, line, end="")
def close_log():
log_file.close()
sys.stdout = Logger(True)
sys.stderr = Logger(False)
atexit.register(close_log)
except Exception as e:
print(f"[ComfyUI-Manager] Logging failed: {e}")
print("** ComfyUI start up time:", datetime.datetime.now())
# Perform install
script_list_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "startup-scripts", "install-scripts.txt")
# Check if script_list_path exists
if os.path.exists(script_list_path):
print("\n#######################################################################")
print("[ComfyUI-Manager] Starting dependency installation/(de)activation for the extension\n")
executed = set()
# Read each line from the file and convert it to a list using eval
with open(script_list_path, 'r') as file:
for line in file:
if line in executed:
continue
executed.add(line)
try:
script = eval(line)
print(f"\n## ComfyUI-Manager: EXECUTE => {script[1:]}")
print(f"\n## Execute install/(de)activation script for '{script[0]}'")
process = subprocess.Popen(script[1:], cwd=script[0], 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()
exit_code = process.wait()
if exit_code != 0:
print(f"install/(de)activation script failed: {script[0]}")
except Exception as e:
print(f"[ERROR] Failed to execute install/(de)activation script: {line} / {e}")
# Remove the script_list_path file
if os.path.exists(script_list_path):
os.remove(script_list_path)
print("\n[ComfyUI-Manager] Startup script completed.")
print("#######################################################################\n")