-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathcmdline.py
More file actions
625 lines (539 loc) · 20.1 KB
/
cmdline.py
File metadata and controls
625 lines (539 loc) · 20.1 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import os
import subprocess
import sys
import webbrowser
from typing import Optional
import questionary
import typer
from rich import print as rprint
from rich.console import Console
from typing_extensions import Annotated, List
from comfy_cli import constants, env_checker, logging, tracking, ui, utils
from comfy_cli.command import custom_nodes
from comfy_cli.command import install as install_inner
from comfy_cli.command import run as run_inner
from comfy_cli.command.install import validate_version
from comfy_cli.command.launch import launch as launch_command
from comfy_cli.command.models import models as models_command
from comfy_cli.config_manager import ConfigManager
from comfy_cli.constants import GPU_OPTION, CUDAVersion
from comfy_cli.env_checker import EnvChecker
from comfy_cli.standalone import StandalonePython
from comfy_cli.update import check_for_updates
from comfy_cli.uv import DependencyCompiler
from comfy_cli.workspace_manager import WorkspaceManager, check_comfy_repo
logging.setup_logging()
app = typer.Typer()
workspace_manager = WorkspaceManager()
console = Console()
def main():
app()
class MutuallyExclusiveValidator:
def __init__(self):
self.group = []
def reset_for_testing(self):
self.group.clear()
def validate(self, _ctx: typer.Context, param: typer.CallbackParam, value: str):
# Add cli option to group if it was called with a value
if value is not None and param.name not in self.group:
self.group.append(param.name)
if len(self.group) > 1:
raise typer.BadParameter(f"option `{param.name}` is mutually exclusive with option `{self.group.pop()}`")
return value
g_exclusivity = MutuallyExclusiveValidator()
g_gpu_exclusivity = MutuallyExclusiveValidator()
@app.command(help="Display help for commands")
def help(ctx: typer.Context):
rprint(ctx.find_root().get_help())
ctx.exit(0)
@app.callback(invoke_without_command=True)
def entry(
ctx: typer.Context,
workspace: Annotated[
Optional[str],
typer.Option(
show_default=False,
help="Path to ComfyUI workspace",
callback=g_exclusivity.validate,
),
] = None,
recent: Annotated[
Optional[bool],
typer.Option(
show_default=False,
is_flag=True,
help="Execute from recent path",
callback=g_exclusivity.validate,
),
] = None,
here: Annotated[
Optional[bool],
typer.Option(
show_default=False,
is_flag=True,
help="Execute from current path",
callback=g_exclusivity.validate,
),
] = None,
skip_prompt: Annotated[
bool,
typer.Option(
show_default=False,
is_flag=True,
help="Do not prompt user for input, use default options",
),
] = False,
enable_telemetry: Annotated[
bool,
typer.Option(
show_default=False,
hidden=True,
is_flag=True,
help="Enable tracking",
),
] = False,
version: bool = typer.Option(
False,
"--version",
"-v",
help="Print version and exit",
is_flag=True,
),
):
if version:
rprint(ConfigManager().get_cli_version())
ctx.exit(0)
workspace_manager.setup_workspace_manager(workspace, here, recent, skip_prompt)
tracking.prompt_tracking_consent(skip_prompt, default_value=enable_telemetry)
if ctx.invoked_subcommand is None:
rprint("[bold yellow]Welcome to Comfy CLI![/bold yellow]: https://github.com/Comfy-Org/comfy-cli")
rprint(ctx.get_help())
ctx.exit()
# TODO: Move this to proper place
# start_time = time.time()
# workspace_manager.scan_dir()
# end_time = time.time()
#
# logging.info(f"scan_dir took {end_time - start_time:.2f} seconds to run")
def validate_commit_and_version(commit: Optional[str], ctx: typer.Context) -> Optional[str]:
"""
Validate that the commit is not specified unless the version is 'nightly'.
"""
version = ctx.params.get("version")
if commit and version != "nightly":
raise typer.BadParameter("You can only specify the commit if the version is 'nightly'.")
return commit
@app.command(help="Download and install ComfyUI and ComfyUI-Manager")
@tracking.track_command()
def install(
url: Annotated[
str,
typer.Option(
show_default=False,
help="url or local path pointing to the ComfyUI core git repo to be installed. A specific branch can optionally be specified using a setuptools-like syntax, eg https://foo.git@bar",
),
] = constants.COMFY_GITHUB_URL,
version: Annotated[
str,
typer.Option(
show_default=False,
help="Specify version of ComfyUI to install. Default is nightl, which is the latest commit on master branch. Other options include: latest, which is the latest stable release. Or a specific version number, eg. 0.2.0",
callback=validate_version,
),
] = "nightly",
restore: Annotated[
bool,
typer.Option(
show_default=False,
help="Restore dependencies for installed ComfyUI if not installed",
),
] = False,
skip_torch_or_directml: Annotated[
bool,
typer.Option(show_default=False, help="Skip installing PyTorch Or DirectML"),
] = False,
skip_requirement: Annotated[
bool, typer.Option(show_default=False, help="Skip installing requirements.txt")
] = False,
nvidia: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Install for Nvidia gpu",
callback=g_gpu_exclusivity.validate,
),
] = None,
cuda_version: Annotated[CUDAVersion, typer.Option(show_default=True)] = CUDAVersion.v12_6,
amd: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Install for AMD gpu",
callback=g_gpu_exclusivity.validate,
),
] = None,
m_series: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Install for Mac M-Series gpu",
callback=g_gpu_exclusivity.validate,
),
] = None,
intel_arc: Annotated[
Optional[bool],
typer.Option(
hidden=True,
show_default=False,
help="(Beta support) install for Intel Arc gpu, based on https://github.com/comfyanonymous/ComfyUI/pull/3439",
callback=g_gpu_exclusivity.validate,
),
] = None,
cpu: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Install for CPU",
callback=g_gpu_exclusivity.validate,
),
] = None,
commit: Annotated[
Optional[str], typer.Option(help="Specify commit hash for ComfyUI", callback=validate_commit_and_version)
] = None,
fast_deps: Annotated[
bool,
typer.Option(
"--fast-deps",
show_default=False,
help="Use new fast dependency installer",
),
] = False,
):
check_for_updates()
checker = EnvChecker()
comfy_path, _ = workspace_manager.get_workspace_path()
is_comfy_installed_at_path, repo_dir = check_comfy_repo(comfy_path)
if is_comfy_installed_at_path and not restore:
rprint(f"[bold red]ComfyUI is already installed at the specified path:[/bold red] {comfy_path}\n")
rprint(
"[bold yellow]If you want to restore dependencies, add the '--restore' option.[/bold yellow]",
)
raise typer.Exit(code=1)
if repo_dir is not None:
comfy_path = str(repo_dir.working_dir)
if checker.python_version.major < 3 or checker.python_version.minor < 9:
rprint("[bold red]Python version 3.9 or higher is required to run ComfyUI.[/bold red]")
rprint(f"You are currently using Python version {env_checker.format_python_version(checker.python_version)}.")
platform = utils.get_os()
if cpu:
rprint("[bold yellow]Installing for CPU[/bold yellow]")
install_inner.execute(
url,
comfy_path,
restore,
commit=commit,
version=version,
gpu=None,
cuda_version=cuda_version,
plat=platform,
skip_torch_or_directml=skip_torch_or_directml,
skip_requirement=skip_requirement,
fast_deps=fast_deps,
)
rprint(f"ComfyUI is installed at: {comfy_path}")
return None
if nvidia and platform == constants.OS.MACOS:
rprint("[bold red]Nvidia GPU is never on MacOS. What are you smoking? 🤔[/bold red]")
raise typer.Exit(code=1)
if platform != constants.OS.MACOS and m_series:
rprint(f"[bold red]You are on {platform} bruh [/bold red]")
gpu = None
if nvidia:
gpu = GPU_OPTION.NVIDIA
elif amd:
gpu = GPU_OPTION.AMD
elif m_series:
gpu = GPU_OPTION.MAC_M_SERIES
elif intel_arc:
gpu = GPU_OPTION.INTEL_ARC
else:
if platform == constants.OS.MACOS:
gpu = ui.prompt_select_enum(
"What type of Mac do you have?",
[GPU_OPTION.MAC_M_SERIES, GPU_OPTION.MAC_INTEL],
)
else:
gpu = ui.prompt_select_enum(
"What GPU do you have?",
[GPU_OPTION.NVIDIA, GPU_OPTION.AMD, GPU_OPTION.INTEL_ARC],
)
if gpu == GPU_OPTION.INTEL_ARC:
rprint("[bold yellow]Installing on Intel ARC is not yet completely supported[/bold yellow]")
env_check = env_checker.EnvChecker()
if env_check.conda_env is None:
rprint("[bold red]Intel ARC support requires conda environment to be activated.[/bold red]")
raise typer.Exit(code=1)
if intel_arc is None:
confirm_result = ui.prompt_confirm_action(
"Are you sure you want to try beta install feature on Intel ARC?", True
)
if not confirm_result:
raise typer.Exit(code=0)
rprint("[bold yellow]Installing on Intel ARC is in beta stage.[/bold yellow]")
if gpu is None and not cpu:
rprint(
"[bold red]No GPU option selected or `--cpu` enabled, use --\\[gpu option] flag (e.g. --nvidia) to pick GPU. use `--cpu` to install for CPU. Exiting...[/bold red]"
)
raise typer.Exit(code=1)
install_inner.execute(
url,
comfy_path,
restore,
commit=commit,
gpu=gpu,
version=version,
cuda_version=cuda_version,
plat=platform,
skip_torch_or_directml=skip_torch_or_directml,
skip_requirement=skip_requirement,
fast_deps=fast_deps,
)
rprint(f"ComfyUI is installed at: {comfy_path}")
@app.command(help="Update ComfyUI Environment [all|comfy]")
@tracking.track_command()
def update(
target: str = typer.Argument(
"comfy",
help="[all|comfy]",
autocompletion=utils.create_choice_completer(["all", "comfy"]),
),
):
if target not in ["all", "comfy"]:
typer.echo(
f"Invalid target: {target}. Allowed targets are 'all', 'comfy'.",
err=True,
)
raise typer.Exit(code=1)
comfy_path = workspace_manager.workspace_path
if "all" == target:
custom_nodes.command.execute_cm_cli(["update", "all"])
else:
rprint(f"Updating ComfyUI in {comfy_path}...")
if comfy_path is None:
rprint("ComfyUI path is not found.")
raise typer.Exit(code=1)
os.chdir(comfy_path)
subprocess.run(["git", "pull"], check=True)
subprocess.run(
[sys.executable, "-m", "pip", "install", "-r", "requirements.txt"],
check=True,
)
custom_nodes.command.update_node_id_cache()
@app.command(help="Run API workflow file using the ComfyUI launched by `comfy launch --background`")
@tracking.track_command()
def run(
workflow: Annotated[str, typer.Option(help="Path to the workflow API json file.")],
wait: Annotated[
bool,
typer.Option(help="If the command should wait until execution completes."),
] = True,
verbose: Annotated[
bool,
typer.Option(help="Enables verbose output of the execution process."),
] = False,
host: Annotated[
Optional[str],
typer.Option(help="The IP/hostname where the ComfyUI instance is running, e.g. 127.0.0.1 or localhost."),
] = None,
port: Annotated[
Optional[int],
typer.Option(help="The port where the ComfyUI instance is running, e.g. 8188."),
] = None,
timeout: Annotated[
Optional[int],
typer.Option(help="The timeout in seconds for the workflow execution."),
] = 30,
):
config = ConfigManager()
if host:
s = host.split(":")
host = s[0]
if not port and len(s) == 2:
port = int(s[1])
local_paths = False
if config.background:
if not host:
host = config.background[0]
local_paths = True
if port:
local_paths = False
else:
port = config.background[1]
if not host:
host = "127.0.0.1"
if not port:
port = 8188
run_inner.execute(workflow, host, port, wait, verbose, local_paths, timeout)
def validate_comfyui(_env_checker):
if _env_checker.comfy_repo is None:
rprint("[bold red]If ComfyUI is not installed, this feature cannot be used.[/bold red]")
raise typer.Exit(code=1)
@app.command(help="Stop background ComfyUI")
@tracking.track_command()
def stop():
if constants.CONFIG_KEY_BACKGROUND not in ConfigManager().config["DEFAULT"]:
rprint("[bold red]No ComfyUI is running in the background.[/bold red]\n")
raise typer.Exit(code=1)
bg_info = ConfigManager().background
if not bg_info:
rprint("[bold red]No ComfyUI is running in the background.[/bold red]\n")
raise typer.Exit(code=1)
is_killed = utils.kill_all(bg_info[2])
if not is_killed:
rprint("[bold red]Failed to stop ComfyUI in the background.[/bold red]\n")
else:
rprint(f"[bold yellow]Background ComfyUI is stopped.[/bold yellow] ({bg_info[0]}:{bg_info[1]})")
ConfigManager().remove_background()
@app.command(help="Launch ComfyUI: ?[--background] ?[-- <extra args ...>]")
@tracking.track_command()
def launch(
background: Annotated[bool, typer.Option(help="Launch ComfyUI in background")] = False,
extra: List[str] = typer.Argument(None),
):
launch_command(background, extra)
@app.command("set-default", help="Set default ComfyUI path")
@tracking.track_command()
def set_default(
workspace_path: str,
launch_extras: Annotated[str, typer.Option(help="Specify extra options for launch")] = "",
):
comfy_path = os.path.abspath(os.path.expanduser(workspace_path))
if not os.path.exists(comfy_path):
rprint(
f"\nPath not found: {comfy_path}.\n",
file=sys.stderr,
)
raise typer.Exit(code=1)
is_comfy_repo, comfy_repo = check_comfy_repo(comfy_path)
if not is_comfy_repo:
rprint(
f"\nSpecified path is not a ComfyUI path: {comfy_path}.\n",
file=sys.stderr,
)
raise typer.Exit(code=1)
comfy_path = comfy_repo.working_dir
rprint(f"Specified path is set as default ComfyUI path: {comfy_path} ")
workspace_manager.set_default_workspace(comfy_path)
workspace_manager.set_default_launch_extras(launch_extras)
@app.command(help="Show which ComfyUI is selected.")
@tracking.track_command()
def which():
comfy_path = workspace_manager.workspace_path
if comfy_path is None:
rprint(
"ComfyUI not found, please run 'comfy install', run 'comfy' in a ComfyUI directory, or specify the workspace path with '--workspace'."
)
raise typer.Exit(code=1)
rprint(f"Target ComfyUI path: {comfy_path}")
@app.command(help="Print out current environment variables.")
@tracking.track_command()
def env():
check_for_updates()
_env_checker = EnvChecker()
table = _env_checker.fill_print_table()
workspace_manager.fill_print_table(table)
console.print(table)
@app.command(hidden=True)
@tracking.track_command()
def nodes():
rprint("\n[bold red] No such command, did you mean 'comfy node' instead?[/bold red]\n")
@app.command(hidden=True)
@tracking.track_command()
def models():
rprint("\n[bold red] No such command, did you mean 'comfy model' instead?[/bold red]\n")
@app.command(help="Provide feedback on the Comfy CLI tool.")
@tracking.track_command()
def feedback():
rprint("Feedback Collection for Comfy CLI Tool\n")
# General Satisfaction
general_satisfaction_score = ui.prompt_select(
question="On a scale of 1 to 5, how satisfied are you with the Comfy CLI tool? (1 being very dissatisfied and 5 being very satisfied)",
choices=["1", "2", "3", "4", "5"],
force_prompting=True,
)
tracking.track_event("feedback_general_satisfaction", {"score": general_satisfaction_score})
# Usability and User Experience
usability_satisfaction_score = ui.prompt_select(
question="On a scale of 1 to 5, how satisfied are you with the usability and user experience of the Comfy CLI tool? (1 being very dissatisfied and 5 being very satisfied)",
choices=["1", "2", "3", "4", "5"],
force_prompting=True,
)
tracking.track_event("feedback_usability_satisfaction", {"score": usability_satisfaction_score})
# Additional Feature-Specific Feedback
if questionary.confirm("Do you want to provide additional feature-specific feedback on our GitHub page?").ask():
tracking.track_event("feedback_additional")
webbrowser.open("https://github.com/Comfy-Org/comfy-cli/issues/new/choose")
rprint("Thank you for your feedback!")
@app.command(hidden=True)
@app.command(
help="Given an existing installation of comfy core and any custom nodes, installs any needed python dependencies"
)
@tracking.track_command()
def dependency():
comfy_path, _ = workspace_manager.get_workspace_path()
depComp = DependencyCompiler(cwd=comfy_path)
depComp.compile_deps()
depComp.install_deps()
@app.command(help="Download a standalone Python interpreter and dependencies based on an existing comfyui workspace")
@tracking.track_command()
def standalone(
cli_spec: Annotated[
str,
typer.Option(
show_default=False,
help="setuptools-style requirement specificer pointing to an instance of comfy-cli",
),
] = "comfy-cli",
pack_wheels: Annotated[
bool,
typer.Option(
show_default=False,
help="Pack requirement wheels in archive when creating standalone bundle",
),
] = False,
platform: Annotated[
Optional[constants.OS],
typer.Option(
show_default=False,
help="Create standalone Python for specified platform",
),
] = None,
proc: Annotated[
Optional[constants.PROC],
typer.Option(
show_default=False,
help="Create standalone Python for specified processor",
),
] = None,
rehydrate: Annotated[
bool,
typer.Option(
show_default=False,
help="Create standalone Python for CPU",
),
] = False,
):
comfy_path, _ = workspace_manager.get_workspace_path()
platform = utils.get_os() if platform is None else platform
proc = utils.get_proc() if proc is None else proc
if rehydrate:
sty = StandalonePython.FromTarball(fpath="python.tgz")
sty.rehydrate_comfy_deps(packWheels=pack_wheels)
else:
sty = StandalonePython.FromDistro(platform=platform, proc=proc)
sty.dehydrate_comfy_deps(comfyDir=comfy_path, extraSpecs=[], packWheels=pack_wheels)
sty.to_tarball()
app.add_typer(models_command.app, name="model", help="Manage models.")
app.add_typer(custom_nodes.app, name="node", help="Manage custom nodes.")
app.add_typer(custom_nodes.manager_app, name="manager", help="Manage ComfyUI-Manager.")
app.add_typer(tracking.app, name="tracking", help="Manage analytics tracking settings.")