diff --git a/src/CNIRevelator.py b/src/CNIRevelator.py index 7b2f719..f7356a4 100644 --- a/src/CNIRevelator.py +++ b/src/CNIRevelator.py @@ -33,10 +33,7 @@ import launcher # launcher.py import ihm # ihm.py import logger # logger.py import updater # updater.py - -## Global Handlers -logfile = logger.logCur -launcherWindow = ihm.launcherWindowCur +import globs # globs.py ## MAIN FUNCTION OF CNIREVELATOR def main(): @@ -66,11 +63,23 @@ try: launcherThread = threading.Thread(target=updater.umain, daemon=False) launcher.lmain(launcherThread) except Exception: - logfile.printerr("A FATAL ERROR OCCURED : " + str(traceback.format_exc())) sys.exit(1) +updater.UPDATE_IS_MADE = True + if updater.UPDATE_IS_MADE: + # Launch app ! + args = updater.UPATH + '\\CNIRevelator.exe ' + globs.CNIRFolder + cd = updater.UPATH + for i in range(0,3): + try: + updater.spawnProcess(args, cd) + except: + time.sleep(3) + continue + break sys.exit(0) main() + sys.exit(0) diff --git a/src/downloader.py b/src/downloader.py index e25040b..4145058 100644 --- a/src/downloader.py +++ b/src/downloader.py @@ -81,7 +81,7 @@ class newcredentials: try: sessionAnswer = session.get('https://www.google.com') except Exception as e: - logfile.printerr('Network Error : ' + str(e)) + logfile.printdbg('Network Error : ' + str(e)) sessionAnswer = '' logfile.printdbg("Session Answer : " + str(sessionAnswer)) @@ -183,9 +183,7 @@ class newdownload: reducedFilename = filename.split("\\")[-1] - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text=('Downloading {}'.format(reducedFilename))) - launcherWindow.progressBar.stop() - launcherWindow.progressBar.configure(mode='determinate', value=0, maximum=100) + launcherWindow.printmsg('Downloading {}'.format(reducedFilename)) try: os.remove(filename) @@ -199,8 +197,9 @@ class newdownload: self.count = os.path.getsize(self.destinationFile) Percent = int(self.count / self.filesize * 100) - launcherWindow.progressBar.configure(mode='determinate', value=(int(Percent))) - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text=('Downloading {}'.format(reducedFilename) + ' : ' + str((Percent)) + ' %')) + launcherWindow.progressBar.stop() + launcherWindow.progressBar.configure(mode='determinate', value=(int(Percent)), maximum=100) + launcherWindow.printmsg('Downloading {}'.format(reducedFilename) + ' : ' + str((Percent)) + ' %') launcherWindow.progressBar.configure(mode='indeterminate', value=0, maximum=20) launcherWindow.progressBar.start() diff --git a/src/ihm.py b/src/ihm.py index 76fdf06..850794a 100644 --- a/src/ihm.py +++ b/src/ihm.py @@ -110,7 +110,10 @@ class LauncherWindow(Tk): self.protocol('WM_DELETE_WINDOW', lambda : self.destroy()) self.update() - + + def printmsg(self, msg): + self.mainCanvas.itemconfigure(self.msg, text=(msg)) + class AutoScrollbar(ttk.Scrollbar): def set(self, lo, hi): @@ -125,7 +128,7 @@ class AutoScrollbar(ttk.Scrollbar): def place(self, **kw): raise TclError('Cannot use place with the widget ' + self.__class__.__name__) - + class OpenPageDialog(Toplevel): def __init__(self, parent, number): @@ -157,7 +160,7 @@ class OpenPageDialog(Toplevel): def valid(self): self.parent.page = self.combotry.current() self.destroy() - + class OpenScanWin(Toplevel): diff --git a/src/launcher.py b/src/launcher.py index 02cdcb7..77e1574 100644 --- a/src/launcher.py +++ b/src/launcher.py @@ -33,19 +33,18 @@ import ihm # ihm.py import globs # globs.py import logger # logger.py -## Global Handlers -logfile = logger.logCur -launcherWindow = ihm.launcherWindowCur - ## Main function def lmain(mainThread): + logfile = logger.logCur + launcherWindow = ihm.launcherWindowCur + # Hello world logfile.printdbg('*** CNIRLauncher LOGFILE. Hello World ! ***') #logfile.printdbg('Files in directory : ' + str(os.listdir(globs.CNIRFolder))) # Hello user launcherWindow.progressBar.configure(mode='indeterminate', value=0, maximum=20) - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text='Starting...') + launcherWindow.printmsg('Starting...') launcherWindow.progressBar.start() # Starting the main update thread @@ -54,4 +53,5 @@ def lmain(mainThread): launcherWindow.mainloop() logfile.printdbg('*** CNIRLauncher LOGFILE. Goodbye World ! ***') + logfile.close() return diff --git a/src/logger.py b/src/logger.py index 5acd8f8..65d6c2d 100644 --- a/src/logger.py +++ b/src/logger.py @@ -61,5 +61,8 @@ class NewLoggingSystem: self.printerr = logger.error self.printdbg = logger.info + def close(self): + logging.shutdown() + ## Global Handler logCur = NewLoggingSystem() diff --git a/src/updater.py b/src/updater.py index 7b92bc3..90372b1 100644 --- a/src/updater.py +++ b/src/updater.py @@ -24,7 +24,6 @@ """ from win32com.client import Dispatch -import subprocess import traceback import sys import time @@ -32,6 +31,7 @@ import os import shutil import zipfile import hashlib +import subprocess import logger # logger.py import globs # globs.py @@ -39,6 +39,7 @@ import ihm # ihm.py import downloader # downloader.py UPDATE_IS_MADE = False +UPATH = ' ' def createShortcut(path, target='', wDir='', icon=''): ext = path[-3:] @@ -58,6 +59,9 @@ def createShortcut(path, target='', wDir='', icon=''): shortcut.IconLocation = icon shortcut.save() +def spawnProcess(args, cd): + subprocess.Popen(args, close_fds=True, cwd=cd, creationflags=subprocess.DETACHED_PROCESS) + ## Main Batch Function def batch(): @@ -133,7 +137,7 @@ def batch(): getTheUpdate = downloader.newdownload(credentials, finalurl, globs.CNIRFolder + '\\..\\CNIPackage.zip').download() - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text=('Verifying download...')) + launcherWindow.printmsg('Verifying download...') BUF_SIZE = 65536 # lets read stuff in 64kb chunks! @@ -153,18 +157,23 @@ def batch(): logfile.printerr("Checksum error") return False - # And now unzip + # And now prepare install + global UPATH UPATH = globs.CNIRFolder + '\\..\\CNIRevelator' + "{}.{}.{}".format(finalver[0], finalver[1], finalver[2]) logfile.printdbg("Make place") - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text=('Preparing installation...')) + launcherWindow.printmsg('Preparing installation...') + # Cleanup try: shutil.rmtree(UPATH + 'temp') + except Exception as e: + logfile.printdbg('Unable to cleanup : ' +str(e)) + try: shutil.rmtree(UPATH) - except: - pass - + except Exception as e: + logfile.printdbg('Unable to cleanup : ' +str(e)) + # Unzip logfile.printdbg("Unzipping the package") - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text=('Installing the updates')) + launcherWindow.printmsg('Installing the updates') zip_ref = zipfile.ZipFile(globs.CNIRFolder + '\\..\\CNIPackage.zip', 'r') zip_ref.extractall(UPATH + "temp") zip_ref.close() @@ -175,62 +184,62 @@ def batch(): logfile.printdbg('Extracted :' + UPATH + '\\CNIRevelator.exe') - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text=('Success !')) + launcherWindow.printmsg('Success !') # Cleanup try: os.remove(globs.CNIRFolder + '\\..\\CNIPackage.zip') except: pass - # Launch app ! - args = [UPATH + '\\CNIRevelator.exe', globs.CNIRFolder] - subprocess.Popen(args) - - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text=('Launched the new process.')) - + # Time to quit + launcherWindow.printmsg('Launched the new process.') + global UPDATE_IS_MADE UPDATE_IS_MADE = True return True ## Main Function def umain(): + # Global Handlers + logfile = logger.logCur + launcherWindow = ihm.launcherWindowCur + if len(sys.argv) > 1: + launcherWindow.printmsg('Deleting old version !') logfile.printdbg("Old install detected : {}".format(sys.argv[1])) while os.path.exists(str(sys.argv[1])): try: - shutil.rmtree(str(sys.argv[1]), ignore_errors=True) + shutil.rmtree(str(sys.argv[1])) except: pass logfile.printdbg("Fail to delete old install !") try: - # Global Handlers - logfile = logger.logCur - launcherWindow = ihm.launcherWindowCur - try: # EXECUTING THE UPDATE BATCH success = batch() except Exception as e: logfile.printerr("An error occured on the thread : " + str(traceback.format_exc())) - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text=('ERROR : ' + str(e))) + launcherWindow.printmsg('ERROR : ' + str(e)) time.sleep(3) launcherWindow.destroy() return 1 if success: logfile.printdbg("Software is up-to-date !") - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text='Software is up-to-date !') + launcherWindow.printmsg('Software is up-to-date !') else: logfile.printerr("An error occured. No effective update !") - launcherWindow.mainCanvas.itemconfigure(launcherWindow.msg, text='An error occured. No effective update !') + launcherWindow.printmsg('An error occured. No effective update !') time.sleep(2) + launcherWindow = ihm.launcherWindowCur launcherWindow.destroy() return 0 except: logfile.printerr("A FATAL ERROR OCCURED : " + str(traceback.format_exc())) + launcherWindow = ihm.launcherWindowCur launcherWindow.destroy() sys.exit(2) return 2