consolidator
/
VersionChecker.py
93 строки · 3.6 Кб
1import sys
2import yaml
3import urllib.request
4from http import client
5from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QSizePolicy, QWidget, QLabel
6from PySide6.QtCore import QRect
7from PySide6.QtGui import QFont
8
9class UpdatePromptDialod(QMainWindow):
10def __init__(self):
11super().__init__()
12self.need_update = False
13
14# window
15self.setWindowTitle("Обновление")
16sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
17sizePolicy.setHorizontalStretch(0)
18sizePolicy.setVerticalStretch(0)
19sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
20self.resize(420,100)
21self.setSizePolicy(sizePolicy)
22self.centralwidget = QWidget(self)
23
24# label
25self.label = QLabel(self.centralwidget)
26self.label.setGeometry(QRect(10, 10, 400, 30))
27font = QFont()
28font.setPointSize(12)
29self.label.setFont(font)
30self.label.setText("Доступно обновление продукта. Скачать обновление?")
31
32# ok button
33self.okButton = QPushButton(self.centralwidget)
34self.okButton.setGeometry(QRect(90, 50, 75, 24))
35self.okButton.setText("Ok")
36self.okButton.clicked.connect(self.ok_button_clicked)
37
38# cancel buttton
39self.cancelButton = QPushButton(self.centralwidget)
40self.cancelButton.setGeometry(QRect(220, 50, 75, 24))
41self.cancelButton.setText("Cancel")
42self.cancelButton.clicked.connect(self.cancel_button_clicked)
43
44self.setCentralWidget(self.centralwidget)
45
46def ok_button_clicked(self, s):
47self.need_update = True
48self.close()
49def cancel_button_clicked(self, s):
50self.need_update = False
51self.close()
52
53class VersionChecker(object):
54
55def __init__(self,metadata:dict={}):
56self.metadata = metadata
57self.current_version = metadata.get("Version","0.0.0.0")
58self.update_version = "0.0.0.0"
59self.repo_root_url = metadata.get("Repo",None)
60self.distr_url = f"{self.repo_root_url}/dummy.zip"
61@staticmethod
62def get_int_version(str_version:str)->int:
63try:
64ver = int(str_version.replace(".",""))
65return ver
66except: return -1
67
68def update_exists(self)->bool:
69try:
70if self.repo_root_url is None: raise Exception("Отсутствует url репозитория для проверки обновлений.")
71try:
72response:client.HTTPResponse = urllib.request.urlopen(f"{self.repo_root_url}/metadata.yml")
73except: return False
74if response.status!=200: return False
75data = response.read()
76update_metadata:dict = yaml.load(data, Loader=yaml.FullLoader)
77self.update_version = update_metadata.get("Version","0.0.0.0")
78product = self.metadata.get("ProductName","dummy")
79self.distr_url = f"{self.repo_root_url}/{product}-v.{self.update_version}.zip"
80update_ver_int = VersionChecker.get_int_version(self.update_version)
81cur_ver_int = VersionChecker.get_int_version(self.current_version)
82return update_ver_int > cur_ver_int
83except: raise
84
85def show_update_dialog(self)->bool:
86try:
87app = QApplication(sys.argv)
88app.setStyle("Windows")
89win = UpdatePromptDialod()
90win.show()
91res=app.exec()
92return win.need_update
93except : raise