Window instead of terminal

This commit is contained in:
Alexis Delhaie
2020-12-15 16:48:40 +01:00
parent c5c2ca221e
commit 749c14c488
6 changed files with 122 additions and 105 deletions

2
.idea/misc.xml generated
View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (pythonProject)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (pythonProject)" project-jdk-type="Python SDK" />
</project> </project>

View File

@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.9 (pythonProject)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@@ -1,16 +1,3 @@
@echo off @echo off
call clean.bat
pip install -r requirements.txt pip install -r requirements.txt
IF EXIST "build" ( python -m PyInstaller --clean --onefile --uac-admin --noconsole --icon=app.ico -n=chronos-installer main.py
RMDIR /S /Q build
)
IF EXIST "dist" (
RMDIR /S /Q dist
)
IF EXIST "__pycache__" (
RMDIR /S /Q __pycache__
)
IF EXIST "main.spec" (
ERASE main.spec
)
pyinstaller --onefile --uac-admin --icon=app.ico main.py

View File

@@ -1,13 +0,0 @@
@echo off
IF EXIST "build" (
RMDIR /S /Q build
)
IF EXIST "dist" (
RMDIR /S /Q dist
)
IF EXIST "__pycache__" (
RMDIR /S /Q __pycache__
)
IF EXIST "main.spec" (
ERASE main.spec
)

193
main.py
View File

