Fix Window freezing while downloading
This commit is contained in:
167
main.py
167
main.py
@@ -3,12 +3,13 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import wx
|
import wx
|
||||||
from wx import Size, Point
|
from wx import Size, Point
|
||||||
|
|
||||||
## Installer configuration
|
# Installer configuration
|
||||||
REPO_URL = "https://alexisdelhaie.ovh/dlcenter/chronos-repo/installer/"
|
REPO_URL = "https://alexisdelhaie.ovh/dlcenter/chronos-repo/installer/"
|
||||||
VERSION_FILE = "version"
|
VERSION_FILE = "version"
|
||||||
COMPANY_NAME = "alexlegarnd"
|
COMPANY_NAME = "alexlegarnd"
|
||||||
@@ -23,6 +24,11 @@ FILES = ["libeay32.dll", "ssleay32.dll", "7z.dll", EXECUTABLE]
|
|||||||
WINDOW_TITLE = "Chronos Installer"
|
WINDOW_TITLE = "Chronos Installer"
|
||||||
|
|
||||||
|
|
||||||
|
EVT_START_ID = wx.Window.NewControlId()
|
||||||
|
EVT_UPDATE_ID = wx.Window.NewControlId()
|
||||||
|
EVT_END_ID = wx.Window.NewControlId()
|
||||||
|
|
||||||
|
|
||||||
def create_install_folder():
|
def create_install_folder():
|
||||||
company_directory = "{}\\{}".format(os.getenv('APPDATA'), COMPANY_NAME)
|
company_directory = "{}\\{}".format(os.getenv('APPDATA'), COMPANY_NAME)
|
||||||
if not os.path.exists(company_directory):
|
if not os.path.exists(company_directory):
|
||||||
@@ -31,20 +37,6 @@ def create_install_folder():
|
|||||||
os.mkdir(INSTALL_FOLDER)
|
os.mkdir(INSTALL_FOLDER)
|
||||||
|
|
||||||
|
|
||||||
def download_file_from_repo(filename, dlg):
|
|
||||||
url = "{}{}".format(REPO_URL, filename)
|
|
||||||
with open("{}\\{}".format(INSTALL_FOLDER, filename), 'wb') as f:
|
|
||||||
response = requests.get(url, stream=True)
|
|
||||||
total_length = response.headers.get('content-length')
|
|
||||||
dlg.set_total(filename, int(total_length))
|
|
||||||
if total_length is None: # no content length header
|
|
||||||
f.write(response.content)
|
|
||||||
else:
|
|
||||||
for data in response.iter_content(chunk_size=4096):
|
|
||||||
f.write(data)
|
|
||||||
dlg.update_progress(len(data))
|
|
||||||
|
|
||||||
|
|
||||||
def get_version_from_repo():
|
def get_version_from_repo():
|
||||||
url = "{}{}".format(REPO_URL, VERSION_FILE)
|
url = "{}{}".format(REPO_URL, VERSION_FILE)
|
||||||
r = requests.get(url, allow_redirects=True)
|
r = requests.get(url, allow_redirects=True)
|
||||||
@@ -72,7 +64,19 @@ class MyApplication(wx.App):
|
|||||||
def OnInit(self):
|
def OnInit(self):
|
||||||
self.SetAppName(WINDOW_TITLE)
|
self.SetAppName(WINDOW_TITLE)
|
||||||
if is_admin():
|
if is_admin():
|
||||||
self.main()
|
version = get_version_from_repo()
|
||||||
|
create_install_folder()
|
||||||
|
if get_installed_version() != version:
|
||||||
|
dlg = MyDialog(None, -1, WINDOW_TITLE, version, Size(400, 120))
|
||||||
|
dlg.ShowModal()
|
||||||
|
dlg.Destroy()
|
||||||
|
if os.path.exists(EXECUTABLE_PATH):
|
||||||
|
subprocess.Popen([EXECUTABLE_PATH], cwd=INSTALL_FOLDER)
|
||||||
|
else:
|
||||||
|
dlg = wx.MessageDialog(None, "Installer executable not found",
|
||||||
|
WINDOW_TITLE, style=wx.OK | wx.ICON_HAND)
|
||||||
|
dlg.ShowModal()
|
||||||
|
dlg.Destroy()
|
||||||
else:
|
else:
|
||||||
dlg = wx.MessageDialog(None, "This application requires administrative privileges to run",
|
dlg = wx.MessageDialog(None, "This application requires administrative privileges to run",
|
||||||
WINDOW_TITLE, style=wx.OK | wx.ICON_HAND)
|
WINDOW_TITLE, style=wx.OK | wx.ICON_HAND)
|
||||||
@@ -80,29 +84,41 @@ class MyApplication(wx.App):
|
|||||||
dlg.Destroy()
|
dlg.Destroy()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def main(self):
|
|
||||||
version = get_version_from_repo()
|
class MyDialog(wx.Dialog):
|
||||||
create_install_folder()
|
|
||||||
|
def __init__(self, parent, i, title, version, size=wx.DefaultSize):
|
||||||
|
wx.Dialog.__init__(self)
|
||||||
|
self.Create(parent, i, title, wx.DefaultPosition, size, wx.DEFAULT_DIALOG_STYLE, WINDOW_TITLE)
|
||||||
|
self.Centre()
|
||||||
|
self.panel = wx.Panel(self)
|
||||||
|
self.status = wx.StaticText(self.panel, label="Starting...", pos=Point(17, 10))
|
||||||
|
self.progress = wx.Gauge(self.panel, -1, size=Size(350, 25), pos=Point(17, 40))
|
||||||
|
self.pg = 0
|
||||||
|
self.total = 100
|
||||||
|
self.filename = "no_name_file"
|
||||||
|
self.Connect(-1, -1, EVT_START_ID, self.set_total)
|
||||||
|
self.Connect(-1, -1, EVT_UPDATE_ID, self.update_progress)
|
||||||
|
self.Connect(-1, -1, EVT_END_ID, self.end)
|
||||||
self.clean()
|
self.clean()
|
||||||
if get_installed_version() != version:
|
self.clean_folder(INSTALL_FOLDER)
|
||||||
dlg = MyDialog(None, -1, WINDOW_TITLE, Size(400, 120))
|
self.handler = WorkerThread(version, self)
|
||||||
dlg.Show()
|
|
||||||
self.clean_folder(INSTALL_FOLDER)
|
def update_progress(self, event):
|
||||||
for file in FILES:
|
self.pg += event.value
|
||||||
download_file_from_repo(file, dlg)
|
percentage = (self.pg / self.total) * 100
|
||||||
if os.path.exists(INSTALLED_VERSION_PATH):
|
self.status.SetLabelText("Downloading {} ({}%)".format(self.filename, int(percentage)))
|
||||||
os.remove(INSTALLED_VERSION_PATH)
|
self.progress.SetValue(self.pg)
|
||||||
file = open(INSTALLED_VERSION_PATH, "w")
|
|
||||||
file.write(version)
|
def set_total(self, event):
|
||||||
file.close()
|
self.filename = event.filename
|
||||||
dlg.Destroy()
|
self.pg = 0
|
||||||
if os.path.exists(EXECUTABLE_PATH):
|
self.total = event.total
|
||||||
subprocess.Popen([EXECUTABLE_PATH], cwd=INSTALL_FOLDER)
|
self.progress.SetValue(0)
|
||||||
else:
|
self.progress.SetRange(event.total)
|
||||||
dlg = wx.MessageDialog(None, "Installer executable not found",
|
|
||||||
WINDOW_TITLE, style=wx.OK | wx.ICON_HAND)
|
def end(self, event):
|
||||||
dlg.ShowModal()
|
self.Close()
|
||||||
dlg.Destroy()
|
|
||||||
|
|
||||||
def clean_folder(self, path):
|
def clean_folder(self, path):
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
@@ -130,31 +146,62 @@ class MyApplication(wx.App):
|
|||||||
self.clean_folder(INSTALL_FOLDER)
|
self.clean_folder(INSTALL_FOLDER)
|
||||||
|
|
||||||
|
|
||||||
class MyDialog(wx.Dialog):
|
class PercentageInfo(wx.PyEvent):
|
||||||
|
|
||||||
def __init__(self, parent, i, title, size=wx.DefaultSize):
|
def __init__(self, filename, total):
|
||||||
wx.Dialog.__init__(self)
|
super().__init__()
|
||||||
self.Create(parent, i, title, wx.DefaultPosition, size, wx.DEFAULT_DIALOG_STYLE, WINDOW_TITLE)
|
self.SetEventType(EVT_START_ID)
|
||||||
self.Centre()
|
|
||||||
self.panel = wx.Panel(self)
|
|
||||||
self.status = wx.StaticText(self.panel, label="-------------------------------------------", pos=Point(17, 10))
|
|
||||||
self.progress = wx.Gauge(self.panel, -1, size=Size(350, 25), pos=Point(17, 40))
|
|
||||||
self.pg = 0
|
|
||||||
self.total = 100
|
|
||||||
self.filename = "no_name_file"
|
|
||||||
|
|
||||||
def update_progress(self, value):
|
|
||||||
self.pg += value
|
|
||||||
percentage = (self.pg / self.total) * 100
|
|
||||||
self.status.SetLabelText("Downloading {} ({}%)".format(self.filename, int(percentage)))
|
|
||||||
self.progress.SetValue(self.pg)
|
|
||||||
|
|
||||||
def set_total(self, filename, total):
|
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.pg = 0
|
|
||||||
self.total = total
|
self.total = total
|
||||||
self.progress.SetValue(0)
|
|
||||||
self.progress.SetRange(total)
|
|
||||||
|
class UpdatePercentage(wx.PyEvent):
|
||||||
|
|
||||||
|
def __init__(self, value):
|
||||||
|
super().__init__()
|
||||||
|
self.SetEventType(EVT_UPDATE_ID)
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
|
||||||
|
class ProcessTerminated(wx.PyEvent):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.SetEventType(EVT_END_ID)
|
||||||
|
|
||||||
|
|
||||||
|
class WorkerThread(Thread):
|
||||||
|
|
||||||
|
def __init__(self, version, dlg):
|
||||||
|
Thread.__init__(self)
|
||||||
|
self.dlg = dlg
|
||||||
|
self.version = version
|
||||||
|
self.start()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
try:
|
||||||
|
for file in FILES:
|
||||||
|
self.download_file_from_repo(file)
|
||||||
|
if os.path.exists(INSTALLED_VERSION_PATH):
|
||||||
|
os.remove(INSTALLED_VERSION_PATH)
|
||||||
|
file = open(INSTALLED_VERSION_PATH, "w")
|
||||||
|
file.write(self.version)
|
||||||
|
file.close()
|
||||||
|
finally:
|
||||||
|
wx.PostEvent(self.dlg, ProcessTerminated())
|
||||||
|
|
||||||
|
def download_file_from_repo(self, filename):
|
||||||
|
url = "{}{}".format(REPO_URL, filename)
|
||||||
|
with open("{}\\{}".format(INSTALL_FOLDER, filename), 'wb') as f:
|
||||||
|
response = requests.get(url, stream=True)
|
||||||
|
total_length = response.headers.get('content-length')
|
||||||
|
wx.PostEvent(self.dlg, PercentageInfo(filename, int(total_length)))
|
||||||
|
if total_length is None: # no content length header
|
||||||
|
f.write(response.content)
|
||||||
|
else:
|
||||||
|
for data in response.iter_content(chunk_size=4096):
|
||||||
|
f.write(data)
|
||||||
|
wx.PostEvent(self.dlg, UpdatePercentage(len(data)))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user