Skip to content

Commit 91c2232

Browse files
committed
modified: use constants.DEFAULT_CONFIG instead of venv/comfy-cli
modified: __COMFY_CLI_SESSION__ is changed based on uuid. restore: comfy recent_path feature feat: comfy launch --recent feat: comfy set-default <path> fix: add `mixpanel` into pyproject.toml dependencies
1 parent b4cf1b0 commit 91c2232

File tree

6 files changed

+93
-33
lines changed

6 files changed

+93
-33
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,28 @@ Comfy provides commands that allow you to easily run the installed ComfyUI.
4141

4242
`comfy launch --workspace <path>`
4343

44-
- To run ComfyUI from the current directory, if you are inside the ComfyUI repository:
45-
46-
`comfy launch`
47-
48-
- To execute the ComfyUI that was last run or last installed, if you are outside of the ComfyUI repository: [TODO: NEED TO DISCUSS]
44+
- To execute commands automatically, prioritize without specifying a path:
45+
- 1st: To run ComfyUI from the current directory, if you are inside the ComfyUI repository
46+
- 2nd: To run ComfyUI from the default workspace, if you are inside the ComfyUI repository and default workspace is set
47+
- 3rd: To execute the ComfyUI that was last run or last installed, if you are outside of the ComfyUI repository
4948

5049
`comfy launch`
5150

51+
- To execute the ComfyUI that was last run or last installed
52+
- If the `--workspace` option is provided, the `--recent` option is ignored.):
53+
54+
`comfy launch --recent`
55+
5256
- To run with default ComfyUI options:
5357

5458
`comfy launch -- <extra args...>`
5559

5660
`comfy launch -- --cpu --listen 0.0.0.0`
5761

62+
- To set default workspace:
63+
64+
`comfy set-default <workspace path>`
65+
5866

5967
### Managing Packages [WIP]
6068

comfy_cli/cmdline.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from comfy_cli import env_checker
1818
from rich.console import Console
1919
import time
20+
import uuid
21+
2022

2123
app = typer.Typer()
2224

@@ -119,14 +121,8 @@ def validate_comfyui(_env_checker):
119121

120122

121123
def launch_comfyui(_env_checker, extra):
122-
# TODO(yoland/data): Disabled config writing for now, checking with @data
123-
# We need to find a viable place to write config file, e.g. standard place
124-
# for macOS: ~/Library/Application Support/
125-
# for linx: ~/.config/
126-
# for windows: C:\Users\<username>\AppData\Local\
127-
#_env_checker.config['DEFAULT']['recent_path'] = os.getcwd()
128-
#_env_checker.write_config()
129-
124+
_env_checker.config['DEFAULT']['recent_path'] = os.getcwd()
125+
_env_checker.write_config()
130126

131127
validate_comfyui(_env_checker)
132128

@@ -136,8 +132,11 @@ def launch_comfyui(_env_checker, extra):
136132
new_env = os.environ.copy()
137133

138134
if env_path is not None:
139-
new_env['__COMFY_CLI_SESSION__'] = os.path.join(env_path, 'comfy-cli')
140-
reboot_path = os.path.join(env_path, 'comfy-cli', '.reboot')
135+
session_path = os.path.join(_env_checker.get_config_path(), 'tmp', str(uuid.uuid4()))
136+
new_env['__COMFY_CLI_SESSION__'] = session_path
137+
138+
# To minimize the possibility of leaving residue in the tmp directory, use files instead of directories.
139+
reboot_path = os.path.join(session_path + '.reboot')
141140

142141
extra = extra if extra is not None else []
143142

@@ -150,15 +149,16 @@ def launch_comfyui(_env_checker, extra):
150149
os.remove(reboot_path)
151150

152151

153-
@app.command(help="Launch ComfyUI: ?[--workspace <path>] ?[-- <extra args ...>]")
152+
@app.command(help="Launch ComfyUI: ?[--workspace <path>] ?[--recent] ?[-- <extra args ...>]")
154153
@tracking.track_command()
155154
def launch(workspace: Annotated[str, typer.Option(show_default=False, help="Path to ComfyUI workspace")] = None,
155+
recent: Annotated[bool, typer.Option(help="Launch from recent path (--workspace is higher)")] = False,
156156
extra: List[str] = typer.Argument(None)):
157157

158158
_env_checker = EnvChecker()
159159

