2013-04-08 18:17:02 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
|
|
|
"""
|
|
|
|
Function to check for updates.
|
|
|
|
"""
|
|
|
|
import os
|
2015-11-03 22:27:53 +00:00
|
|
|
import dosagelib
|
2015-11-06 22:07:19 +00:00
|
|
|
from dosagelib import configuration
|
2013-04-08 18:17:02 +00:00
|
|
|
from .util import urlopen
|
|
|
|
from distutils.version import StrictVersion
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2015-11-06 22:07:19 +00:00
|
|
|
UPDATE_URL = "https://api.github.com/repos/webcomics/dosage/releases/latest"
|
2013-04-08 18:17:02 +00:00
|
|
|
|
|
|
|
def check_update ():
|
|
|
|
"""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)
|
|
|
|
|
|
|
|
|
|
|
|
def get_online_version ():
|
|
|
|
"""Download update info and parse it."""
|
|
|
|
session = requests.session()
|
2015-11-06 22:07:19 +00:00
|
|
|
page = urlopen(UPDATE_URL, session).json()
|
2013-04-08 18:17:02 +00:00
|
|
|
version, url = None, None
|
2015-11-06 22:07:19 +00:00
|
|
|
version = page['tag_name']
|
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
url = next((x['browser_download_url'] for x in page['assets'] if x['content_type'] == 'application/x-msdos-program'), configuration.Url)
|
|
|
|
else:
|
|
|
|
url = page['tarball_url']
|
2013-04-08 18:17:02 +00:00
|
|
|
return version, url
|
|
|
|
|
|
|
|
|
|
|
|
def is_newer_version (version):
|
|
|
|
"""Check if given version is newer than current version."""
|
2015-11-03 22:27:53 +00:00
|
|
|
return StrictVersion(version) > StrictVersion(dosagelib.__version__)
|