auto_fix_deps.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env python3
  2. """
  3. auto_fix_deps.py — Run check_deps.py, then attempt to install whatever is missing.
  4. For local servers:
  5. - Missing custom nodes → `comfy node install <package>`
  6. - Missing models → `comfy model download` (only if a URL is supplied via
  7. --model-source-file or detected via well-known names)
  8. For cloud: prints what would be needed but cannot install (cloud preinstalls
  9. custom nodes and most models server-side; if something genuinely isn't there,
  10. ask Comfy support).
  11. This is conservative: it never installs without an explicit URL for models
  12. (downloading the wrong model is hard to undo). Custom nodes from the registry
  13. are auto-installed by name.
  14. Usage:
  15. python3 auto_fix_deps.py workflow_api.json
  16. python3 auto_fix_deps.py workflow_api.json --models-from-file urls.json
  17. python3 auto_fix_deps.py workflow_api.json --dry-run
  18. """
  19. from __future__ import annotations
  20. import argparse
  21. import json
  22. import shutil
  23. import subprocess
  24. import sys
  25. from pathlib import Path
  26. sys.path.insert(0, str(Path(__file__).resolve().parent))
  27. from _common import (
  28. DEFAULT_LOCAL_HOST, ENV_API_KEY, emit_json, log, resolve_api_key,
  29. )
  30. from check_deps import check_deps
  31. from _common import unwrap_workflow
  32. def comfy_cli_available() -> str | None:
  33. """Return command prefix for comfy-cli, or None."""
  34. if shutil.which("comfy"):
  35. return "comfy"
  36. if shutil.which("uvx"):
  37. return "uvx --from comfy-cli comfy"
  38. return None
  39. def run_cmd(cmd: list[str], *, dry_run: bool = False) -> tuple[int, str]:
  40. if dry_run:
  41. return 0, "[dry-run]"
  42. log(f"$ {' '.join(cmd)}")
  43. # health: allow HX006 -- node installs and multi-GB model downloads have no fair upper bound
  44. proc = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8', errors='replace',
  45. check=False, stdin=subprocess.DEVNULL)
  46. out = (proc.stdout or "") + (proc.stderr or "")
  47. return proc.returncode, out
  48. def install_node(package: str, *, dry_run: bool = False, comfy_cmd: str = "comfy") -> bool:
  49. cmd = comfy_cmd.split() + ["--skip-prompt", "node", "install", package]
  50. code, _ = run_cmd(cmd, dry_run=dry_run)
  51. return code == 0
  52. def install_model(url: str, folder: str, filename: str | None = None,
  53. *, dry_run: bool = False, comfy_cmd: str = "comfy",
  54. hf_token: str | None = None, civitai_token: str | None = None) -> bool:
  55. cmd = comfy_cmd.split() + [
  56. "--skip-prompt", "model", "download",
  57. "--url", url,
  58. "--relative-path", f"models/{folder}",
  59. ]
  60. if filename:
  61. cmd.extend(["--filename", filename])
  62. if hf_token:
  63. cmd.extend(["--set-hf-api-token", hf_token])
  64. if civitai_token:
  65. cmd.extend(["--set-civitai-api-token", civitai_token])
  66. code, _ = run_cmd(cmd, dry_run=dry_run)
  67. return code == 0
  68. def main(argv: list[str] | None = None) -> int:
  69. p = argparse.ArgumentParser(description="Run check_deps and install whatever is missing")
  70. p.add_argument("workflow")
  71. p.add_argument("--host", default=DEFAULT_LOCAL_HOST)
  72. p.add_argument("--api-key", help=f"or set ${ENV_API_KEY}")
  73. p.add_argument("--models-from-file",
  74. help="JSON file mapping {model_filename: download_url} for models that need install")
  75. p.add_argument("--hf-token", help="HuggingFace token for downloads")
  76. p.add_argument("--civitai-token", help="CivitAI token for downloads")
  77. p.add_argument("--dry-run", action="store_true",
  78. help="Show what would be installed without doing it")
  79. p.add_argument("--no-restart", action="store_true",
  80. help="Don't suggest restarting the server after node install")
  81. args = p.parse_args(argv)
  82. api_key = resolve_api_key(args.api_key)
  83. wf_path = Path(args.workflow).expanduser()
  84. if not wf_path.exists():
  85. emit_json({"error": f"Workflow not found: {args.workflow}"})
  86. return 1
  87. try:
  88. with wf_path.open(encoding="utf-8-sig") as f:
  89. workflow = unwrap_workflow(json.load(f))
  90. except (ValueError, json.JSONDecodeError) as e:
  91. emit_json({"error": str(e)})
  92. return 1
  93. report = check_deps(workflow, host=args.host, api_key=api_key)
  94. if report["is_ready"]:
  95. emit_json({"status": "ready", "report": report})
  96. return 0
  97. if report["is_cloud"]:
  98. emit_json({
  99. "status": "cannot_fix_cloud",
  100. "reason": "Comfy Cloud preinstalls nodes; if something is genuinely missing, contact support.",
  101. "report": report,
  102. })
  103. return 1
  104. comfy_cmd = comfy_cli_available()
  105. if not comfy_cmd:
  106. emit_json({
  107. "status": "cannot_fix",
  108. "reason": "comfy-cli not on PATH; install with `pip install comfy-cli` or `pipx install comfy-cli`",
  109. "report": report,
  110. })
  111. return 1
  112. actions: list[dict] = []
  113. failures: list[dict] = []
  114. # ---- Install missing custom nodes ----
  115. seen_packages: set[str] = set()
  116. for entry in report["missing_nodes"]:
  117. cmd = entry.get("fix_command", "")
  118. if cmd.startswith("comfy node install "):
  119. package = cmd.split(" ")[-1]
  120. if package in seen_packages:
  121. continue
  122. seen_packages.add(package)
  123. ok = install_node(package, dry_run=args.dry_run, comfy_cmd=comfy_cmd)
  124. (actions if ok else failures).append({
  125. "kind": "node", "package": package, "node_class": entry["class_type"],
  126. "ok": ok,
  127. })
  128. else:
  129. failures.append({
  130. "kind": "node", "node_class": entry["class_type"],
  131. "ok": False, "reason": "No registry mapping known. " + entry.get("fix_hint", ""),
  132. })
  133. # ---- Install missing models (only when URL provided) ----
  134. sources: dict[str, str] = {}
  135. if args.models_from_file:
  136. try:
  137. sources = json.loads(Path(args.models_from_file).read_text(encoding="utf-8"))
  138. except (OSError, json.JSONDecodeError) as e:
  139. log(f"Could not read --models-from-file: {e}")
  140. for entry in report["missing_models"]:
  141. filename = entry["value"]
  142. url = sources.get(filename)
  143. if not url:
  144. failures.append({
  145. "kind": "model", "filename": filename, "folder": entry["folder"],
  146. "ok": False, "reason": "No URL provided in --models-from-file. "
  147. "Refusing to guess.",
  148. })
  149. continue
  150. ok = install_model(
  151. url, entry["folder"], filename,
  152. dry_run=args.dry_run, comfy_cmd=comfy_cmd,
  153. hf_token=args.hf_token, civitai_token=args.civitai_token,
  154. )
  155. (actions if ok else failures).append({
  156. "kind": "model", "filename": filename, "folder": entry["folder"],
  157. "url": url, "ok": ok,
  158. })
  159. # ---- Embeddings ----
  160. for entry in report["missing_embeddings"]:
  161. emb_name = entry["embedding_name"]
  162. # Try common extensions in user-supplied source map
  163. url = (sources.get(f"{emb_name}.pt")
  164. or sources.get(f"{emb_name}.safetensors")
  165. or sources.get(emb_name))
  166. if not url:
  167. failures.append({
  168. "kind": "embedding", "name": emb_name,
  169. "ok": False, "reason": "No URL provided in --models-from-file.",
  170. })
  171. continue
  172. target_filename = (
  173. f"{emb_name}.safetensors" if url.endswith(".safetensors")
  174. else f"{emb_name}.pt"
  175. )
  176. ok = install_model(
  177. url, "embeddings", target_filename,
  178. dry_run=args.dry_run, comfy_cmd=comfy_cmd,
  179. hf_token=args.hf_token, civitai_token=args.civitai_token,
  180. )
  181. (actions if ok else failures).append({
  182. "kind": "embedding", "name": emb_name, "url": url, "ok": ok,
  183. })
  184. needs_restart = any(a["kind"] == "node" and a.get("ok") for a in actions)
  185. emit_json({
  186. "status": "fixed" if not failures else "partial",
  187. "actions_taken": actions,
  188. "failures": failures,
  189. "needs_server_restart": needs_restart and not args.no_restart,
  190. "restart_hint": "comfy stop && comfy launch --background",
  191. "dry_run": args.dry_run,
  192. })
  193. return 0 if not failures else 1
  194. if __name__ == "__main__":
  195. sys.exit(main())