|
| 1 | +from pathlib import Path |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +import json |
| 5 | +from typing import Union |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | + |
| 9 | + |
| 10 | +COMFYUI_REPO = "https://github.com/comfyanonymous/ComfyUI.git" |
| 11 | + |
| 12 | + |
| 13 | +def _clone_commit( |
| 14 | + url: str, |
| 15 | + commit: str, |
| 16 | + dir: Path, |
| 17 | +): |
| 18 | + if not dir.exists(): |
| 19 | + dir.mkdir(parents=True) |
| 20 | + script = f"""cd {dir} |
| 21 | + git init |
| 22 | + git remote add origin {url} |
| 23 | + git fetch --depth 1 origin {commit} |
| 24 | + git checkout FETCH_HEAD |
| 25 | + """ |
| 26 | + subprocess.check_call( |
| 27 | + script, |
| 28 | + shell=True, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def install_comfyui(snapshot, workspace: Path): |
| 33 | + comfyui_commit = snapshot["comfyui"] |
| 34 | + |
| 35 | + _clone_commit( |
| 36 | + COMFYUI_REPO, |
| 37 | + comfyui_commit, |
| 38 | + workspace, |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def install_custom_modules(snapshot, workspace: Path): |
| 43 | + for module in snapshot["custom_nodes"]: |
| 44 | + url = module["url"] |
| 45 | + directory = url.split("/")[-1].split(".")[0] |
| 46 | + module_dir = workspace / "custom_nodes" / directory |
| 47 | + |
| 48 | + commit_hash = module["commit_hash"] |
| 49 | + disabled = module.get("disabled", False) |
| 50 | + # if disabled: |
| 51 | + # continue |
| 52 | + _clone_commit( |
| 53 | + url, |
| 54 | + commit_hash, |
| 55 | + module_dir, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def install_dependencies(snapshot: dict, req_file: str, workspace: Path): |
| 60 | + python_version = snapshot["python"] |
| 61 | + subprocess.check_call( |
| 62 | + [ |
| 63 | + "uv", |
| 64 | + "python", |
| 65 | + "install", |
| 66 | + python_version, |
| 67 | + ], |
| 68 | + cwd=workspace, |
| 69 | + ) |
| 70 | + venv = workspace / ".venv" |
| 71 | + if (venv / "DONE").exists(): |
| 72 | + return |
| 73 | + venv_py = ( |
| 74 | + venv / "Scripts" / "python.exe" if os.name == "nt" else venv / "bin" / "python" |
| 75 | + ) |
| 76 | + subprocess.check_call( |
| 77 | + [ |
| 78 | + "uv", |
| 79 | + "venv", |
| 80 | + "--python", |
| 81 | + python_version, |
| 82 | + venv, |
| 83 | + ], |
| 84 | + ) |
| 85 | + subprocess.check_call( |
| 86 | + [ |
| 87 | + "uv", |
| 88 | + "pip", |
| 89 | + "install", |
| 90 | + "-p", |
| 91 | + str(venv_py), |
| 92 | + "pip", |
| 93 | + ], |
| 94 | + ) |
| 95 | + subprocess.check_call( |
| 96 | + [ |
| 97 | + "uv", |
| 98 | + "pip", |
| 99 | + "install", |
| 100 | + "-p", |
| 101 | + str(venv_py), |
| 102 | + "-r", |
| 103 | + req_file, |
| 104 | + "--no-deps", |
| 105 | + ], |
| 106 | + ) |
| 107 | + with open(venv / "DONE", "w") as f: |
| 108 | + f.write("DONE") |
| 109 | + |
| 110 | + |
| 111 | +def install(cpack: Union[str, Path]): |
| 112 | + workspace = Path("workspace") |
| 113 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 114 | + pack_dir = Path(temp_dir) / ".cpack" |
| 115 | + subprocess.check_call( |
| 116 | + [ |
| 117 | + "unzip", |
| 118 | + "-o", |
| 119 | + cpack, |
| 120 | + "-d", |
| 121 | + pack_dir, |
| 122 | + ] |
| 123 | + ) |
| 124 | + snapshot = json.loads((pack_dir / "snapshot.json").read_text()) |
| 125 | + req_txt_file = pack_dir / "requirements.txt" |
| 126 | + |
| 127 | + install_comfyui(snapshot, workspace) |
| 128 | + install_custom_modules(snapshot, workspace) |
| 129 | + install_dependencies(snapshot, str(req_txt_file), workspace) |
| 130 | + for f in (pack_dir / "inputs").glob("*"): |
| 131 | + shutil.copy(f, workspace / "input" / f.name) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + import sys |
| 136 | + |
| 137 | + install(sys.argv[1]) |
0 commit comments