Skip to content

Instantly share code, notes, and snippets.

@faph
Created September 30, 2015 13:57
Show Gist options
  • Save faph/d9f7e079749b22dfc8b2 to your computer and use it in GitHub Desktop.
Save faph/d9f7e079749b22dfc8b2 to your computer and use it in GitHub Desktop.
Check for application update based on latest GitHub release tag
from distutils.version import LooseVersion
import json
import threading
import request
__version__ = '0.0.0'
Update = namedtuple('Update', ['version', 'url'])
class UpdateChecker(threading.Thread):
"""
Checks for application updates based on GitHub repo published releases
"""
API_URL = 'https://api.github.com/repos/{user}/{repo}/releases/latest'
TAG_PREFIX = 'v'
def __init__(self):
threading.Thread.__init__(self)
self.update = None
def run(self):
try:
with request.urlopen(self.API_URL, timeout=5) as f:
data = json.loads(f.read().decode('utf-8'))
repo_version = LooseVersion(data['tag_name'].lstrip(self.TAG_PREFIX))
url = data['html_url']
except BaseException:
return
current_version = LooseVersion(__version__)
if repo_version > current_version:
self.update = Update(version=repo_version, url=url)
def join(self):
threading.Thread.join(self)
return self.update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment