Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for hanging when run in an off-line environment #183

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions comfy_cli/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def install(
),
] = False,
):
check_for_updates()
check_for_updates(timeout=3)
checker = EnvChecker()

comfy_path, _ = workspace_manager.get_workspace_path()
Expand Down Expand Up @@ -518,7 +518,7 @@ def which():
@app.command(help="Print out current environment variables.")
@tracking.track_command()
def env():
check_for_updates()
check_for_updates(timeout=3)
_env_checker = EnvChecker()
table = _env_checker.fill_print_table()
workspace_manager.fill_print_table(table)
Expand Down
2 changes: 1 addition & 1 deletion comfy_cli/command/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def launch(
background: bool = False,
extra: list[str] | None = None,
):
check_for_updates()
check_for_updates(timeout=3)
resolved_workspace = workspace_manager.workspace_path

if not resolved_workspace:
Expand Down
14 changes: 11 additions & 3 deletions comfy_cli/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ def set(self, key, value):
"""
Set a key-value pair in the config file.
"""
self.config["DEFAULT"][key] = value
self.config["DEFAULT"][key] = str(value)
self.write_config() # Write changes to file immediately

def get(self, key):
def get(self, key, type_cast=str):
"""
Get a value from the config file. Returns None if the key does not exist.
"""
return self.config["DEFAULT"].get(key, None) # Returns None if the key does not exist
value = self.config["DEFAULT"].get(key, None) # Returns None if the key does not exist
if value is None:
return None

# Handle boolean conversion
if type_cast == bool:
return value.lower() in ("true", "yes", "1")

return type_cast(value)

def load(self):
config_file_path = self.get_config_file_path()
Expand Down
7 changes: 3 additions & 4 deletions comfy_cli/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def track_event(event_name: str, properties: any = None):
if properties is None:
properties = {}
logging.debug(f"tracking event called with event_name: {event_name} and properties: {properties}")
enable_tracking = config_manager.get(constants.CONFIG_KEY_ENABLE_TRACKING)
enable_tracking = config_manager.get(constants.CONFIG_KEY_ENABLE_TRACKING, type_cast=bool)
if not enable_tracking:
return

Expand All @@ -56,7 +56,6 @@ def track_event(event_name: str, properties: any = None):
except Exception as e:
logging.warning(f"Failed to track event: {e}") # Log the error but do not raise


def track_command(sub_command: str = None):
"""
A decorator factory that logs the command function name and selected arguments when it's called.
Expand Down Expand Up @@ -98,7 +97,7 @@ def init_tracking(enable_tracking: bool):
Initialize the tracking system by setting the user identifier and tracking enabled status.
"""
logging.debug(f"Initializing tracking with enable_tracking: {enable_tracking}")
config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, str(enable_tracking))
config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, enable_tracking)
if not enable_tracking:
return

Expand All @@ -119,5 +118,5 @@ def init_tracking(enable_tracking: bool):


def set_tracking_enabled(enabled: bool):
config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, str(enabled))
config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, enabled)
return enabled
43 changes: 35 additions & 8 deletions comfy_cli/update.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,71 @@
import sys
import logging
from importlib.metadata import metadata

import requests
from packaging import version
from rich.console import Console
from rich.panel import Panel

# Set up a logger for the module
logger = logging.getLogger(__name__)

console = Console()


def check_for_newer_pypi_version(package_name, current_version):
def check_for_newer_pypi_version(package_name: str, current_version: str, timeout: float) -> tuple[bool, str]:
"""
Checks if a newer version of the specified package is available on PyPI.

:param package_name: The name of the package to check.
:param current_version: The current version of the package.
:param timeout: Timeout in seconds for the request to PyPI.
:return: A tuple where the first value indicates if a newer version is available,
and the second value is the latest version (or the current version if no update is found).
"""
url = f"https://pypi.org/pypi/{package_name}/json"
try:
response = requests.get(url)
response = requests.get(url, timeout=timeout)
response.raise_for_status() # Raises stored HTTPError, if one occurred
latest_version = response.json()["info"]["version"]

if version.parse(latest_version) > version.parse(current_version):
return True, latest_version

return False, current_version
except requests.RequestException:
# print(f"Error checking latest version: {e}")
except requests.RequestException as e:
logger.warning(f"Unable to fetch {package_name} version metadata from PyPI. Retaining current version {current_version}. "
f"Exception: {type(e).__name__} - {e}")
return False, current_version


def check_for_updates():
def check_for_updates(timeout: float = 10) -> None:
"""
Checks for updates to the 'comfy-cli' package by comparing the current version
to the latest version on PyPI. If a newer version is available, a notification
is displayed.

:param timeout: (default 10) Timeout in seconds for the request to check for updates.
"""
current_version = get_version_from_pyproject()
has_newer, newer_version = check_for_newer_pypi_version("comfy-cli", current_version)
has_newer, newer_version = check_for_newer_pypi_version("comfy-cli", current_version, timeout=timeout)

if has_newer:
notify_update(current_version, newer_version)


def get_version_from_pyproject():
def get_version_from_pyproject() -> str:
package_metadata = metadata("comfy-cli")
return package_metadata["Version"]


def notify_update(current_version: str, newer_version: str):
def notify_update(current_version: str, newer_version: str) -> None:
"""
Notifies the user that a newer version of the 'comfy-cli' package is available.

:param current_version: The current version of the package.
:param newer_version: The newer version available on PyPI.
"""
message = (
f":sparkles: Newer version of [bold magenta]comfy-cli[/bold magenta] is available: [bold green]{newer_version}[/bold green].\n"
f"Current version: [bold cyan]{current_version}[/bold cyan]\n"
Expand Down