-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
updater.py
248 lines (201 loc) · 9.28 KB
/
updater.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
""" Update notification and release parsers
Module : updater
Authors : yannick
"""
import PyQt6.QtGui
import PyQt6.QtCore
import PyQt6.QtWidgets
from PyQt6.QtWidgets import QListWidgetItem,QCheckBox,QGroupBox,QHBoxLayout,QVBoxLayout,QLabel,QDialog,QTextBrowser,QPushButton
import requests
import json
import re
import helper
from PyQt6.QtGui import QPalette, QColor
from PyQt6.QtCore import Qt
from datetime import datetime
from optionsdialog import OptionsDialogGroupBox
from profile_ui import ProfileUI
MAINREPO = "Ultrawipf/OpenFFBoard"
GUIREPO = "Ultrawipf/OpenFFBoard-Configurator"
class GithubRelease():
@staticmethod
def get_releases(repo : str, prerelease : bool = False, draft : bool = False):
"""Returns a list of releases or connection error or empty list if there was a connection error
api: https://api.github.com/repos/Ultrawipf/OpenFFBoard/releases
"""
url = f"https://api.github.com/repos/{repo}/releases"
try:
response = requests.get(url)
except requests.ConnectionError:
return []
if not response:
return []
releases = json.loads(response.content)
# Filter out prereleases and drafts if not allowed
releases = filter(lambda r : (not bool(r["prerelease"]) or prerelease) and (not bool(r["draft"]) or draft), releases)
return list(releases)
@staticmethod
def get_latest_release(repo : str):
"""Returns the latest release for repo or empty dict on error"""
url = f"https://api.github.com/repos/{repo}/releases/latest"
try:
response = requests.get(url)
except requests.ConnectionError:
return {}
if not response:
return {}
release = json.loads(response.content)
return(release)
@staticmethod
def get_time(release : dict):
"""Returns a datetime object of the creation time"""
return datetime.strptime(release['created_at'],'%Y-%m-%dT%H:%M:%SZ')
@staticmethod
def get_version(release : dict):
"""Returns None if invalid tag, otherwise (version,postfix) like ("1.2.3","dev")"""
vermatch = re.match(r"v(\d+\.\d+\.\d+)(?:-?(.+))?",release.get("tag_name",""))
if not vermatch or not release:
return None,None
version = vermatch.group(1)
postfix = vermatch.group(2)
return version,postfix
@staticmethod
def get_description(release : dict):
"""Returns the description text of a release"""
return release.get("body",None)
@staticmethod
def get_title(release : dict):
"""Returns the title of the release"""
return release.get("name",release.get("tag_name",""))
class UpdateChecker():
@staticmethod
def compare_versions(curver,comparever):
"""Returns False if current version newer or identical, True if comparever is newer"""
if not curver or not comparever:
return False
comparever_i = [int(i) for i in comparever.split(".")]
curver_i = [int(i) for i in curver.split(".")]
outdated = (
comparever_i[0] > curver_i[0] \
or comparever_i[1] > curver_i[1] and comparever_i[0] == curver_i[0] \
or comparever_i[2] > curver_i[2] and comparever_i[1] == curver_i[1] and comparever_i[0] == curver_i[0]
)
# return comparever_i[0] > curver_i[0] or comparever_i[1] > curver_i[1] or comparever_i[2] > curver_i[2]
return outdated
@staticmethod
def check_update(repo : str, version : str):
"""Returns (bool new version available, release object)"""
release = GithubRelease.get_latest_release(repo)
if not release:
return False, {}
releasever,_ = GithubRelease.get_version(release)
return UpdateChecker.compare_versions(version,releasever), release
class UpdateBrowser(PyQt6.QtWidgets.QDialog):
"""Gets updates and displays them in a list"""
def __init__(self,parentWidget,settingsmanager : ProfileUI = None):
PyQt6.QtWidgets.QDialog.__init__(self, parentWidget)
PyQt6.uic.loadUi(helper.res_path("updatebrowser.ui"), self)
self.listWidget_release.currentItemChanged.connect(self.release_changed)
self.listWidget_files.currentItemChanged.connect(self.file_changed)
self.buttonGroup_repo.buttonClicked.connect(self.repo_changed)
self.read_releases(MAINREPO)
self.settingsmanager = settingsmanager
if settingsmanager:
self.checkBox_notify.setChecked(not self.settingsmanager.get_global_setting("donotnotify_updates",False))
self.checkBox_notify.toggled.connect(self.notify_checkbox_toggled)
else:
self.checkBox_notify.setVisible(False)
def notify_checkbox_toggled(self,status):
self.settingsmanager.set_global_setting("donotnotify_updates",not status)
def fill_releases(self,releases : dict):
self.listWidget_release.clear()
for r in releases:
if r.get("name",None):
name = f"{r.get('tag_name','No tag')} ({r.get('name')})"
else:
name = f"{r.get('tag_name','No tag')}"
item = QListWidgetItem(name)
if r.get("prerelease",False):
item.setBackground(QColor('#ffb810'))
else:
item.setBackground(QColor('#70ea5d'))
item.setData(Qt.ItemDataRole.UserRole, r)
self.listWidget_release.addItem(item)
self.listWidget_release.setCurrentRow(0)
def get_selected_release(self):
r = self.listWidget_release.currentItem().data(Qt.ItemDataRole.UserRole)
return r
def repo_changed(self,button):
if button == self.radioButton_configurator:
self.read_releases(GUIREPO)
else:
self.read_releases(MAINREPO)
def read_releases(self,repo : str):
releases = GithubRelease.get_releases(repo,True)
if releases:
self.fill_releases(releases)
def fill_files(self,release : dict):
self.listWidget_files.clear()
for r in release.get("assets",[]):
name = r['name']
url = r['browser_download_url']
item = QListWidgetItem(name)
item.setData(Qt.ItemDataRole.UserRole, url)
self.listWidget_files.addItem(item)
self.listWidget_files.setCurrentRow(0)
def release_changed(self,current : QListWidgetItem,old : QListWidgetItem):
if not current:
return
release = current.data(Qt.ItemDataRole.UserRole)
self.textBrowser_releasenotes.setMarkdown(GithubRelease.get_description(release))
self.label_releaseUrl.setText(f"<a href=\"{release['html_url']}\">{release['html_url']}</a>")
self.fill_files(release)
publishtime = GithubRelease.get_time(release)
infotext = f"Date: {publishtime}"
self.label_releaseInfo.setText(infotext)
def file_changed(self,current : QListWidgetItem,old : QListWidgetItem):
if not current:
return
url = current.data(Qt.ItemDataRole.UserRole)
self.label_fileUrl.setText(f"<a href=\"{url}\">Download</a>")
class UpdateNotification(QDialog):
"""Shows a dialog with release information"""
def __init__(self,release,main,desc,curver,donotnotifysetting = None):
self.main = main
QDialog.__init__(self, main)
self.setWindowTitle("Update available")
self.vbox = QVBoxLayout()
self.setLayout(self.vbox)
self.infolabel = QLabel(desc)
self.infolabel.setOpenExternalLinks(True)
self.infolabel.setTextFormat(Qt.TextFormat.RichText)
self.vbox.addWidget(self.infolabel)
if not release:
self.vbox.addWidget(QLabel("Error displaying release"))
return
self.releaseversion = release["tag_name"]
url = release['html_url']
self.versionlabel = QLabel(f"New version: <a href=\"{url}\"> {self.releaseversion}</a>, Current version: {curver}")
self.versionlabel.setOpenExternalLinks(True)
self.versionlabel.setTextFormat(Qt.TextFormat.RichText)
self.vbox.addWidget(self.versionlabel)
self.titlelabel = QLabel(f"<a href=\"{url}\"> {GithubRelease.get_title(release)}</a>")
self.titlelabel.setOpenExternalLinks(True)
self.vbox.addWidget(self.titlelabel)
self.updatebrowserbutton = QPushButton("Open update browser",self)
self.updatebrowserbutton.clicked.connect(self.open_updater)
self.releasenotes = QTextBrowser(self)
self.releasenotes.setMarkdown(GithubRelease.get_description(release))
self.vbox.addWidget(self.releasenotes)
self.vbox.addWidget(self.updatebrowserbutton)
if donotnotifysetting:
self.donotnotify = QCheckBox("Notify about releases at startup",self)
self.donotnotify.setChecked(not self.main.profile_ui.get_global_setting(donotnotifysetting,False))
self.vbox.addWidget(self.donotnotify)
def donotnotify_save(status):
self.main.profile_ui.set_global_setting(donotnotifysetting,not status)
self.donotnotify.toggled.connect(donotnotify_save)
def open_updater(self):
"""Opens the updatebrowser"""
self.close()
UpdateBrowser(self,self.main.profile_ui).exec()