Skip to content

Commit

Permalink
Simplify update checker
Browse files Browse the repository at this point in the history
Motivated by the distutils removal, this replaces version parsing by a
"good enough" version comparison algorithm...
  • Loading branch information
TobiX committed Oct 29, 2023
1 parent a392a41 commit af20ca7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
19 changes: 7 additions & 12 deletions dosagelib/cmd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: MIT
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2022 Tobias Gruetzmacher
# SPDX-FileCopyrightText: © 2004 Tristan Seligmann and Jonathan Jacobs
# SPDX-FileCopyrightText: © 2012 Bastian Kleineidam
# SPDX-FileCopyrightText: © 2015 Tobias Gruetzmacher
import argparse
import os
import platform
Expand Down Expand Up @@ -124,8 +124,8 @@ def display_version(verbose):
if verbose:
# search for updates
from .updater import check_update
result, value = check_update()
if result:
try:
value = check_update()
if value:
version, url = value
if url is None:
Expand All @@ -139,13 +139,8 @@ def display_version(verbose):
attrs = {'version': version, 'app': AppName,
'url': url, 'currentversion': __version__}
print(text % attrs)
else:
if value is None:
value = 'invalid update file syntax'
text = ('An error occured while checking for an '
'update of %(app)s: %(error)s.')
attrs = {'error': value, 'app': AppName}
print(text % attrs)
except (IOError, KeyError) as err:
print(f'An error occured while checking for an update of {AppName}: {err!r}')
return 0


Expand Down
49 changes: 27 additions & 22 deletions dosagelib/updater.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# SPDX-License-Identifier: MIT
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2020 Tobias Gruetzmacher
# SPDX-FileCopyrightText: © 2004 Tristan Seligmann and Jonathan Jacobs
# SPDX-FileCopyrightText: © 2012 Bastian Kleineidam
# SPDX-FileCopyrightText: © 2015 Tobias Gruetzmacher
import os

from distutils.version import LooseVersion
import re
from typing import Any

import dosagelib
from . import http
Expand All @@ -18,35 +18,34 @@
}


def check_update():
def check_update() -> None | tuple[str, None | str]:
"""Return the following values:
(False, errmsg) - online version could not be determined
(True, None) - user has newest version
(True, (version, url string)) - update available
(True, (version, None)) - current version is newer than online version
throws exception - online version could not be determined
None - user has newest version
(version, url string) - update available
(version, None) - current version is newer than online version
"""
version, value = get_online_version()
if version is None:
# value is an error message
return False, value
if version == dosagelib.__version__:
# user has newest version
return True, None
return None
if is_newer_version(version):
# value is an URL linking to the update package
return True, (version, value)
return (version, value)
# user is running a local or development version
return True, (version, None)
return (version, None)


def asset_key(asset):
def asset_key(asset: dict[str, Any]) -> int:
return EXTPRIO.get(os.path.splitext(asset['browser_download_url'])[1], 99)


def get_online_version():
def get_online_version() -> tuple[str, None | str]:
"""Download update info and parse it."""
page = http.default_session.get(UPDATE_URL).json()
version = page.get('tag_name', None)
response = http.default_session.get(UPDATE_URL)
response.raise_for_status()
page = response.json()
version = page['tag_name']

url = None
try:
Expand All @@ -58,6 +57,12 @@ def get_online_version():
return version, url


def is_newer_version(version):
def version_nums(ver: str) -> tuple[int, ...]:
"""Extract all numeric "sub-parts" of a version string. Not very exact, but
works for our use case."""
return tuple(int(s) for s in re.split(r'\D+', ver + '0'))


def is_newer_version(version) -> bool:
"""Check if given version is newer than current version."""
return LooseVersion(version) > LooseVersion(dosagelib.__version__)
return version_nums(version) > version_nums(dosagelib.__version__)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ dev = [
"pytest-cov",
"pytest-xdist",
"responses",
"setup-cfg-fmt",
]
lint = [
"flake8<6",
"flake8~=6.0",
"flake8-2020",
"flake8-breakpoint",
"flake8-bugbear",
"flake8-coding",
"flake8-commas",
"flake8-comprehensions",
"flake8-deprecated",
"flake8-eradicate",
"flake8-fixme",
"flake8-functions",
Expand Down
10 changes: 9 additions & 1 deletion tests/test_dosage.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ def test_update_broken(self, capfd):
json={})
cmd_ok('--version', '-v')
out = capfd.readouterr().out
assert 'invalid update file' in out
assert 'KeyError' in out

@responses.activate
def test_update_rate_limit(self, capfd):
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
status=403)
cmd_ok('--version', '-v')
out = capfd.readouterr().out
assert 'HTTPError' in out

def test_display_help(self):
for option in ("-h", "--help"):
Expand Down

0 comments on commit af20ca7

Please sign in to comment.