Fix checking for updates (#674)

* Because the application version seems to be a generated value, a different
method needed to be devised to try and determine what the current version
nuber if. We now attempt scan the github release tags for the latest release
version. If a newer version is detected, the application will display the
current version and direct the user to click the link to view the latest
release page.

* Rather, should report error against TAGS_URL and not LATEST_URL at this stage.
pull/687/head
t52ta6ek 2023-08-08 14:30:25 +02:00 zatwierdzone przez GitHub
rodzic 2f8c5346eb
commit 96dd23211a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 58 dodań i 39 usunięć

Wyświetl plik

@ -23,10 +23,6 @@ try:
except LookupError: except LookupError:
from NanoVNASaver._version import version from NanoVNASaver._version import version
VERSION_URL = (
"https://github.com/NanoVNA-Saver/nanovna-saver/raw/main/src/NanoVNASaver/About.py"
)
INFO_URL = "https://github.com/NanoVNA-Saver/nanovna-saver" INFO_URL = "https://github.com/NanoVNA-Saver/nanovna-saver"
INFO = f"""NanoVNASaver {version} INFO = f"""NanoVNASaver {version}
@ -39,4 +35,7 @@ This program is licensed under the GNU General Public License version 3
See {INFO_URL} for further details. See {INFO_URL} for further details.
""" """
RELEASE_URL = "https://github.com/NanoVNA-Saver/nanovna-saver" TAGS_URL = "https://github.com/NanoVNA-Saver/nanovna-saver/tags"
TAGS_KEY = "/NanoVNA-Saver/nanovna-saver/releases/tag/v"
LATEST_URL = "https://github.com/NanoVNA-Saver/nanovna-saver/releases/latest"

Wyświetl plik

@ -16,14 +16,17 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import contextlib import contextlib
import logging import logging
import re
from time import strftime, localtime from time import strftime, localtime
from urllib import request, error from urllib import request, error
from PyQt6 import QtWidgets, QtCore, QtGui from PyQt6 import QtWidgets, QtCore, QtGui
from NanoVNASaver.About import VERSION_URL, INFO_URL from NanoVNASaver.About import INFO_URL, LATEST_URL, TAGS_URL, TAGS_KEY
from NanoVNASaver.Version import Version from NanoVNASaver.Version import Version
from NanoVNASaver.Windows.Defaults import make_scrollable from NanoVNASaver.Windows.Defaults import make_scrollable
@ -126,23 +129,30 @@ class AboutWindow(QtWidgets.QWidget):
"NanoVNA Firmware Version: Not connected." "NanoVNA Firmware Version: Not connected."
) )
# attempt to scan the TAGS_URL web page for something that looks like
# a version tag. assume the first match with a line containing the TAGS_KEY
# will contain the latest version substring since it appears at the top
# of the web page.
#
# this routine can also allow the application to automatically perform a
# check-for-updates and display a pop-up if any are found when this
# function is called with automatic=True.
def findUpdates(self, automatic=False): def findUpdates(self, automatic=False):
latest_version = Version()
latest_url = ""
try: try:
req = request.Request(VERSION_URL) req = request.Request(TAGS_URL)
req.add_header("User-Agent", f"NanoVNASaver/{self.app.version}") req.add_header("User-Agent", f"NanoVNASaver/{self.app.version}")
for line in request.urlopen(req, timeout=3): for line in request.urlopen(req, timeout=3):
line = line.decode("utf-8") line = line.decode("utf-8")
if line.startswith("VERSION ="): found_latest_version = TAGS_KEY in line
latest_version = Version(line[8:].strip(" \"'")) if found_latest_version:
if line.startswith("RELEASE_URL ="): latest_version = Version(re.search("(\d+\.\d+\.\d+)", line).group())
latest_url = line[13:].strip(" \"'") break
except error.HTTPError as e: except error.HTTPError as e:
logger.exception( logger.exception(
"Checking for updates produced an HTTP exception: %s", e "Checking for updates produced an HTTP exception: %s", e
) )
self.updateLabel.setText(f"{e}\n{VERSION_URL}") self.updateLabel.setText(f"{e}\n{TAGS_URL}")
return return
except TypeError as e: except TypeError as e:
logger.exception( logger.exception(
@ -157,35 +167,45 @@ class AboutWindow(QtWidgets.QWidget):
self.updateLabel.setText("Connection error.") self.updateLabel.setText("Connection error.")
return return
logger.info("Latest version is %s", latest_version) if found_latest_version:
this_version = Version(self.app.version) logger.info("Latest version is %s", latest_version)
logger.info("This is %s", this_version) this_version = Version(self.app.version)
if latest_version > this_version: logger.info("This is %s", this_version)
logger.info("New update available: %s!", latest_version) if latest_version > this_version:
if automatic: logger.info("New update available: %s!", latest_version)
QtWidgets.QMessageBox.information( if automatic:
self, QtWidgets.QMessageBox.information(
"Updates available", self,
f"There is a new update for NanoVNASaver available!\n" "Update available",
f"Version {latest_version}\n\n" f"There is a new update for NanoVNASaver available!\n"
f'Press "About" to find the update.', f"Version {latest_version}\n\n"
f'Press "About ..." to find the update.',
)
else:
QtWidgets.QMessageBox.information(
self,
"Update available",
"There is a new update for NanoVNASaver available!\n"
f"Version {latest_version}\n\n",
)
self.updateLabel.setText(
f'<a href="{LATEST_URL}">View release page for version {latest_version} in browser</a>'
) )
self.updateLabel.setOpenExternalLinks(True)
else: else:
QtWidgets.QMessageBox.information( # Probably don't show a message box, just update the screen?
self, # Maybe consider showing it if not an automatic update.
"Updates available", #
"There is a new update for NanoVNASaver available!", self.updateLabel.setText(
f"NanoVNASaver is up to date as of: "
f"{strftime('%Y-%m-%d %H:%M:%S', localtime())}"
) )
self.updateLabel.setText(
f'<a href="{latest_url}">New version available</a>.'
)
self.updateLabel.setOpenExternalLinks(True)
else: else:
# Probably don't show a message box, just update the screen? # not good. was not able to find TAGS_KEY in file in TAGS_URL content!
# Maybe consider showing it if not an automatic update. # if we get here, something may have changed in the way github creates
# # the .../latest web page.
self.updateLabel.setText( self.updateLabel.setText(
f"Last checked: " f"ERROR - Unable to determine what the latest version is! "
f"{strftime('%Y-%m-%d %H:%M:%S', localtime())}"
) )
logger.error(f"Can't find {TAGS_KEY} in {TAGS_URL} content.")
return return