160160
if workspace is not None:
161-
comfyui_path = os.path.join(workspace, 'ComfyUI')
161+
comfyui_path = os.path.join(os.path.expanduser(workspace), 'ComfyUI')
162162
if os.path.exists(comfyui_path):
163163
os.chdir(comfyui_path)
164164
_env_checker.check() # update env
@@ -169,11 +169,20 @@ def launch(workspace: Annotated[str, typer.Option(show_default=False, help="Path
169169
print(f"\nInvalid ComfyUI not found in specified workspace: {workspace}\n", file=sys.stderr)
170170
raise typer.Exit(code=1)
171171

172-
elif _env_checker.comfy_repo is not None:
172+
elif not recent and _env_checker.comfy_repo is not None:
173173
os.chdir(_env_checker.comfy_repo.working_dir)
174174
print(f"\nLaunch ComfyUI from current repo: {_env_checker.comfy_repo.working_dir}\n")
175175
launch_comfyui(_env_checker, extra)
176176

177+
elif not recent and _env_checker.config['DEFAULT'].get('default_workspace') is not None:
178+
comfy_path = os.path.join(_env_checker.config['DEFAULT'].get('default_workspace'), 'ComfyUI')
179+
print(f"\nLaunch ComfyUI from default workspace: {comfy_path}\n")
180+
181+
os.chdir(comfy_path)
182+
_env_checker.check() # update env
183+
184+
launch_comfyui(_env_checker, extra)
185+
177186
elif _env_checker.config['DEFAULT'].get('recent_path') is not None:
178187
comfy_path = _env_checker.config['DEFAULT'].get('recent_path')
179188
print(f"\nLaunch ComfyUI from recent repo: {comfy_path}\n")
@@ -182,11 +191,26 @@ def launch(workspace: Annotated[str, typer.Option(show_default=False, help="Path
182191
_env_checker.check() # update env
183192

184193
launch_comfyui(_env_checker, extra)
194+
185195
else:
186196
print("\nComfyUI is not available.\nTo install ComfyUI, you can run:\n\n\tcomfy install\n\n", file=sys.stderr)
187197
raise typer.Exit(code=1)
188198

189199

200+
@app.command("set-default", help="Set default workspace")
201+
@tracking.track_command()
202+
def set_default(workspace_path: str):
203+
workspace_path = os.path.expanduser(workspace_path)
204+
comfy_path = os.path.join(workspace_path, 'ComfyUI')
205+
if not os.path.exists(comfy_path):
206+
print(f"Invalid workspace path: {workspace_path}\nThe workspace path must contain 'ComfyUI'.")
207+
raise typer.Exit(code=1)
208+
209+
_env_checker = EnvChecker()
210+
_env_checker.config['DEFAULT']['default_workspace'] = workspace_path
211+
_env_checker.write_config()
212+
213+
190214
@app.command(help="Print out current environment variables.")
191215
@tracking.track_command()
192216
def env():

comfy_cli/command/custom_nodes/command.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import subprocess
88
import sys
99
from rich import print
10+
import uuid
1011

1112
app = typer.Typer()
1213
manager_app = typer.Typer()
@@ -53,7 +54,8 @@ def execute_cm_cli(args, channel=None, mode=None, workspace=None):
5354
env_path = _env_checker.get_isolated_env()
5455
new_env = os.environ.copy()
5556
if env_path is not None:
56-
new_env['__COMFY_CLI_SESSION__'] = os.path.join(env_path, 'comfy-cli')
57+
session_path = os.path.join(_env_checker.get_config_path(), 'tmp', str(uuid.uuid4()))
58+
new_env['__COMFY_CLI_SESSION__'] = session_path
5759
new_env['COMFYUI_PATH'] = comfyui_path
5860

5961
subprocess.run(cmd, env=new_env)

comfy_cli/constants.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from enum import Enum
22
import os
33

4+
45
class OS(Enum):
56
WINDOWS = 'windows'
67
MACOS = 'macos'
78
LINUX = 'linux'
89

10+
911
COMFY_GITHUB_URL = 'https://github.com/comfyanonymous/ComfyUI'
1012
COMFY_MANAGER_GITHUB_URL = 'https://github.com/ltdrdata/ComfyUI-Manager'
1113

@@ -15,6 +17,12 @@ class OS(Enum):
1517
OS.LINUX: os.path.join(os.path.expanduser('~'), 'ComfyUI'),
1618
}
1719

20+
DEFAULT_CONFIG = {
21+
OS.WINDOWS: os.path.join(os.path.expanduser('~'), 'AppData', 'Local', 'comfy-cli'),
22+
OS.MACOS: os.path.join(os.path.expanduser('~'), 'Library', 'Application Support', 'comfy-cli'),
23+
OS.LINUX: os.path.join(os.path.expanduser('~'), '.config', "comfy-cli"),
24+
}
25+
1826
# TODO: figure out a better way to check if this is a comfy repo
1927
COMFY_ORIGIN_URL_CHOICES = [
2028
"git@github.com:comfyanonymous/ComfyUI.git",

comfy_cli/env_checker.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
import sys
77
import git
8-
from rich import print
98
from rich.console import Console
109
from rich.table import Table
1110
import requests
@@ -122,19 +121,24 @@ def get_isolated_env(self):
122121

123122
return None
124123

124+
def get_os(self):
125+
if 'win' in sys.platform:
126+
return constants.OS.WINDOWS
127+
elif sys.platform == 'darwin':
128+
return constants.OS.MACOS
129+
130+
return constants.OS.LINUX
131+
125132
def get_config_path(self):
126-
env_path = self.get_isolated_env()
127-
if env_path:
128-
return os.path.join(env_path, 'comfy-cli', 'config.json')
129-
return None
133+
return constants.DEFAULT_CONFIG[self.get_os()]
130134

131135
def write_config(self):
132-
env_path = self.get_isolated_env()
133-
cli_path = os.path.join(env_path, 'comfy-cli')
134-
if not os.path.exists(cli_path):
135-
os.mkdir(cli_path)
136+
config_file_path = os.path.join(self.get_config_path(), 'config.ini')
137+
dir_path = os.path.dirname(config_file_path)
138+
if not os.path.exists(dir_path):
139+
os.mkdir(dir_path)
136140

137-
with open(self.get_config_path(), 'w') as configfile:
141+
with open(config_file_path, 'w') as configfile:
138142
self.config.write(configfile)
139143

140144
def check(self):
@@ -158,23 +162,36 @@ def check(self):
158162
self.currently_in_comfy_repo = False
159163
self.comfy_repo = None
160164

161-
config_path = self.get_config_path()
162-
if os.path.exists(config_path):
165+
config_file_path = os.path.join(self.get_config_path(), 'config.ini')
166+
if os.path.exists(config_file_path):
163167
self.config = configparser.ConfigParser()
164-
self.config.read(config_path)
168+
self.config.read(config_file_path)
169+
170+
# TODO: We need a policy for clearing the tmp directory.
171+
tmp_path = os.path.join(self.get_config_path(), 'tmp')
172+
if not os.path.exists(tmp_path):
173+
os.makedirs(tmp_path)
165174

166175
def print(self):
167176
table = Table(":laptop_computer: Environment", "Value")
168177
table.add_row("Python Version", format_python_version(sys.version_info))
169178
table.add_row("Python Executable", sys.executable)
170179
table.add_row("Virtualenv Path", self.virtualenv_path if self.virtualenv_path else "Not Used")
171180
table.add_row("Conda Env", self.conda_env if self.conda_env else "Not Used")
172-
if self.config.has_section('DEFAULT') and self.config.has_option('DEFAULT', 'recent_path'):
181+
182+
if self.config.has_option('DEFAULT', 'default_workspace'):
183+
table.add_row("Default ComfyUI workspace", self.config['DEFAULT']['default_workspace'])
184+
else:
185+
table.add_row("Default ComfyUI workspace", "No default ComfyUI workspace")
186+
187+
if self.config.has_option('DEFAULT', 'recent_path'):
173188
table.add_row("Recent ComfyUI", self.config['DEFAULT']['recent_path'])
174189
else:
175190
table.add_row("Recent ComfyUI", "No recent run")
191+
176192
if check_comfy_server_running():
177193
table.add_row("Comfy Server Running", "[bold green]Yes[/bold green]\nhttp://localhost:8188")
178194
else:
179195
table.add_row("Comfy Server Running", "[bold red]No[/bold red]")
196+
180197
console.print(table)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies = [
2525
"requests",
2626
"pyyaml",
2727
"typing-extensions",
28+
"mixpanel",
2829
]
2930

3031
classifiers = [

0 commit comments

Comments
 (0)