Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added automatic update check during startup
- Loading branch information
1 parent
b93f73e
commit 28d9558
Showing
4 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from distutils.version import LooseVersion, StrictVersion | ||
import json | ||
import os | ||
import struct | ||
import sys | ||
import traceback | ||
import urllib.request | ||
|
||
from PyQt5 import QtCore | ||
|
||
|
||
class UpdateWorker(QtCore.QObject): | ||
finished = QtCore.pyqtSignal() | ||
data_ready = QtCore.pyqtSignal(dict) | ||
|
||
@QtCore.pyqtSlot(str, str) | ||
def processUpdate(self, version, ghrepo): | ||
mdict = check_release(ghrepo, version) | ||
if mdict["update available"]: | ||
self.data_ready.emit(mdict) | ||
self.finished.emit() | ||
|
||
|
||
def check_for_update(version, ghrepo): | ||
thread = QtCore.QThread() | ||
obj = UpdateWorker() | ||
obj.moveToThread(thread) | ||
obj.finished.connect(thread.quit) | ||
thread.start() | ||
|
||
QtCore.QMetaObject.invokeMethod(obj, 'processUpdate', | ||
QtCore.Qt.QueuedConnection, | ||
QtCore.Q_ARG(str, version), | ||
QtCore.Q_ARG(str, ghrepo), | ||
) | ||
|
||
|
||
def check_release(ghrepo="user/repo", version=None, timeout=20): | ||
"""Check GitHub repository for latest release""" | ||
url = "https://api.github.com/repos/{}/releases/latest".format(ghrepo) | ||
if "GITHUB_TOKEN" in os.environ: | ||
hdr = {'authorization': os.environ["GITHUB_TOKEN"]} | ||
else: | ||
hdr = {} | ||
web = "https://github.com/{}/releases".format(ghrepo) | ||
errors = None # error messages (str) | ||
update = False # whether an update is available | ||
binary = None # download link to binary file | ||
new_version = None # string identifying new version | ||
try: | ||
req = urllib.request.Request(url, headers=hdr) | ||
data = urllib.request.urlopen(req, timeout=timeout).read() | ||
except BaseException: | ||
errors = traceback.format_exc() | ||
else: | ||
j = json.loads(data) | ||
|
||
newversion = j["tag_name"] | ||
|
||
if version is not None: | ||
try: | ||
new = StrictVersion(newversion) | ||
old = StrictVersion(version) | ||
except ValueError: | ||
new = LooseVersion(newversion) | ||
old = LooseVersion(version) | ||
if new > old: | ||
update = True | ||
new_version = newversion | ||
if hasattr(sys, "frozen"): | ||
# determine which binary URL we need | ||
if sys.platform == "win32": | ||
nbit = 8 * struct.calcsize("P") | ||
if nbit == 32: | ||
dlid = "win_32bit_setup.exe" | ||
else: | ||
dlid = "win_64bit_setup.exe" | ||
elif sys.platform == "darwin": | ||
dlid = ".pkg" | ||
else: | ||
dlid = False | ||
# search for binary download file | ||
if dlid: | ||
for a in j["assets"]: | ||
if a["browser_download_url"].count(dlid): | ||
binary = a["browser_download_url"] | ||
break | ||
mdict = {"releases url": web, | ||
"binary url": binary, | ||
"version": new_version, | ||
"update available": update, | ||
"errors": errors, | ||
} | ||
return mdict |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import os | ||
import socket | ||
|
||
import pytest | ||
from pyjibe.head import update | ||
|
||
|
||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
try: | ||
s.connect(("www.python.org", 80)) | ||
NET_AVAILABLE = True | ||
except socket.gaierror: | ||
# no internet | ||
NET_AVAILABLE = False | ||
|
||
|
||
@pytest.mark.xfail(os.getenv("APPVEYOR") in ["true", "True"], | ||
reason="does not always run on Appveyor") | ||
@pytest.mark.xfail(os.getenv("TRAVIS") == "true", | ||
reason="does not always run on travisCI") | ||
@pytest.mark.skipif(not NET_AVAILABLE, reason="No network connection!") | ||
def test_update_basic(): | ||
mdict = update.check_release(ghrepo="AFM-analysis/PyJibe", | ||
version="0.11.0") | ||
assert mdict["errors"] is None | ||
assert mdict["update available"] | ||
mdict = update.check_release(ghrepo="AFM-analysis/PyJibe", | ||
version="8472.0.0") | ||
assert mdict["errors"] is None | ||
assert not mdict["update available"] |