From 80c22055c74bc55d5aaa0388b3f9703b63a104d5 Mon Sep 17 00:00:00 2001 From: Obijuan Date: Tue, 13 Feb 2024 10:48:27 +0100 Subject: [PATCH] get_pypi_latest_version(): documented and refactored --- apio/commands/upgrade.py | 3 ++ apio/util.py | 71 +++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/apio/commands/upgrade.py b/apio/commands/upgrade.py index 077eb068..acc510f6 100644 --- a/apio/commands/upgrade.py +++ b/apio/commands/upgrade.py @@ -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 " diff --git a/apio/util.py b/apio/util.py index a57fe18b..ede59d6b 100644 --- a/apio/util.py +++ b/apio/util.py @@ -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: