Skip to content

Commit

Permalink
Add a timeout to requests on check_for_updates, default 10 but can be…
Browse files Browse the repository at this point in the history
… set optionally. added PEP 585 type hints to functions in module.
  • Loading branch information
jason-weirather committed Sep 14, 2024
1 parent 237e4bf commit 7f12c0c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
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
37 changes: 30 additions & 7 deletions comfy_cli/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@
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"]

Expand All @@ -21,24 +30,38 @@ def check_for_newer_pypi_version(package_name, current_version):

return False, current_version
except requests.RequestException:
# print(f"Error checking latest version: {e}")
# Fail quietly on timeout or any request exception
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.
If not provided, no timeout is enforced.
"""
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

0 comments on commit 7f12c0c

Please sign in to comment.