Coverage for apio/commands/apio_upgrade.py: 56%
46 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-06 10:20 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-06 10:20 +0000
1# -*- coding: utf-8 -*-
2# -- This file is part of the Apio project
3# -- (C) 2016-2024 FPGAwars
4# -- Authors
5# -- * Jesús Arroyo (2016-2019)
6# -- * Juan Gonzalez (obijuan) (2019-2024)
7# -- License GPLv2
8"""Implementation of 'apio upgrade' command"""
10import sys
11import click
12import requests
13from packaging import version
14from apio.utils import util, cmd_util
15from apio.common.apio_console import cout, cerror
16from apio.common.apio_styles import INFO, WARNING, EMPH1, EMPH3, SUCCESS
19def get_pypi_latest_version() -> str:
20 """Get the latest stable version of apio from Pypi
21 Internet connection is required
22 Returns: A string with the version (Ex: "0.9.0")
23 Exits on an error.
24 """
26 # -- Create http request headers.
27 # -- Added May 2025 when we started to get the following error:
28 # -- "406 Client Error: go-http-client redirect ..."
29 headers = {"User-Agent": "python-requests/2.31.0"}
31 # -- Read the latest apio version from pypi
32 # -- More information: https://warehouse.pypa.io/api-reference/json.html
33 try:
34 req = requests.get(
35 "https://pypi.python.org/pypi/apio/json",
36 headers=headers,
37 timeout=10,
38 )
39 req.raise_for_status()
41 # -- Connection error
42 except requests.exceptions.ConnectionError as e:
43 cout(str(e), style=WARNING)
44 cerror("Connection error while accessing Pypi.")
45 sys.exit(1)
47 # -- HTTP Error
48 except requests.exceptions.HTTPError as e:
49 cout(str(e), style=WARNING)
50 cerror("HTTP error while accessing Pypi.")
51 sys.exit(1)
53 # -- Timeout!
54 except requests.exceptions.Timeout as e:
55 cout(str(e), style=WARNING)
56 cerror("HTTP timeout while accessing Pypi.")
57 sys.exit(1)
59 # -- Another error
60 except requests.exceptions.RequestException as e:
61 cout(str(e), style=WARNING)
62 cerror("HTTP exception while accessing Pypi.")
63 sys.exit(1)
65 # -- Get the version field from the json response
66 ver = req.json()["info"]["version"]
68 return ver
71# ---------- apio upgrade
73# -- Text in the rich-text format of the python rich library.
74APIO_UPGRADE_HELP = """
75The command 'apio upgrade' checks for the version of the latest Apio release \
76and provides upgrade directions if necessary.
78Examples:[code]
79 apio upgrade[/code]
80"""
83@click.command(
84 name="upgrade",
85 cls=cmd_util.ApioCommand,
86 short_help="Check the latest Apio version.",
87 help=APIO_UPGRADE_HELP,
88)
89@click.pass_context
90def cli(_: click.Context):
91 """Check the latest Apio version."""
93 # -- Get the current apio version from the python package installed
94 # -- on the system
95 current_version = util.get_apio_version()
97 # -- Get the latest stable version published at Pypi
98 latest_version = get_pypi_latest_version()
100 # -- There was an error getting the version from pypi
101 if latest_version is None: 101 ↛ 102line 101 didn't jump to line 102 because the condition on line 101 was never true
102 sys.exit(1)
104 # -- Print information about apio.
105 cout(
106 f"Local Apio version: {current_version}",
107 f"Latest Apio stable version (Pypi): {latest_version}",
108 style=EMPH1,
109 )
111 # -- Case 1: Using an old version.
112 if version.parse(current_version) < version.parse(latest_version): 112 ↛ 113line 112 didn't jump to line 113 because the condition on line 112 was never true
113 cout(
114 "You're not up to date.",
115 "Please execute 'pip install -U apio' to upgrade.",
116 style=INFO,
117 )
118 return
120 # -- Case 2: Using a dev version.
121 if version.parse(current_version) > version.parse(latest_version): 121 ↛ 129line 121 didn't jump to line 129 because the condition on line 121 was always true
122 cout(
123 "You are using a development version. Enjoy it at your own risk.",
124 style=EMPH3,
125 )
126 return
128 # -- Case 3: Using the latest version.
129 cout(
130 f"You're up-to-date!\nApio {latest_version} is currently the "
131 "latest stable version available.",
132 style=SUCCESS,
133 )