Skip to content

Commit 4596d7d

Browse files
committed
Refactor code to improve initialization process
1 parent c50234a commit 4596d7d

File tree

6 files changed

+99
-10
lines changed

6 files changed

+99
-10
lines changed

comfy/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from comfy.cmdline import app
1+
from comfy.cmdline import init, app
22

33
if __name__ == '__main__': # pragma: nocover
4+
init()
45
app()

comfy/cmdline.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,75 @@
22
from typing_extensions import Annotated
33
from comfy.command.models import models
44
from rich import print
5+
import os
6+
import subprocess
57

68

79
from comfy.command import custom_nodes
810
from comfy.command import install as install_inner
911
from comfy.command import run as run_inner
1012
from comfy import constants
1113
from comfy.env_checker import EnvChecker
14+
from comfy.meta_data import MetadataManager
15+
from comfy import env_checker
16+
from rich.console import Console
17+
import time
1218

1319
app = typer.Typer()
1420

21+
22+
def init():
23+
_env_checker = EnvChecker()
24+
metadata_manager = MetadataManager()
25+
start_time = time.time()
26+
metadata_manager.scan_dir()
27+
end_time = time.time()
28+
29+
print(f"scan_dir took {end_time - start_time:.2f} seconds to run")
30+
31+
1532
@app.callback(invoke_without_command=True)
1633
def no_command(ctx: typer.Context):
1734
if ctx.invoked_subcommand is None:
1835
print(ctx.get_help())
1936
ctx.exit()
2037

38+
2139
@app.command(help="Download and install ComfyUI")
2240
def install(
23-
url: str = constants.COMFY_GITHUB_URL,
24-
workspace: Annotated[str, typer.Option(help="Path to the output directory.")] = None,
25-
):
26-
install_inner.execute(url)
41+
url: Annotated[
42+
str,
43+
typer.Option(show_default=False)
44+
] = constants.COMFY_GITHUB_URL,
45+
workspace: Annotated[
46+
str,
47+
typer.Option(
48+
show_default=False,
49+
help="Path to ComfyUI workspace")
50+
] = None,
51+
):
52+
checker = EnvChecker()
53+
if checker.python_version.major < 3:
54+
print(
55+
"[bold red]Python version 3.6 or higher is required to run ComfyUI.[/bold red]"
56+
)
57+
print(
58+
f"You are currently using Python version {env_checker.format_python_version(checker.python_version)}."
59+
)
60+
if checker.currently_in_comfy_repo:
61+
console = Console()
62+
# TODO: warn user that you are teh
63+
64+
65+
install_inner.execute(url, workspace)
66+
67+
68+
def update(self):
69+
print(f"Updating ComfyUI in {self.workspace}...")
70+
os.chdir(self.workspace)
71+
subprocess.run(["git", "pull"])
72+
subprocess.run(["pip", "install", "-r", "requirements.txt"])
73+
2774

2875
@app.command(help="Run workflow file")
2976
def run(

comfy/command/install.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from rich import print
55
from comfy import env_checker
66

7-
def execute(url: str, *args, **kwargs):
7+
def execute(url: str, comfy_workspace: str, *args, **kwargs):
88
print(f"Installing from {url}")
99

1010
checker = env_checker.EnvChecker()

comfy/constants.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
"git@github.com:drip-art/comfy.git",
88
"https://github.com/comfyanonymous/ComfyUI.git",
99
"https://github.com/drip-art/ComfyUI.git",
10-
]
10+
]
11+
12+
# Referencing supported pt extension from ComfyUI
13+
# https://github.com/comfyanonymous/ComfyUI/blob/a88b0ebc2d2f933c94e42aa689c42e836eedaf3c/folder_paths.py#L5
14+
SUPPORTED_PT_EXTENSIONS = set(['.ckpt', '.pt', '.bin', '.pth', '.safetensors'])

comfy/env_checker.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def format_python_version(version_info):
3030
str: The formatted Python version string.
3131
"""
3232
if version_info.major == 3 and version_info.minor > 8:
33-
return "{}.{}".format(version_info.major, version_info.minor)
34-
return "[bold red]{}.{}[/bold red]".format(version_info.major, version_info.minor)
33+
return "{}.{}.{}".format(version_info.major, version_info.minor, version_info.micro)
34+
return "[bold red]{}.{}.{}[/bold red]".format(version_info.major, version_info.minor, version_info.micro)
3535

3636

3737
def check_comfy_server_running():
@@ -71,8 +71,9 @@ class EnvChecker(object):
7171
def __init__(self):
7272
self.virtualenv_path = None
7373
self.conda_env = None
74-
self.python_version = None
74+
self.python_version: None = None
7575
self.currently_in_comfy_repo = False
76+
self.comfy_repo = None
7677
self.check()
7778

7879
def check(self):
@@ -93,9 +94,12 @@ def check(self):
9394
self.currently_in_comfy_repo = (
9495
repo.remotes.origin.url in constants.COMFY_ORIGIN_URL_CHOICES
9596
)
97+
if self.currently_in_comfy_repo:
98+
self.comfy_repo = repo
9699
except git.exc.InvalidGitRepositoryError:
97100
self.currently_in_comfy_repo = False
98101

102+
99103
def print(self):
100104
table = Table(":laptop_computer: Environment", "Value")
101105
table.add_row("Python Version", format_python_version(sys.version_info))

comfy/meta_data.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import os
22
import yaml
3+
import concurrent.futures
4+
from pathlib import Path
5+
import os
36

47
from comfy.utils import singleton
8+
from comfy import constants
59

610

711
@singleton
@@ -14,6 +18,35 @@ def __init__(self):
1418
self.metadata_file = None
1519
self.metadata = {}
1620

21+
22+
def scan_dir(self):
23+
config_files = []
24+
for root, dirs, files in os.walk("."):
25+
for file in files:
26+
if file.endswith(constants.SUPPORTED_PT_EXTENSIONS):
27+
config_files.append(os.path.join(root, file))
28+
return config_files
29+
30+
31+
def scan_dir_concur(self):
32+
base_path = Path(".")
33+
model_files = []
34+
35+
# Function to check if the file is config.json
36+
def check_file(path):
37+
if path.name.endswith(constants.SUPPORTED_PT_EXTENSIONS):
38+
return str(path)
39+
40+
# Use ThreadPoolExecutor to manage concurrency
41+
with concurrent.futures.ThreadPoolExecutor() as executor:
42+
futures = [executor.submit(check_file, p) for p in base_path.rglob('*')]
43+
for future in concurrent.futures.as_completed(futures):
44+
if future.result():
45+
model_files.append(future.result())
46+
47+
return model_files
48+
49+
1750
def load_metadata(self):
1851
if os.path.exists(self.metadata_file):
1952
with open(self.metadata_file, "r", encoding="utf-8") as file:

0 commit comments

Comments
 (0)