-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.py
101 lines (84 loc) · 3.91 KB
/
launcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /usr/bin/env python3
# * Copyright : Copyright (C) 2019
# * Author : ddc
# * License : GPL v3
# * Python : 3.6
# # -*- coding: utf-8 -*-
import os
import subprocess
import sys
import requests
from PyQt5 import QtCore, QtWidgets
from src.utils import constants, messages, utilities
class Launcher:
def __init__(self):
self.progressBar = utilities.ProgressBar()
self.log = None
self.configs = None
self.client_version = None
self.new_version = None
self.new_version_msg = None
def init(self):
self.progressBar.setValues(messages.checking_files, 25)
utilities.check_dirs()
self.log = utilities.setup_logging(self)
sys.excepthook = utilities.log_uncaught_exceptions
utilities.check_files(self)
self.configs = utilities.get_all_ini_file_settings(constants.SETTINGS_FILENAME)
self.progressBar.setValues(messages.checking_new_version, 75)
self._check_update_required()
self.progressBar.close()
self._call_program()
def _check_update_required(self):
if self.configs['programVersion'] is None:
self.client_version = constants.VERSION
else:
self.client_version = self.configs['programVersion']
new_version_obj = utilities.check_new_program_version(self)
if new_version_obj.new_version_available:
self.new_version = new_version_obj.new_version
self.new_version_msg = new_version_obj.new_version_msg
self._download_new_program_version(False)
def _download_new_program_version(self, show_dialog=True):
if show_dialog:
msg = f"""{messages.new_version_available}
\nYour version: v{self.client_version}\nNew version: v{self.new_version}
\n{messages.confirm_download}"""
reply = utilities.show_message_window("question", self.new_version_msg, msg)
if reply == QtWidgets.QMessageBox.No:
new_title = f"{constants.FULL_PROGRAM_NAME} ({self.new_version_msg})"
_translate = QtCore.QCoreApplication.translate
self.form.setWindowTitle(_translate("Main", new_title))
return
program_url = f"{constants.GITHUB_EXE_PROGRAM_URL}{self.new_version}/{constants.EXE_PROGRAM_NAME}"
downloaded_program_path = f"{utilities.get_current_path()}\\{constants.EXE_PROGRAM_NAME}"
r = requests.get(program_url)
if r.status_code == 200:
with open(downloaded_program_path, 'wb') as outfile:
outfile.write(r.content)
utilities.show_message_window("Info", "INFO", f"{messages.program_updated}v{self.new_version}")
else:
utilities.show_message_window("error", "ERROR", f"{messages.error_dl_new_version}")
self.log.error(f"{messages.error_dl_new_version} {r.status_code} {r}")
def _call_program(self):
code = None
cmd = [f"{os.path.abspath(os.getcwd())}\\{constants.EXE_PROGRAM_NAME}"]
try:
process = subprocess.run(cmd, shell=True, check=True, universal_newlines=True)
#output = process.stdout
code = process.returncode
except Exception as e:
emsg = None
if code is None:
code = e.returncode
emsg = f"cmd:{cmd} - code:{code} - {e}"
self.log.error(f"{messages.error_executing_program}{constants.EXE_PROGRAM_NAME}"
f" - {messages.error_check_installation} - {emsg}")
utilities.show_message_window("error", "ERROR",
f"{messages.error_executing_program}{constants.EXE_PROGRAM_NAME}\n"
f"{messages.error_check_installation}")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
launcher = Launcher()
launcher.init()
sys.exit(0)