Skip to content

Commit

Permalink
Improve type hints and checking for None cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
robinjhuang committed May 4, 2024
1 parent 54a22ea commit 8c4430b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
40 changes: 26 additions & 14 deletions comfy_cli/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import questionary
import typer
from rich import print
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.models import models as models_command
from comfy_cli.config_manager import ConfigManager
from comfy_cli.env_checker import EnvChecker, check_comfy_server_running
Expand Down Expand Up @@ -105,7 +103,9 @@ def install(
bool, typer.Option(help="Skip installing the manager component")
] = False,
amd: Annotated[bool, typer.Option(help="Install for AMD gpu")] = False,
commit: Annotated[str, typer.Option(help="Specify commit hash for ComfyUI")] = None,
commit: Annotated[
Optional[str], typer.Option(help="Specify commit hash for ComfyUI")
] = None,
):
checker = EnvChecker()

Expand All @@ -124,7 +124,7 @@ def install(
raise typer.Exit(code=1)

if repo_dir is not None:
comfy_path = repo_dir.working_dir
comfy_path = str(repo_dir.working_dir)

if checker.python_version.major < 3:
print(
Expand Down Expand Up @@ -168,6 +168,9 @@ def update(target: str = typer.Argument("comfy", help="[all|comfy]")):
custom_nodes.command.execute_cm_cli(["update", "all"])
else:
print(f"Updating ComfyUI in {comfy_path}...")
if comfy_path is None:
print("ComfyUI path is not found.")
raise typer.Exit(code=1)
os.chdir(comfy_path)
subprocess.run(["git", "pull"], check=True)
subprocess.run(
Expand All @@ -187,18 +190,17 @@ def update(target: str = typer.Argument("comfy", help="[all|comfy]")):
def validate_comfyui(_env_checker):
if _env_checker.comfy_repo is None:
print(
f"[bold red]If ComfyUI is not installed, this feature cannot be used.[/bold red]"
"[bold red]If ComfyUI is not installed, this feature cannot be used.[/bold red]"
)
raise typer.Exit(code=1)


def launch_comfyui(extra, background=False):
if background:
if ConfigManager().background is not None and utils.is_running(
ConfigManager().background[2]
):
config_background = ConfigManager().background
if config_background is not None and utils.is_running(config_background[2]):
print(
f"[bold red]ComfyUI is already running in background.\nYou cannot start more than one background service.[/bold red]\n"
"[bold red]ComfyUI is already running in background.\nYou cannot start more than one background service.[/bold red]\n"
)
raise typer.Exit(code=1)

Expand Down Expand Up @@ -260,6 +262,10 @@ def launch_comfyui(extra, background=False):
while True:
subprocess.run([sys.executable, "main.py"] + extra, env=new_env, check=False)

if not reboot_path:
print("[bold red]ComfyUI is not installed.[/bold red]\n")
return

if not os.path.exists(reboot_path):
return

Expand All @@ -270,15 +276,21 @@ def launch_comfyui(extra, background=False):
@tracking.track_command()
def stop():
if constants.CONFIG_KEY_BACKGROUND not in ConfigManager().config["DEFAULT"]:
print(f"[bold red]No ComfyUI is running in the background.[/bold red]\n")
print("[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:
print("[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])

print(
f"[bold yellow]Background ComfyUI is stopped.[/bold yellow] ({bg_info[0]}:{bg_info[1]})"
)
if not is_killed:
print("[bold red]Failed to stop ComfyUI in the background.[/bold red]\n")
else:
print(
f"[bold yellow]Background ComfyUI is stopped.[/bold yellow] ({bg_info[0]}:{bg_info[1]})"
)

ConfigManager().remove_background()

Expand Down Expand Up @@ -331,7 +343,7 @@ def which():
comfy_path = workspace_manager.workspace_path
if comfy_path is None:
print(
f"ComfyUI not found, please run 'comfy install', run 'comfy' in a ComfyUI directory, or specify the workspace path with '--workspace'."
"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)

Expand Down
14 changes: 7 additions & 7 deletions comfy_cli/config_manager.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import configparser
import os
import configparser
from comfy_cli.utils import singleton, get_os, is_running
from comfy_cli import constants
from rich import print
from typing import Optional, Tuple


@singleton
class ConfigManager(object):
def __init__(self):
self.config = configparser.ConfigParser()
self.background = None
self.background: Optional[Tuple[str, int, int]] = None
self.load()

@staticmethod
Expand Down Expand Up @@ -82,10 +81,11 @@ def fill_print_env(self, table):

if self.config.has_option("DEFAULT", "background"):
bg_info = self.background
table.add_row(
"Background ComfyUI",
f"http://{bg_info[0]}:{bg_info[1]} (pid={bg_info[2]})",
)
if bg_info:
table.add_row(
"Background ComfyUI",
f"http://{bg_info[0]}:{bg_info[1]} (pid={bg_info[2]})",
)
else:
table.add_row("Background ComfyUI", "[bold red]No[/bold red]")

Expand Down
5 changes: 1 addition & 4 deletions comfy_cli/env_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

import os
import sys
import git
from rich.console import Console
from rich.table import Table
import requests

from comfy_cli import constants
from comfy_cli.utils import singleton

from comfy_cli.config_manager import ConfigManager

console = Console()
Expand Down Expand Up @@ -76,7 +73,7 @@ class EnvChecker(object):
def __init__(self):
self.virtualenv_path = None
self.conda_env = None
self.python_version: None = None
self.python_version: sys._version_info = sys.version_info
self.check()

def is_isolated_env(self):
Expand Down

0 comments on commit 8c4430b

Please sign in to comment.