Created
September 30, 2015 13:57
-
-
Save faph/d9f7e079749b22dfc8b2 to your computer and use it in GitHub Desktop.
Check for application update based on latest GitHub release tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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