2020-04-18 11:45:44 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
2017-05-14 22:54:02 +00:00
|
|
|
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
|
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2020-01-26 16:42:35 +00:00
|
|
|
# Copyright (C) 2015-2020 Tobias Gruetzmacher
|
2013-04-08 18:17:02 +00:00
|
|
|
import os
|
2019-12-03 19:27:37 +00:00
|
|
|
|
2019-12-05 21:23:48 +00:00
|
|
|
from distutils.version import LooseVersion
|
2019-12-03 19:27:37 +00:00
|
|
|
|
2015-11-03 22:27:53 +00:00
|
|
|
import dosagelib
|
2019-12-03 19:27:37 +00:00
|
|
|
from . import http
|
|
|
|
|
2013-04-08 18:17:02 +00:00
|
|
|
|
2020-01-26 16:42:35 +00:00
|
|
|
UPDATE_URL = 'https://api.github.com/repos/webcomics/dosage/releases/latest'
|
|
|
|
EXTPRIO = {
|
|
|
|
'.exe': 1 if os.name == 'nt' else 9,
|
|
|
|
'.whl': 2,
|
|
|
|
'.gz': 3,
|
|
|
|
}
|
2013-04-08 18:17:02 +00:00
|
|
|
|
2017-05-14 22:54:02 +00:00
|
|
|
|
|
|
|
def check_update():
|
2013-04-08 18:17:02 +00:00
|
|
|
"""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
|
|
|
|
"""
|
|
|
|
version, value = get_online_version()
|
|
|
|
if version is None:
|
|
|
|
# value is an error message
|
|
|
|
return False, value
|
2015-11-03 22:27:53 +00:00
|
|
|
if version == dosagelib.__version__:
|
2013-04-08 18:17:02 +00:00
|
|
|
# user has newest version
|
|
|
|
return True, None
|
|
|
|
if is_newer_version(version):
|
|
|
|
# value is an URL linking to the update package
|
|
|
|
return True, (version, value)
|
|
|
|
# user is running a local or development version
|
|
|
|
return True, (version, None)
|
|
|
|
|
|
|
|
|
2020-01-26 16:42:35 +00:00
|
|
|
def asset_key(asset):
|
|
|
|
return EXTPRIO.get(os.path.splitext(asset['browser_download_url'])[1], 99)
|
|
|
|
|
|
|
|
|
2017-05-14 22:54:02 +00:00
|
|
|
def get_online_version():
|
2013-04-08 18:17:02 +00:00
|
|
|
"""Download update info and parse it."""
|
2019-12-03 19:27:37 +00:00
|
|
|
page = http.default_session.get(UPDATE_URL).json()
|
2019-12-05 21:23:48 +00:00
|
|
|
version = page.get('tag_name', None)
|
2015-11-06 22:07:19 +00:00
|
|
|
|
2020-01-26 16:42:35 +00:00
|
|
|
url = None
|
|
|
|
try:
|
|
|
|
assets = sorted(page['assets'], key=asset_key)
|
|
|
|
if len(assets) > 0:
|
|
|
|
url = assets[0]['browser_download_url']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2013-04-08 18:17:02 +00:00
|
|
|
return version, url
|
|
|
|
|
|
|
|
|
2017-05-14 22:54:02 +00:00
|
|
|
def is_newer_version(version):
|
2013-04-08 18:17:02 +00:00
|
|
|
"""Check if given version is newer than current version."""
|
2019-12-05 21:23:48 +00:00
|
|
|
return LooseVersion(version) > LooseVersion(dosagelib.__version__)
|