Skip to content

Commit

Permalink
get_pypi_latest_version(): documented and refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Obijuan committed Feb 13, 2024
1 parent 67da020 commit 80c2205
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
3 changes: 3 additions & 0 deletions apio/commands/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ def cli(ctx):
current_version = importlib.metadata.version("apio")
latest_version = get_pypi_latest_version()

# -- There was an error getting the version from pypi
if latest_version is None:
ctx.exit(1)

print(f"DEBUG: {latest_version=}")

if latest_version == current_version:
click.secho(
f"You're up-to-date!\nApio {latest_version} is currently the "
Expand Down
71 changes: 49 additions & 22 deletions apio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,35 +594,62 @@ def _parse_result(kwargs, result):
result[k].strip()


# W0703: Catching too general exception Exception (broad-except)
# pylint: disable=W0703
def get_pypi_latest_version():
"""DOC: TODO"""
def get_pypi_latest_version() -> str:
"""Get the latest stable version of apio from Pypi
Internet connection is required
Returns: A string with the version (Ex: "0.9.0")
In case of error, it returns None
"""

req = None
version = None
# -- Error message common to all exceptions
error_msg = "Error: could not connect to Pypi\n"

# -- Read the latest apio version from pypi
# -- More information: https://warehouse.pypa.io/api-reference/json.html
try:
req = requests.get("https://pypi.python.org/pypi/apio/json", timeout=5)
version = req.json().get("info").get("version")
req.raise_for_status()
except requests.exceptions.ConnectionError as exc:
error_message = str(exc)
if "NewConnectionError" in error_message:
click.secho(
"Error: could not connect to Pypi.\n"
"Check your internet connection and try again",
fg="red",
)
else:
click.secho(error_message, fg="red")
except Exception as exc:
click.secho("Error: " + str(exc), fg="red")
finally:
if req:
req.close()

# -- Connection error
except requests.exceptions.ConnectionError as e:
click.secho(
f"\n{error_msg}" "Check your internet connection and try again\n",
fg="red",
)
print_exception_developers(e)
return None

# -- HTTP Error
except requests.exceptions.HTTPError as e:
click.secho(f"\nHTTP ERROR\n{error_msg}", fg="red")
print_exception_developers(e)
return None

# -- Timeout!
except requests.exceptions.Timeout as e:
click.secho(f"\nTIMEOUT!\n{error_msg}", fg="red")
print_exception_developers(e)
return None

# -- Another error
except requests.exceptions.RequestException as e:
click.secho(f"\nFATAL ERROR!\n{error_msg}", fg="red")
print_exception_developers(e)
return None

# -- Get the version field from the json response
version = req.json()["info"]["version"]

return version


def print_exception_developers(e):
"""Print a message for developers, caused by the exception e"""

click.secho("Info for developers:")
click.secho(f"{e}\n", fg="yellow")


def get_full_path(folder: string):
"""Get the full path to the given folder
Inputs:
Expand Down

0 comments on commit 80c2205

Please sign in to comment.