-
Notifications
You must be signed in to change notification settings - Fork 0
/
_external_functions.py
251 lines (195 loc) · 8.3 KB
/
_external_functions.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# All code below is from ComfyUIManager.__init__.py
# Only edits are deleting unnecessary checks for ComfyUI versions
# Support as of March.11 2024
import os
import sys
import urllib.parse
import urllib.request
import zipfile
from urllib.parse import urlparse
import subprocess
import threading
import locale
import time
import platform
import shutil
import folder_paths
from torchvision.datasets.utils import download_url
base_path = folder_paths.base_path # src/nodes/ComfyUIManager
custom_nodes_path = folder_paths.get_folder_paths("custom_nodes")[0] # src/nodes/ComfyUIManager/custom_nodes
js_path = os.path.join(base_path, "web", "extensions") # src/nodes/ComfyUIManager/web/extensions
comfyui_manager_path = os.path.dirname(__file__)
startup_script_path = os.path.join(comfyui_manager_path, "startup-scripts")
def is_valid_url(url):
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except ValueError:
return False
def unzip_install(files):
temp_filename = 'manager-temp.zip'
for url in files:
if url.endswith("/"):
url = url[:-1]
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
data = response.read()
with open(temp_filename, 'wb') as f:
f.write(data)
with zipfile.ZipFile(temp_filename, 'r') as zip_ref:
zip_ref.extractall(custom_nodes_path)
os.remove(temp_filename)
except Exception as e:
print(f"Install(unzip) error: {url} / {e}", file=sys.stderr)
return False
print("Installation was successful.")
return True
def copy_install(files, js_path_name=None):
for url in files:
if url.endswith("/"):
url = url[:-1]
try:
if url.endswith(".py"):
download_url(url, custom_nodes_path)
else:
path = os.path.join(js_path, js_path_name) if js_path_name is not None else js_path
if not os.path.exists(path):
os.makedirs(path)
download_url(url, path)
except Exception as e:
print(f"Install(copy) error: {url} / {e}", file=sys.stderr)
return False
print("Installation was successful.")
return True
def copy_uninstall(files, js_path_name='.'):
for url in files:
if url.endswith("/"):
url = url[:-1]
dir_name = os.path.basename(url)
base_path = custom_nodes_path if url.endswith('.py') else os.path.join(js_path, js_path_name)
file_path = os.path.join(base_path, dir_name)
try:
if os.path.exists(file_path):
os.remove(file_path)
elif os.path.exists(file_path + ".disabled"):
os.remove(file_path + ".disabled")
except Exception as e:
print(f"Uninstall(copy) error: {url} / {e}", file=sys.stderr)
return False
print("Uninstallation was successful.")
return True
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-Manager] 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()
def try_install_script(url, repo_path, install_cmd):
if (len(install_cmd) > 0 and install_cmd[0].startswith('#')):
if not os.path.exists(startup_script_path):
os.makedirs(startup_script_path)
script_path = os.path.join(startup_script_path, "install-scripts.txt")
with open(script_path, "a") as file:
obj = [repo_path] + install_cmd
file.write(f"{obj}\n")
return True
else:
print(f"\n## ComfyUI-Manager: EXECUTE => {install_cmd}")
code = run_script(install_cmd, cwd=repo_path)
if code != 0:
if url is None:
url = os.path.dirname(repo_path)
print(f"install script failed: {url}")
return False
def execute_install_script(url, repo_path, lazy_mode=False):
install_script_path = os.path.join(repo_path, "install.py")
requirements_path = os.path.join(repo_path, "requirements.txt")
if lazy_mode:
install_cmd = ["#LAZY-INSTALL-SCRIPT", sys.executable]
try_install_script(url, repo_path, install_cmd)
else:
if os.path.exists(requirements_path):
print("Install: pip packages")
with open(requirements_path, "r") as requirements_file:
for line in requirements_file:
package_name = line.strip()
if package_name:
install_cmd = [sys.executable, "-m", "pip", "install", package_name]
if package_name.strip() != "":
try_install_script(url, repo_path, install_cmd)
if os.path.exists(install_script_path):
print(f"Install: install script")
install_cmd = [sys.executable, "install.py"]
try_install_script(url, repo_path, install_cmd)
return True
def rmtree(path):
retry_count = 3
while True:
try:
retry_count -= 1
if platform.system() == "Windows":
run_script(['attrib', '-R', path + '\\*', '/S'])
shutil.rmtree(path)
return True
except Exception as ex:
print(f"ex: {ex}")
time.sleep(3)
if retry_count < 0:
raise ex
print(f"Uninstall retry({retry_count})")
def gitclone_uninstall(files):
import shutil
import os
print(f"uninstall: {files}")
for url in files:
if url.endswith("/"):
url = url[:-1]
try:
dir_name = os.path.splitext(os.path.basename(url))[0].replace(".git", "")
dir_path = os.path.join(custom_nodes_path, dir_name)
# safety check
if dir_path == '/' or dir_path[1:] == ":/" or dir_path == '':
print(f"Uninstall(git-clone) error: invalid path '{dir_path}' for '{url}'")
return False
install_script_path = os.path.join(dir_path, "uninstall.py")
disable_script_path = os.path.join(dir_path, "disable.py")
if os.path.exists(install_script_path):
uninstall_cmd = [sys.executable, "uninstall.py"]
code = run_script(uninstall_cmd, cwd=dir_path)
if code != 0:
print(f"An error occurred during the execution of the uninstall.py script. Only the '{dir_path}' will be deleted.")
elif os.path.exists(disable_script_path):
disable_script = [sys.executable, "disable.py"]
code = run_script(disable_script, cwd=dir_path)
if code != 0:
print(f"An error occurred during the execution of the disable.py script. Only the '{dir_path}' will be deleted.")
if os.path.exists(dir_path):
rmtree(dir_path)
elif os.path.exists(dir_path + ".disabled"):
rmtree(dir_path + ".disabled")
except Exception as e:
print(f"Uninstall(git-clone) error: {url} / {e}", file=sys.stderr)
return False
print("Uninstallation was successful.")
return True