From 66b89f4fed19c05e0de20f69fa0e437cfa71ce0d Mon Sep 17 00:00:00 2001 From: Tobias Gruetzmacher Date: Sun, 26 Jan 2020 17:42:35 +0100 Subject: [PATCH] Fix update check It should return assets for all platforms, since the repo tarball is unsuitable for setuptools_scm. Preferred asset is the universal wheel. --- CHANGELOG.md | 3 +++ dosagelib/updater.py | 30 ++++++++++++++++++------------ tests/test_dosage.py | 18 ++++++++++++++++-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94ec39d41..0c0c55542 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on ## [Unreleased] +### Fixed +- Update check now always shows assets instead of the repo tarball. + ## [2.16] - 2020-01-12 diff --git a/dosagelib/updater.py b/dosagelib/updater.py index f86360d21..657b6ce52 100644 --- a/dosagelib/updater.py +++ b/dosagelib/updater.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs # Copyright (C) 2012-2014 Bastian Kleineidam -# Copyright (C) 2015-2019 Tobias Gruetzmacher +# Copyright (C) 2015-2020 Tobias Gruetzmacher from __future__ import absolute_import, division, print_function @@ -10,11 +10,15 @@ import os from distutils.version import LooseVersion import dosagelib -from dosagelib import configuration from . import http -UPDATE_URL = "https://api.github.com/repos/webcomics/dosage/releases/latest" +UPDATE_URL = 'https://api.github.com/repos/webcomics/dosage/releases/latest' +EXTPRIO = { + '.exe': 1 if os.name == 'nt' else 9, + '.whl': 2, + '.gz': 3, +} def check_update(): @@ -38,20 +42,22 @@ def check_update(): return True, (version, None) +def asset_key(asset): + return EXTPRIO.get(os.path.splitext(asset['browser_download_url'])[1], 99) + + def get_online_version(): """Download update info and parse it.""" page = http.default_session.get(UPDATE_URL).json() version = page.get('tag_name', None) - if os.name == 'nt': - try: - url = next((x['browser_download_url'] for x in page['assets'] if - x['content_type'] == 'application/x-msdos-program'), - configuration.Url) - except KeyError: - url = None - else: - url = page.get('tarball_url', None) + url = None + try: + assets = sorted(page['assets'], key=asset_key) + if len(assets) > 0: + url = assets[0]['browser_download_url'] + except KeyError: + pass return version, url diff --git a/tests/test_dosage.py b/tests/test_dosage.py index 2bd840c40..44ab11677 100644 --- a/tests/test_dosage.py +++ b/tests/test_dosage.py @@ -6,6 +6,7 @@ from __future__ import absolute_import, division, print_function import json +import os import re import pytest @@ -46,19 +47,32 @@ class TestDosage(object): @responses.activate def test_update_available(self, capsys): responses.add(responses.GET, re.compile(r'https://api\.github\.com/'), - json={'tag_name': '9999.0', 'tarball_url': 'NOWHERE'}) + json={'tag_name': '9999.0', 'assets': [ + {'browser_download_url': 'TEST.whl'}, + {'browser_download_url': 'TEST.exe'}, + ]}) cmd_ok('--version', '-v') captured = capsys.readouterr() + best = 'TEST.exe' if os.name == 'nt' else 'TEST.whl' + assert best in captured.out assert 'A new version' in captured.out @responses.activate def test_no_update_available(self, capsys): responses.add(responses.GET, re.compile(r'https://api\.github\.com/'), - json={'tag_name': '1.0', 'tarball_url': 'NOWHERE'}) + json={'tag_name': '1.0'}) cmd_ok('--version', '-v') captured = capsys.readouterr() assert 'Detected local or development' in captured.out + @responses.activate + def test_current(self, capsys): + responses.add(responses.GET, re.compile(r'https://api\.github\.com/'), + json={'tag_name': dosagelib.__version__}) + cmd_ok('--version', '-v') + captured = capsys.readouterr() + assert captured.out.endswith('issues\n') + @responses.activate def test_update_broken(self, capsys): responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),