Skip to content

Commit

Permalink
Enabling Automatic update
Browse files Browse the repository at this point in the history
  • Loading branch information
beniroquai committed Sep 25, 2022
1 parent ef34188 commit 854b06f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion imswitch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.2.3'
__version__ = '1.2.0'


# Copyright (C) 2020-2021 ImSwitch developers
Expand Down
39 changes: 35 additions & 4 deletions imswitch/imcommon/controller/CheckUpdatesController.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from luddite import get_version_pypi
from packaging import version
import subprocess

import urllib.request
import threading

import imswitch
from imswitch.imcommon.framework import Signal, Thread
Expand All @@ -23,6 +24,7 @@ def __init__(self, *args, **kwargs):
self.thread.sigNoUpdate.connect(self._widget.showNoUpdateAvailable)
self.thread.sigNewVersionPyInstaller.connect(self._widget.showPyInstallerUpdate)
self.thread.sigNewVersionPyPI.connect(self._widget.showPyPIUpdate)
self.thread.sigNewVersionShowInfo.connect(self._widget.showInfo)

def __del__(self):
self.thread.quit()
Expand All @@ -46,33 +48,62 @@ class CheckUpdatesThread(Thread):
sigNoUpdate = Signal()
sigNewVersionPyInstaller = Signal(str) # (latestVersion)
sigNewVersionPyPI = Signal(str) # (latestVersion)

sigNewVersionShowInfo = Signal(str) # (someText)
def __init__(self):
super().__init__()
self.__logger = initLogger(self, tryInheritParent=True)

def run(self):
currentVersion = imswitch.__version__
try:
if 'IMSWITCH_IS_BUNDLE' in os.environ and os.environ['IMSWITCH_IS_BUNDLE'] == '1':
if True: #'IMSWITCH_IS_BUNDLE' in os.environ and os.environ['IMSWITCH_IS_BUNDLE'] == '1':
# Installed from bundle - check GitHub
self.__logger.debug("We are checking for pre-built bundles on Github")
releaseResponse = requests.get(
'https://api.github.com/repos/openUC2/ImSwitch/releases/latest'
)
latestVersion = releaseResponse.json()['tag_name'].lstrip('v')
latestVersionDate = releaseResponse.json()['published_at'].split('T')[0]

if version.parse(latestVersion) > version.parse(currentVersion):
self.sigNewVersionPyInstaller.emit(latestVersion)

## attempting to downloda the current ImSwitch version
downloadURL = releaseResponse.json()['assets'][0]['browser_download_url']
self.__logger.debug("We are downloading the software from: "+downloadURL)

## inplace replacement won't work I guess?
def dlImSwitch(downloadURL, fileName):
resultDL = urllib.request.urlretrieve(downloadURL, fileName)

self.sigNewVersionShowInfo.emit("Downloading and updating latest version...")


ImSwitchExeFilename = "ImSwitch.exe"
if os.path.isfile(ImSwitchExeFilename):
self.__logger.debug("Renaming old ImSwitch.exe file")
os.rename(ImSwitchExeFilename, ImSwitchExeFilename+"_BAK")
mThread = threading.Thread(target=dlImSwitch, args=(downloadURL, ImSwitchExeFilename))
mThread.start()
mThread.join()
self.__logger.debug("Download successful!")
self.sigNewVersionShowInfo.emit("Download successful! Please restart ImSwitch. ")



else:
self.sigNoUpdate.emit()
else:
# Not installed from bundle - check PyPI
self.__logger.debug("We are checking for latest versions on pypi")

latestVersion = get_version_pypi('ImSwitch')
if version.parse(latestVersion) > version.parse(currentVersion):
self.sigNewVersionPyPI.emit(latestVersion)
else:
self.sigNoUpdate.emit()


except Exception:
self.__logger.warning(traceback.format_exc())
self.sigFailed.emit()
Expand Down
6 changes: 6 additions & 0 deletions imswitch/imcommon/view/CheckUpdatesDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def showPyInstallerUpdate(self, newVersion):
)
self.linkLabel.setVisible(True)

def showInfo(self, text):
self.informationLabel.setText(text)
self.linkLabel.setText("")
self.linkLabel.setVisible(False)


def showPyPIUpdate(self, newVersion):
self.informationLabel.setText(
f'ImSwitch {newVersion} is now available. '
Expand Down

0 comments on commit 854b06f

Please sign in to comment.