-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge_install.py
More file actions
477 lines (394 loc) · 14.7 KB
/
Copy pathbridge_install.py
File metadata and controls
477 lines (394 loc) · 14.7 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
"""
Shared install logic for ComfyUI Manager Bridge.
Used by:
- install.py (ComfyUI-Manager Git URL install)
- setup_model_manager.bat (portable deploy / refresh)
"""
from __future__ import annotations
import json
import os
import shutil
import sys
from pathlib import Path
PIP_JS_FILES = (
"model-manager.js",
"common.js",
"model-manager.css",
"turbogrid.esm.js",
"comfyui-gui-builder.js",
"popover-helper.js",
)
BRIDGE_JS_FILES = (
"bridge.js",
"bridge.css",
"model-hub.js",
"floating-window.js",
"import-url.js",
"bridge-ui.js",
"common.js",
"comfyui-gui-builder.js",
"model-manager.js",
)
NODE_PY_FILES = (
"__init__.py",
"bridge_backend.py",
"bridge_api_keys.py",
"install.py",
"bridge_install.py",
)
SKIP_DEPLOY_NAMES = {
".git",
".gitignore",
"docs",
"tools",
"patches",
"LICENSE",
"README.md",
"AGENTS.md",
"setup_model_manager.bat",
"uninstall_model_manager.bat",
"bridge_routes.py",
"__pycache__",
}
def repo_root_from(start: Path | None = None) -> Path:
start = (start or Path(__file__).resolve()).parent
markers = ("__init__.py", "bridge_backend.py")
for directory in (start, *start.parents):
if all((directory / name).is_file() for name in markers):
return directory
raise FileNotFoundError(
"Could not locate ComfyUI Manager Bridge repo root "
"(expected __init__.py and bridge_backend.py)."
)
def find_portable_root(start: Path) -> Path | None:
for directory in (start, *start.parents):
if (directory / "ComfyUI" / "main.py").is_file() and (
directory / "python_embeded" / "python.exe"
).is_file():
return directory
return None
def custom_nodes_dir(portable_root: Path) -> Path:
return portable_root / "ComfyUI" / "custom_nodes"
def default_deploy_dir(portable_root: Path) -> Path:
return custom_nodes_dir(portable_root) / "comfy-manager-bridge"
def is_deployed_node_dir(path: Path) -> bool:
normalized = path.as_posix().replace("\\", "/").lower()
return normalized.endswith("/custom_nodes/comfy-manager-bridge")
def find_pip_manager_js_dir(portable_root: Path | None) -> Path:
if portable_root is not None:
portable_js = (
portable_root
/ "python_embeded"
/ "Lib"
/ "site-packages"
/ "comfyui_manager"
/ "js"
)
if portable_js.is_dir():
return portable_js
try:
import comfyui_manager
pkg_js = Path(comfyui_manager.__file__).resolve().parent / "js"
if pkg_js.is_dir():
return pkg_js
except ImportError as exc:
raise FileNotFoundError(
"comfyui-manager pip package is not installed. "
"Install ComfyUI-Manager and launch once with --enable-manager."
) from exc
raise FileNotFoundError("Could not locate comfyui_manager/js in site-packages.")
def write_bridge_config(
js_dir: Path,
*,
button: str = "float",
quick_catalog: bool = False,
default_model_tab: str = "browse",
) -> None:
js_dir.mkdir(parents=True, exist_ok=True)
payload = {
"button": button,
"quickCatalog": quick_catalog,
"defaultModelTab": default_model_tab,
}
config_path = js_dir / "bridge-config.json"
config_path.write_text(json.dumps(payload, separators=(",", ":")) + "\n", encoding="utf-8")
def vendor_pip_js(pip_js_dir: Path, dest_js_dir: Path) -> None:
dest_js_dir.mkdir(parents=True, exist_ok=True)
missing = [name for name in PIP_JS_FILES if not (pip_js_dir / name).is_file()]
if missing:
raise FileNotFoundError(
"Missing Manager JS dependencies in pip package:\n "
+ "\n ".join(str(pip_js_dir / name) for name in missing)
)
for name in PIP_JS_FILES:
shutil.copy2(pip_js_dir / name, dest_js_dir / name)
def copy_bridge_js(source_js_dir: Path, dest_js_dir: Path) -> None:
dest_js_dir.mkdir(parents=True, exist_ok=True)
missing = [name for name in BRIDGE_JS_FILES if not (source_js_dir / name).is_file()]
if missing:
raise FileNotFoundError(
"Missing bridge JS sources:\n "
+ "\n ".join(str(source_js_dir / name) for name in missing)
)
for name in BRIDGE_JS_FILES:
shutil.copy2(source_js_dir / name, dest_js_dir / name)
def copy_node_python_files(source_root: Path, dest_root: Path) -> None:
dest_root.mkdir(parents=True, exist_ok=True)
for name in NODE_PY_FILES:
src = source_root / name
if src.is_file():
shutil.copy2(src, dest_root / name)
def rename_legacy_manager(custom_nodes: Path) -> bool:
legacy = custom_nodes / "comfyui-manager"
backup = custom_nodes / "comfyui-manager.orig"
if legacy.is_dir() and not backup.exists():
legacy.rename(backup)
print("[Manager-Bridge] Renamed custom_nodes/comfyui-manager to comfyui-manager.orig")
return True
return False
def refresh_node(
node_dir: Path,
repo_root: Path,
pip_js_dir: Path,
*,
button: str = "float",
quick_catalog: bool = False,
) -> None:
"""Vendor pip JS + bridge JS into an existing custom node directory."""
js_dir = node_dir / "js"
vendor_pip_js(pip_js_dir, js_dir)
source_js = (repo_root / "js").resolve()
if source_js != js_dir.resolve():
copy_bridge_js(source_js, js_dir)
write_bridge_config(
js_dir,
button=button,
quick_catalog=quick_catalog,
)
print(f"[Manager-Bridge] Refreshed JS assets in {node_dir}")
def deploy_from_repo(
repo_root: Path,
deploy_dir: Path,
pip_js_dir: Path,
*,
button: str = "float",
quick_catalog: bool = False,
rename_legacy: bool = True,
portable_root: Path | None = None,
) -> None:
if portable_root is not None and rename_legacy:
rename_legacy_manager(custom_nodes_dir(portable_root))
if deploy_dir.exists():
shutil.rmtree(deploy_dir)
deploy_dir.mkdir(parents=True, exist_ok=True)
copy_node_python_files(repo_root, deploy_dir)
(deploy_dir / "js").mkdir(parents=True, exist_ok=True)
refresh_node(
deploy_dir,
repo_root,
pip_js_dir,
button=button,
quick_catalog=quick_catalog,
)
print(f"[Manager-Bridge] Deployed extension to {deploy_dir}")
def apply_pip_patch(repo_root: Path, portable_root: Path, deploy_dir: Path) -> None:
pip_pkg = (
portable_root / "python_embeded" / "Lib" / "site-packages" / "comfyui_manager"
)
patches_dir = repo_root / "patches"
backup = patches_dir / "__init__.py.original"
if not backup.is_file() and (pip_pkg / "__init__.py").is_file():
patches_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(pip_pkg / "__init__.py", backup)
print("[Manager-Bridge] Backed up pip comfyui_manager/__init__.py")
shutil.copy2(patches_dir / "__init__.py", pip_pkg / "__init__.py")
shutil.copy2(repo_root / "bridge_backend.py", pip_pkg / "bridge_routes.py")
shutil.copy2(repo_root / "bridge_api_keys.py", pip_pkg / "bridge_api_keys.py")
(deploy_dir / ".use-pip-backend").touch()
print("[Manager-Bridge] Applied optional pip patch.")
def clear_pip_patch(deploy_dir: Path) -> None:
marker = deploy_dir / ".use-pip-backend"
if marker.exists():
marker.unlink()
def resolve_install_targets(
repo_root: Path | None = None,
portable_root: Path | None = None,
) -> tuple[Path, Path, Path, Path | None]:
repo_root = repo_root or repo_root_from()
portable_root = portable_root or find_portable_root(repo_root)
pip_js_dir = find_pip_manager_js_dir(portable_root)
if portable_root is None:
raise FileNotFoundError(
"Could not find ComfyUI portable root (ComfyUI/main.py + python_embeded/python.exe)."
)
deploy_dir = default_deploy_dir(portable_root)
if is_deployed_node_dir(repo_root.resolve()):
return repo_root, portable_root, pip_js_dir, deploy_dir
return repo_root, portable_root, pip_js_dir, deploy_dir
def run_setup(
*,
button: str = "float",
quick_catalog: bool = False,
apply_pip_patch: bool = False,
repo_root: Path | None = None,
portable_root: Path | None = None,
) -> int:
repo_root = repo_root or repo_root_from()
repo_root = repo_root.resolve()
portable_root = portable_root or find_portable_root(repo_root)
if portable_root is None:
print(
"ERROR: Could not find ComfyUI portable root.\n"
" cd to your portable folder, then run setup_model_manager.bat",
file=sys.stderr,
)
return 1
pip_js_dir = find_pip_manager_js_dir(portable_root)
deploy_dir = default_deploy_dir(portable_root)
print(f"[Manager-Bridge] Portable root: {portable_root}")
print(f"[Manager-Bridge] Repo/source: {repo_root}")
if is_deployed_node_dir(repo_root):
print(f"[Manager-Bridge] Git install detected - refreshing in place: {repo_root}")
refresh_node(
repo_root,
repo_root,
pip_js_dir,
button=button,
quick_catalog=quick_catalog,
)
target = repo_root
else:
print(f"[Manager-Bridge] Installing to: {deploy_dir}")
deploy_from_repo(
repo_root,
deploy_dir,
pip_js_dir,
button=button,
quick_catalog=quick_catalog,
rename_legacy=True,
portable_root=portable_root,
)
target = deploy_dir
if apply_pip_patch:
apply_pip_patch_fn(repo_root, portable_root, target)
else:
clear_pip_patch(target)
print("[Manager-Bridge] Skipping pip patch - routes load from custom node backend.")
print()
print("[Manager-Bridge] Setup complete.")
print(f" Button mode: {button}")
print(f" Quick catalog: {int(quick_catalog)}")
print(f" Pip patch: {int(apply_pip_patch)}")
return 0
def apply_pip_patch_fn(repo_root: Path, portable_root: Path, deploy_dir: Path) -> None:
apply_pip_patch(repo_root, portable_root, deploy_dir)
def run_install_py() -> int:
"""Entry point for ComfyUI-Manager install.py (Git URL install)."""
repo_root = repo_root_from()
portable_root = find_portable_root(repo_root)
try:
pip_js_dir = find_pip_manager_js_dir(portable_root)
except FileNotFoundError as exc:
print(f"ERROR: {exc}", file=sys.stderr)
return 1
print(f"[Manager-Bridge] install.py running in {repo_root}")
refresh_node(repo_root, repo_root, pip_js_dir)
clear_pip_patch(repo_root)
print("[Manager-Bridge] install.py complete. Restart ComfyUI and hard refresh the browser.")
return 0
def run_uninstall(
*,
repo_root: Path | None = None,
portable_root: Path | None = None,
remove_deploy: bool = True,
) -> int:
repo_root = repo_root or repo_root_from()
portable_root = portable_root or find_portable_root(repo_root)
if portable_root is None:
print("ERROR: Could not find ComfyUI portable root.", file=sys.stderr)
return 1
deploy_dir = default_deploy_dir(portable_root)
pip_pkg = (
portable_root / "python_embeded" / "Lib" / "site-packages" / "comfyui_manager"
)
custom_nodes = custom_nodes_dir(portable_root)
print(f"[Manager-Bridge] Portable root: {portable_root}")
in_place = is_deployed_node_dir(repo_root.resolve())
if remove_deploy:
if in_place:
print(
"[Manager-Bridge] Git install detected - removing vendored pip JS only "
"(keeping extension source)."
)
js_dir = repo_root / "js"
for name in PIP_JS_FILES:
path = js_dir / name
if path.is_file():
path.unlink()
marker = repo_root / ".use-pip-backend"
if marker.exists():
marker.unlink()
elif deploy_dir.is_dir():
print(f"[Manager-Bridge] Removing {deploy_dir}")
shutil.rmtree(deploy_dir)
else:
print(f"ERROR: Deployed extension not found at:\n {deploy_dir}", file=sys.stderr)
return 1
backup = repo_root / "patches" / "__init__.py.original"
if backup.is_file() and (pip_pkg / "__init__.py").is_file():
shutil.copy2(backup, pip_pkg / "__init__.py")
print("[Manager-Bridge] Restored pip comfyui_manager/__init__.py from backup.")
else:
print("[Manager-Bridge] No pip __init__.py backup - skipping pip restore.")
for name in ("bridge_routes.py", "bridge_api_keys.py"):
path = pip_pkg / name
if path.is_file():
path.unlink()
print(f"[Manager-Bridge] Removed pip {name}")
legacy = custom_nodes / "comfyui-manager"
backup_dir = custom_nodes / "comfyui-manager.orig"
if backup_dir.is_dir() and not legacy.exists():
backup_dir.rename(legacy)
print("[Manager-Bridge] Restored custom_nodes/comfyui-manager from comfyui-manager.orig")
print()
print("[Manager-Bridge] Uninstall complete.")
print(" Saved API tokens in ComfyUI/user/__manager/bridge-api-keys.json were not removed.")
print(" Restart ComfyUI to unload the bridge extension.")
return 0
def _parse_bool(value: str) -> bool:
return value.strip().lower() in ("1", "true", "yes", "on")
def main(argv: list[str] | None = None) -> int:
import argparse
parser = argparse.ArgumentParser(description="ComfyUI Manager Bridge installer")
sub = parser.add_subparsers(dest="command", required=True)
setup = sub.add_parser("setup", help="Deploy or refresh the bridge extension")
setup.add_argument("--button", default=os.environ.get("CMB_MANAGER_BTN", "float"))
setup.add_argument(
"--quick-catalog",
default=os.environ.get("CMB_QUICK_CATALOG", "0"),
)
setup.add_argument(
"--pip-patch",
default=os.environ.get("CMB_APPLY_PIP_PATCH", "0"),
)
sub.add_parser("install", help="Manager Git URL install hook")
uninstall = sub.add_parser("uninstall", help="Remove deployed extension")
uninstall.add_argument(
"--keep-git-source",
action="store_true",
help="When installed via Git URL, only remove vendored pip JS",
)
args = parser.parse_args(argv)
if args.command == "setup":
return run_setup(
button=args.button,
quick_catalog=_parse_bool(str(args.quick_catalog)),
apply_pip_patch=_parse_bool(str(args.pip_patch)),
)
if args.command == "install":
return run_install_py()
if args.command == "uninstall":
return run_uninstall()
return 1
if __name__ == "__main__":
raise SystemExit(main())