-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·420 lines (348 loc) · 13.7 KB
/
install.py
File metadata and controls
executable file
·420 lines (348 loc) · 13.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
import os
import platform
import subprocess
import sys
from typing import Dict, List, Optional, TypedDict
import requests
import semver
import typer
from rich import print as rprint
from rich.console import Console
from rich.panel import Panel
from comfy_cli import constants, ui, utils
from comfy_cli.constants import GPU_OPTION
from comfy_cli.git_utils import git_checkout_tag
from comfy_cli.uv import DependencyCompiler
from comfy_cli.workspace_manager import WorkspaceManager, check_comfy_repo
workspace_manager = WorkspaceManager()
console = Console()
def get_os_details():
os_name = platform.system() # e.g., Linux, Darwin (macOS), Windows
os_version = platform.release()
return os_name, os_version
def pip_install_comfyui_dependencies(
repo_dir,
gpu: GPU_OPTION,
plat: constants.OS,
cuda_version: constants.CUDAVersion,
skip_torch_or_directml: bool,
skip_requirement: bool,
):
os.chdir(repo_dir)
result = None
if not skip_torch_or_directml:
# install torch for AMD Linux
if gpu == GPU_OPTION.AMD and plat == constants.OS.LINUX:
pip_url = ["--extra-index-url", "https://download.pytorch.org/whl/rocm6.0"]
result = subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
"torch",
"torchvision",
"torchaudio",
]
+ pip_url,
check=False,
)
# install torch for NVIDIA
if gpu == GPU_OPTION.NVIDIA:
base_command = [
sys.executable,
"-m",
"pip",
"install",
"torch",
"torchvision",
"torchaudio",
]
if plat == constants.OS.WINDOWS and cuda_version == constants.CUDAVersion.v12_6:
base_command += [
"--extra-index-url",
"https://download.pytorch.org/whl/cu126",
]
elif plat == constants.OS.WINDOWS and cuda_version == constants.CUDAVersion.v12_4:
base_command += [
"--extra-index-url",
"https://download.pytorch.org/whl/cu124",
]
elif plat == constants.OS.WINDOWS and cuda_version == constants.CUDAVersion.v12_1:
base_command += [
"--extra-index-url",
"https://download.pytorch.org/whl/cu121",
]
elif plat == constants.OS.WINDOWS and cuda_version == constants.CUDAVersion.v11_8:
base_command += [
"--extra-index-url",
"https://download.pytorch.org/whl/cu118",
]
result = subprocess.run(
base_command,
check=False,
)
# Beta support for intel arch based on this PR: https://github.com/comfyanonymous/ComfyUI/pull/3439
if gpu == GPU_OPTION.INTEL_ARC:
pip_url = [
"--extra-index-url",
"https://pytorch-extension.intel.com/release-whl/stable/xpu/us/",
]
utils.install_conda_package("libuv")
# TODO: wrap pip install in a function
subprocess.run(
[sys.executable, "-m", "pip", "install", "mkl", "mkl-dpcpp"],
check=True,
)
result = subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
"torch==2.1.0.post2",
"torchvision==0.16.0.post2",
"torchaudio==2.1.0.post2",
"intel-extension-for-pytorch==2.1.30",
]
+ pip_url,
check=False,
)
if result and result.returncode != 0:
rprint("Failed to install PyTorch dependencies. Please check your environment (`comfy env`) and try again")
sys.exit(1)
# install directml for AMD windows
if gpu == GPU_OPTION.AMD and plat == constants.OS.WINDOWS:
result = subprocess.run([sys.executable, "-m", "pip", "install", "torch-directml"], check=True)
# install torch for Mac M Series
if gpu == GPU_OPTION.MAC_M_SERIES:
result = subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
"--pre",
"torch",
"torchvision",
"torchaudio",
"--extra-index-url",
"https://download.pytorch.org/whl/nightly/cpu",
],
check=True,
)
# install requirements.txt
if skip_requirement:
return
result = subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], check=False)
if result.returncode != 0:
rprint("Failed to install ComfyUI dependencies. Please check your environment (`comfy env`) and try again.")
sys.exit(1)
# install requirements for manager
def pip_install_manager_dependencies(repo_dir):
os.chdir(os.path.join(repo_dir, "custom_nodes", "ComfyUI-Manager"))
subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], check=True)
def execute(
url: str,
comfy_path: str,
restore: bool,
version: str,
commit: Optional[str] = None,
gpu: constants.GPU_OPTION = None,
cuda_version: constants.CUDAVersion = constants.CUDAVersion.v12_6,
plat: constants.OS = None,
skip_torch_or_directml: bool = False,
skip_requirement: bool = False,
fast_deps: bool = False,
*args,
**kwargs,
):
"""
Install ComfyUI from a given URL.
"""
if not workspace_manager.skip_prompting:
res = ui.prompt_confirm_action(f"Install from {url} to {comfy_path}?", True)
if not res:
rprint("Aborting...")
raise typer.Exit(code=1)
rprint(f"Installing from repository [bold yellow]'{url}'[/bold yellow] to '{comfy_path}'")
repo_dir = comfy_path
parent_path = os.path.abspath(os.path.join(repo_dir, ".."))
if not os.path.exists(parent_path):
os.makedirs(parent_path, exist_ok=True)
if not os.path.exists(repo_dir):
clone_comfyui(url=url, repo_dir=repo_dir)
if version != "nightly":
try:
checkout_stable_comfyui(version=version, repo_dir=repo_dir)
except GitHubRateLimitError as e:
rprint(f"[bold red]Error checking out ComfyUI version: {e}[/bold red]")
sys.exit(1)
elif not check_comfy_repo(repo_dir)[0]:
rprint(
f"[bold red]'{repo_dir}' already exists. But it is an invalid ComfyUI repository. Remove it and retry.[/bold red]"
)
sys.exit(-1)
# checkout specified commit
if commit is not None:
os.chdir(repo_dir)
subprocess.run(["git", "checkout", commit], check=True)
if not fast_deps:
pip_install_comfyui_dependencies(repo_dir, gpu, plat, cuda_version, skip_torch_or_directml, skip_requirement)
WorkspaceManager().set_recent_workspace(repo_dir)
workspace_manager.setup_workspace_manager(specified_workspace=repo_dir)
rprint("")
if fast_deps:
depComp = DependencyCompiler(cwd=repo_dir, gpu=gpu)
depComp.compile_deps()
depComp.install_deps()
os.chdir(repo_dir)
rprint("")
def validate_version(version: str) -> Optional[str]:
"""
Validates the version string as 'latest', 'nightly', or a semantically version number.
Args:
version (str): The version string to validate.
Returns:
Optional[str]: The validated version string, or None if invalid.
Raises:
ValueError: If the version string is invalid.
"""
if version.lower() in ["nightly", "latest"]:
return version.lower()
# Remove 'v' prefix if present
if version.startswith("v"):
version = version[1:]
try:
semver.VersionInfo.parse(version)
return version
except ValueError as exc:
raise ValueError(
f"Invalid version format: {version}. "
"Please use 'nightly', 'latest', or a valid semantic version (e.g., '1.2.3')."
) from exc
class GitHubRateLimitError(Exception):
"""Raised when GitHub API rate limit is exceeded"""
def fetch_github_releases(repo_owner: str, repo_name: str) -> List[Dict[str, str]]:
"""
Fetch the list of releases from the GitHub API.
Handles rate limiting by logging the wait time.
"""
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases"
headers = {}
if github_token := os.getenv("GITHUB_TOKEN"):
headers["Authorization"] = f"Bearer {github_token}"
response = requests.get(url, headers=headers, timeout=5)
# Handle rate limiting
if response.status_code in (403, 429):
# Check rate limit headers
remaining = int(response.headers.get("x-ratelimit-remaining", 0))
if remaining == 0:
reset_time = int(response.headers.get("x-ratelimit-reset", 0))
message = f"Primary rate limit from Github exceeded! Please retry after: {reset_time})"
raise GitHubRateLimitError(message)
if "retry-after" in response.headers:
wait_seconds = int(response.headers["retry-after"])
message = f"Rate limit from Github exceeded! Please wait {wait_seconds} seconds before retrying."
rprint(f"[yellow]{message}[/yellow]")
raise GitHubRateLimitError(message)
response.raise_for_status()
return response.json()
class GithubRelease(TypedDict):
"""
A dictionary representing a GitHub release.
Fields:
- version: The version number of the release. (Removed the v prefix)
- tag: The tag name of the release.
- download_url: The URL to download the release.
"""
version: Optional[semver.VersionInfo]
tag: str
download_url: str
def parse_releases(releases: List[Dict[str, str]]) -> List[GithubRelease]:
"""
Parse the list of releases fetched from the GitHub API into a list of GithubRelease objects.
"""
parsed_releases: List[GithubRelease] = []
for release in releases:
tag = release["tag_name"]
if tag.lower() in ["latest", "nightly"]:
parsed_releases.append({"version": None, "download_url": release["zipball_url"], "tag": tag})
else:
version = semver.VersionInfo.parse(tag.lstrip("v"))
parsed_releases.append({"version": version, "download_url": release["zipball_url"], "tag": tag})
return parsed_releases
def select_version(releases: List[GithubRelease], version: str) -> Optional[GithubRelease]:
"""
Given a list of Github releases, select the release that matches the specified version.
"""
if version.lower() == "latest":
return next((r for r in releases if r["tag"].lower() == version.lower()), None)
version = version.lstrip("v")
try:
requested_version = semver.VersionInfo.parse(version)
return next(
(r for r in releases if isinstance(r["version"], semver.VersionInfo) and r["version"] == requested_version),
None,
)
except ValueError:
return None
def clone_comfyui(url: str, repo_dir: str):
"""
Clone the ComfyUI repository from the specified URL.
"""
if "@" in url:
# clone specific branch
url, branch = url.rsplit("@", 1)
subprocess.run(["git", "clone", "-b", branch, url, repo_dir], check=True)
else:
subprocess.run(["git", "clone", url, repo_dir], check=True)
def checkout_stable_comfyui(version: str, repo_dir: str):
"""
Supports installing stable releases of Comfy (semantic versioning) or the 'latest' version.
"""
rprint(f"Looking for ComfyUI version '{version}'...")
selected_release = None
if version == "latest":
selected_release = get_latest_release("comfyanonymous", "ComfyUI")
else:
releases = fetch_github_releases("comfyanonymous", "ComfyUI")
parsed_releases = parse_releases(releases)
selected_release = select_version(parsed_releases, version)
if selected_release is None:
rprint(f"Error: No release found for version '{version}'.")
sys.exit(1)
tag = str(selected_release["tag"])
console.print(
Panel(
f"Checking out ComfyUI version: [bold cyan]{selected_release['tag']}[/bold cyan]",
title="[yellow]ComfyUI Checkout[/yellow]",
border_style="green",
expand=False,
)
)
with console.status("[bold green]Checking out tag...", spinner="dots"):
success = git_checkout_tag(repo_dir, tag)
if not success:
console.print("\n[bold red]Failed to checkout tag![/bold red]")
sys.exit(1)
def get_latest_release(repo_owner: str, repo_name: str) -> Optional[GithubRelease]:
"""
Fetch the latest release information from GitHub API.
:param repo_owner: The owner of the repository
:param repo_name: The name of the repository
:return: A dictionary containing release information, or None if failed
"""
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/latest"
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
data = response.json()
return GithubRelease(
tag=data["tag_name"],
version=semver.VersionInfo.parse(data["tag_name"].lstrip("v")),
download_url=data["zipball_url"],
)
except requests.RequestException as e:
rprint(f"Error fetching latest release: {e}")
return None