@@ -1,11 +1,12 @@
import requests
import os
import subprocess
from rich.progress import Progress
from rich import print
import ctypes import ctypes
import sys import os
import shutil import shutil
import subprocess
import sys
import requests
import wx
from wx import Size, Point
REPO_URL = "https://alexisdelhaie.ovh/dlcenter/chronos-repo/installer/" REPO_URL = "https://alexisdelhaie.ovh/dlcenter/chronos-repo/installer/"
VERSION_FILE = "version" VERSION_FILE = "version"
@@ -20,6 +21,34 @@ EXECUTABLE_PATH = "{}\\{}".format(INSTALL_FOLDER, EXECUTABLE)
FILES = ["libeay32.dll", "ssleay32.dll", "7z.dll", EXECUTABLE] FILES = ["libeay32.dll", "ssleay32.dll", "7z.dll", EXECUTABLE]
def create_install_folder():
company_directory = "{}\\{}".format(os.getenv('APPDATA'), COMPANY_NAME)
if not os.path.exists(company_directory):
os.mkdir(company_directory)
if not os.path.exists(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():
url = "{}{}".format(REPO_URL, VERSION_FILE)
r = requests.get(url, allow_redirects=True)
return r.text
def get_installed_version(): def get_installed_version():
if os.path.exists(INSTALLED_VERSION_PATH): if os.path.exists(INSTALLED_VERSION_PATH):
file = open(INSTALLED_VERSION_PATH, "r") file = open(INSTALLED_VERSION_PATH, "r")
@@ -36,85 +65,99 @@ def is_admin():
return False return False
def get_version_from_repo(): class MyApplication(wx.App):
url = "{}{}".format(REPO_URL, VERSION_FILE)
r = requests.get(url, allow_redirects=True)
return r.text
def OnInit(self):
def download_file_from_repo(filename): self.SetAppName("EndPoint installer")
url = "{}{}".format(REPO_URL, filename) if is_admin():
with open("{}\\{}".format(INSTALL_FOLDER, filename), 'wb') as f: self.main()
response = requests.get(url, stream=True)
total_length = response.headers.get('content-length')
if total_length is None: # no content length header
f.write(response.content)
else: else:
with Progress() as progress: dlg = wx.MessageDialog(None, "This application requires administrative privileges to run",
task = progress.add_task("Downloading {}".format(filename), total=int(total_length)) "EndPoint installer", style=wx.OK | wx.ICON_HAND)
for data in response.iter_content(chunk_size=4096): dlg.ShowModal()
f.write(data) dlg.Destroy()
progress.update(task, advance=len(data)) return True
def main(self):
version = get_version_from_repo()
create_install_folder()
self.clean()
if get_installed_version() != version:
dlg = MyDialog(None, -1, "EndPoint installer", Size(400, 120))
dlg.Show()
self.clean_folder(INSTALL_FOLDER)
for file in FILES:
download_file_from_repo(file, dlg)
if os.path.exists(INSTALLED_VERSION_PATH):
os.remove(INSTALLED_VERSION_PATH)
file = open(INSTALLED_VERSION_PATH, "w")
file.write(version)
file.close()
dlg.Destroy()
if os.path.exists(EXECUTABLE_PATH):
subprocess.Popen([EXECUTABLE_PATH], cwd=INSTALL_FOLDER)
else:
dlg = wx.MessageDialog(None, "Installer executable not found",
"EndPoint installer", style=wx.OK | wx.ICON_HAND)
dlg.ShowModal()
dlg.Destroy()
def clean_folder(self, path):
if os.path.exists(path):
for filename in os.listdir(path):
if filename != CACHE_FOLDER:
file_path = os.path.join(path, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
dlg = wx.MessageDialog(self, 'Failed to delete {}. Reason: {}'.format(file_path, e),
"EndPoint installer", style=wx.OK | wx.ICON_HAND)
dlg.ShowModal()
dlg.Destroy()
def clean(self):
if '--clean' in sys.argv:
self.clean_folder(INSTALL_FOLDER)
self.clean_folder(CACHE_FOLDER_PATH)
if '--clean-cache' in sys.argv:
self.clean_folder(CACHE_FOLDER_PATH)
if '--clean-installer' in sys.argv:
self.clean_folder(INSTALL_FOLDER)
def create_install_folder(): class MyDialog(wx.Dialog):
company_directory = "{}\\{}".format(os.getenv('APPDATA'), COMPANY_NAME)
if not os.path.exists(company_directory):
os.mkdir(company_directory)
if not os.path.exists(INSTALL_FOLDER):
os.mkdir(INSTALL_FOLDER)
def __init__(self, parent, i, title, size=wx.DefaultSize):
wx.Dialog.__init__(self)
self.Create(parent, i, title, wx.DefaultPosition, size, wx.DEFAULT_DIALOG_STYLE, 'EndPoint installer')
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 clean_folder(path): def update_progress(self, value):
if os.path.exists(path): self.pg += value
for filename in os.listdir(path): percentage = (self.pg / self.total) * 100
if filename != CACHE_FOLDER: self.status.SetLabelText("Downloading {} ({}%)".format(self.filename, int(percentage)))
file_path = os.path.join(path, filename) self.progress.SetValue(self.pg)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('[bold red]ERROR[/] Failed to delete {}. Reason: {}'.format(file_path, e))
def set_total(self, filename, total):
def clean(): self.filename = filename
if '--clean' in sys.argv: self.pg = 0
clean_folder(INSTALL_FOLDER) self.total = total
clean_folder(CACHE_FOLDER_PATH) self.progress.SetValue(0)
if '--clean-cache' in sys.argv: self.progress.SetRange(total)
clean_folder(CACHE_FOLDER_PATH)
if '--clean-installer' in sys.argv:
clean_folder(INSTALL_FOLDER)
def main(): def main():
if not is_admin(): app = MyApplication()
print('[bold red]ERROR[/] This app need to be launched eleveted') app.MainLoop()
input('-- Press enter key to quit --')
sys.exit(0)
print('Chronos installer bootstrap\n\n@alexlegarnd -- https://alexisdelhaie.ovh/')
print('Github: https://github.com/alexlegarnd/chronos-installer-bootstrap\n')
version = get_version_from_repo()
create_install_folder()
clean()
if get_installed_version() != version:
print('[bold green]INFO[/] Downloading latest version of the software installer')
clean_folder(INSTALL_FOLDER)
for file in FILES:
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(version)
file.close()
if os.path.exists(EXECUTABLE_PATH):
subprocess.Popen([EXECUTABLE_PATH], cwd=INSTALL_FOLDER)
else:
print('[bold red]ERROR[/] Executable not found')
input('-- Press enter key to quit --')
main() main()

View File

@@ -1,3 +1,3 @@
requests requests
rich wxPython
pyinstaller pyinstaller