diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index fa718f72..08d02e87 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -242,7 +242,7 @@ def install( ), ] = False, ): - check_for_updates() + check_for_updates(timeout=3) checker = EnvChecker() comfy_path, _ = workspace_manager.get_workspace_path() @@ -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) diff --git a/comfy_cli/command/launch.py b/comfy_cli/command/launch.py index 5d563c12..1005ef7b 100644 --- a/comfy_cli/command/launch.py +++ b/comfy_cli/command/launch.py @@ -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: diff --git a/comfy_cli/config_manager.py b/comfy_cli/config_manager.py index 616bf090..3e0608db 100644 --- a/comfy_cli/config_manager.py +++ b/comfy_cli/config_manager.py @@ -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() diff --git a/comfy_cli/tracking.py b/comfy_cli/tracking.py index da0e993e..5ca7d347 100644 --- a/comfy_cli/tracking.py +++ b/comfy_cli/tracking.py @@ -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 @@ -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. @@ -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 @@ -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 diff --git a/comfy_cli/update.py b/comfy_cli/update.py index 698ecd62..4ca126c9 100644 --- a/comfy_cli/update.py +++ b/comfy_cli/update.py @@ -1,4 +1,5 @@ import sys +import logging from importlib.metadata import metadata import requests @@ -6,13 +7,25 @@ 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"] @@ -20,25 +33,39 @@ def check_for_newer_pypi_version(package_name, 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"