Skip to content

Commit

Permalink
feat: add cli entrypoint
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Nov 15, 2024
1 parent f680263 commit ff7a55a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 54 deletions.
2 changes: 1 addition & 1 deletion nodes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def _write_snapshot(zf: zipfile.ZipFile) -> None:
stdout, _ = await proc.communicate()
with zf.open("snapshot.json", "w") as f:
data = {
"python": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
"python": f"{sys.version_info.major}.{sys.version_info.minor}",
"comfyui": stdout.decode().strip(),
"models": await _get_models(),
"custom_nodes": await _get_custom_nodes(),
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = [{ name = "Frost Ming", email = "frost@bentoml.com" }]
readme = "README.md"
requires-python = ">=3.9"
dependencies = [
"click>=8.1.7",
"comfy-cli>=1.2.8",
"pydantic>=2.9",
]
Expand All @@ -19,6 +20,9 @@ classifiers = [
[project.urls]
Homepage = "https://github.com/bentoml/ComfyUI-IDL"

[project.scripts]
comfy-idl = "comfyui_idl.cli:main"

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"
Expand Down
34 changes: 34 additions & 0 deletions src/comfyui_idl/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import click


@click.group()
def main():
"""ComfyUI IDL CLI"""


@main.command(name="install")
@click.option(
"--workspace",
"-w",
default="workspace",
help="Workspace directory",
type=click.Path(file_okay=False),
)
@click.argument("cpack", type=click.Path(exists=True, dir_okay=False))
def install_cmd(cpack: str, workspace: str):
"""
Install ComfyUI workspace from a zipped package.
Example:
# Install to the default directory(`workspace`)
$ comfyui_idl install workspace.cpack.zip
# Install to a different directory
$ comfyui_idl install -w my_workspace workspace.cpack.zip
"""
from .package import install

install(cpack, workspace)
70 changes: 18 additions & 52 deletions src/comfyui_idl/package.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,44 @@
from pathlib import Path
import shutil
import tempfile
from __future__ import annotations

import json
from typing import Union
import os
import shutil
import subprocess

import tempfile
from pathlib import Path

COMFYUI_REPO = "https://github.com/comfyanonymous/ComfyUI.git"


def _clone_commit(
url: str,
commit: str,
dir: Path,
):
if not dir.exists():
dir.mkdir(parents=True)
script = f"""cd {dir}
git init
git remote add origin {url}
git fetch --depth 1 origin {commit}
git checkout FETCH_HEAD
"""
subprocess.check_call(
script,
shell=True,
)
def _clone_commit(url: str, commit: str, dir: Path):
subprocess.check_call(["git", "clone", "--filter=blob:none", url, dir])
subprocess.check_call(["git", "fetch", "-q", url, commit], cwd=dir)
subprocess.check_call(["git", "checkout", "FETCH_HEAD"], cwd=dir)


def install_comfyui(snapshot, workspace: Path):
print("Installing ComfyUI")
comfyui_commit = snapshot["comfyui"]

_clone_commit(
COMFYUI_REPO,
comfyui_commit,
workspace,
)
_clone_commit(COMFYUI_REPO, comfyui_commit, workspace)


def install_custom_modules(snapshot, workspace: Path):
print("Installing custom nodes")
for module in snapshot["custom_nodes"]:
url = module["url"]
directory = url.split("/")[-1].split(".")[0]
module_dir = workspace / "custom_nodes" / directory

commit_hash = module["commit_hash"]
disabled = module.get("disabled", False)
# if disabled:
# continue
_clone_commit(
url,
commit_hash,
module_dir,
)
_clone_commit(url, commit_hash, module_dir)


def install_dependencies(snapshot: dict, req_file: str, workspace: Path):
print("Installing Python dependencies")
python_version = snapshot["python"]
subprocess.check_call(
[
"uv",
"python",
"install",
python_version,
],
["uv", "python", "install", python_version],
cwd=workspace,
)
venv = workspace / ".venv"
Expand Down Expand Up @@ -108,19 +82,11 @@ def install_dependencies(snapshot: dict, req_file: str, workspace: Path):
f.write("DONE")


def install(cpack: Union[str, Path]):
workspace = Path("workspace")
def install(cpack: str | Path, workspace: str | Path = "workspace") -> None:
workspace = Path(workspace)
with tempfile.TemporaryDirectory() as temp_dir:
pack_dir = Path(temp_dir) / ".cpack"
subprocess.check_call(
[
"unzip",
"-o",
cpack,
"-d",
pack_dir,
]
)
shutil.unpack_archive(cpack, pack_dir)
snapshot = json.loads((pack_dir / "snapshot.json").read_text())
req_txt_file = pack_dir / "requirements.txt"

Expand Down
4 changes: 3 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ff7a55a

Please sign in to comment.