Skip to content

Commit dec98e0

Browse files
authored
fix/check-updates-fixes (#605)
1 parent 15e757c commit dec98e0

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

safety/cli.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import sys
1515
from functools import wraps
1616
from typing import Dict, Optional
17+
from packaging import version as packaging_version
18+
from packaging.version import InvalidVersion
1719

1820
import click
1921
import typer
@@ -767,7 +769,7 @@ def check_updates(ctx: typer.Context,
767769
details = [f"Organization: {organization}",
768770
f"Account: {account}",
769771
current_version,
770-
f"Latest available version: {latest_available_version}"
772+
f"Latest stable available version: {latest_available_version}"
771773
]
772774

773775
for msg in details:
@@ -776,14 +778,25 @@ def check_updates(ctx: typer.Context,
776778
console.print()
777779

778780
if latest_available_version:
779-
console.print(f"Update available: Safety version {latest_available_version}")
780-
console.print()
781-
console.print(
782-
f"If Safety was installed from a requirements file, update Safety to version {latest_available_version} in that requirements file."
783-
)
784-
console.print()
785-
# `pip -i <source_url> install safety=={latest_available_version}` OR
786-
console.print(f"Pip: To install the updated version of Safety directly via pip, run: `pip install safety=={latest_available_version}`")
781+
try:
782+
# Compare the current version and the latest available version using packaging.version
783+
if packaging_version.parse(latest_available_version) > packaging_version.parse(VERSION):
784+
console.print(f"Update available: Safety version {latest_available_version}")
785+
console.print()
786+
console.print(
787+
f"If Safety was installed from a requirements file, update Safety to version {latest_available_version} in that requirements file."
788+
)
789+
console.print()
790+
console.print(f"Pip: To install the updated version of Safety directly via pip, run: pip install safety=={latest_available_version}")
791+
elif packaging_version.parse(latest_available_version) < packaging_version.parse(VERSION):
792+
# Notify user about downgrading
793+
console.print(f"Latest stable version is {latest_available_version}. If you want to downgrade to this version, you can run: pip install safety=={latest_available_version}")
794+
else:
795+
console.print("You are already using the latest stable version of Safety.")
796+
except InvalidVersion as invalid_version:
797+
LOG.exception(f'Invalid version format encountered: {invalid_version}')
798+
console.print(f"Error: Invalid version format encountered for the latest available version: {latest_available_version}")
799+
console.print("Please report this issue or try again later.")
787800

788801
if console.quiet:
789802
console.quiet = False

0 commit comments

Comments
 (0)