-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.py
More file actions
300 lines (248 loc) · 10.2 KB
/
updater.py
File metadata and controls
300 lines (248 loc) · 10.2 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# updater.py
#
# Background update checker for SETS-WARP.
#
# Checks GitHub Releases for a newer version of SETS-WARP and offers to update
# in-place. Current version is read from the nearest git tag (git describe),
# so after `git pull` the version advances automatically — no code change needed.
# VERSION constant is a fallback only (zip installs without git).
#
# Two update paths (auto-detected):
# git install — runs `git pull` then restarts the process
# zip install — downloads the release zip, extracts it in-place, then restarts
#
# User preferences (stored in QSettings):
# warp_update/enabled — bool, default True (Autoupdate checkbox)
# warp_update/snoozed_version — str (tag snoozed by "Don't remind me")
#
# Cross-platform: Linux, macOS, Windows.
from __future__ import annotations
import logging
import os
import subprocess
import sys
import threading
from pathlib import Path
try:
from src.setsdebug import log
except Exception:
log = logging.getLogger(__name__)
# ── Configuration ──────────────────────────────────────────────────────────────
VERSION = '1.6b' # fallback for zip installs / no git
GITHUB_REPO = 'raman78/sets-warp'
API_URL = f'https://api.github.com/repos/{GITHUB_REPO}/releases/latest'
TIMEOUT = 3 # seconds for the API request
# ── Public entry point ─────────────────────────────────────────────────────────
def schedule_update_check(sets_app) -> None:
"""
Start a background update check 3 s after app launch.
Skips silently if autoupdate is disabled in Settings.
"""
if not is_autoupdate_enabled(sets_app):
log.debug('SETS-WARP updater: autoupdate disabled, skipping check')
return
threading.Thread(
target=_check_worker,
args=(sets_app,),
daemon=True,
name='warp-update-check',
).start()
def is_autoupdate_enabled(sets_app) -> bool:
"""Return True if autoupdate is enabled (default: True)."""
try:
return bool(sets_app.settings.value('warp_update/enabled', True, type=bool))
except Exception:
return True
# ── Version detection ──────────────────────────────────────────────────────────
def get_current_version() -> str:
"""
Return the current installed version from the nearest git tag.
Falls back to VERSION constant when git is unavailable.
"""
try:
result = subprocess.run(
['git', 'describe', '--tags', '--abbrev=0'],
cwd=str(_repo_root()),
capture_output=True,
text=True,
timeout=5,
)
if result.returncode == 0:
return result.stdout.strip().lstrip('v')
except Exception:
pass
return VERSION
# ── Background worker ──────────────────────────────────────────────────────────
def _check_worker(sets_app) -> None:
try:
import json
import urllib.request
current = get_current_version()
req = urllib.request.Request(
API_URL,
headers={
'User-Agent': f'SETS-WARP/{current}',
'Accept': 'application/vnd.github+json',
},
)
with urllib.request.urlopen(req, timeout=TIMEOUT) as resp:
data = json.loads(resp.read().decode('utf-8'))
remote_tag = data.get('tag_name', '').lstrip('v')
if not remote_tag:
return
if remote_tag == current:
log.info(f'SETS-WARP updater: up to date (v{current})')
return
# Check snooze
try:
snoozed = sets_app.settings.value('warp_update/snoozed_version', '')
if snoozed == remote_tag:
log.debug(f'SETS-WARP updater: v{remote_tag} snoozed by user, skipping')
return
except Exception:
pass
notes_raw = data.get('body', '') or ''
notes = '\n'.join(notes_raw.splitlines()[:10]).strip()
log.info(
f'SETS-WARP updater: new release v{remote_tag} available (current v{current})')
from PySide6.QtCore import QTimer
from PySide6.QtWidgets import QApplication
QTimer.singleShot(
0, QApplication.instance(),
lambda: _show_update_dialog(sets_app, current, remote_tag, notes))
except Exception as e:
log.warning(f'SETS-WARP updater: check failed ({e})')
# ── Dialog ─────────────────────────────────────────────────────────────────────
def _show_update_dialog(sets_app, current: str, new_tag: str, notes: str) -> None:
from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
QDialog, QDialogButtonBox, QLabel, QTextEdit, QVBoxLayout)
parent = getattr(sets_app, 'window', None)
dlg = QDialog(parent)
dlg.setWindowTitle('SETS-WARP Update')
dlg.setMinimumWidth(500)
layout = QVBoxLayout(dlg)
layout.setSpacing(10)
layout.addWidget(QLabel(
f'<b>New version available: v{new_tag}</b><br>'
f'Current version: v{current}'
))
if notes:
te = QTextEdit()
te.setReadOnly(True)
te.setPlainText(notes)
te.setFixedHeight(150)
layout.addWidget(te)
if _is_git_install():
hint = 'The app will run <b>git pull</b> and restart automatically.'
else:
hint = ('The new release will be downloaded and extracted, '
'then the app will restart.')
layout.addWidget(QLabel(hint))
bb = QDialogButtonBox(
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
bb.button(QDialogButtonBox.StandardButton.Ok).setText('Update now')
bb.button(QDialogButtonBox.StandardButton.Cancel).setText('Later')
bb.accepted.connect(dlg.accept)
bb.rejected.connect(dlg.reject)
layout.addWidget(bb)
accepted = dlg.exec() == QDialog.DialogCode.Accepted
if not accepted:
# "Later" always snoozes this version — re-prompt only on the next release.
try:
sets_app.settings.setValue('warp_update/snoozed_version', new_tag)
log.info(f'SETS-WARP updater: snoozed notifications for v{new_tag}')
except Exception:
pass
return
if _is_git_install():
_do_git_update(sets_app)
else:
_do_zip_update(sets_app, new_tag)
# ── Update methods ─────────────────────────────────────────────────────────────
def _is_git_install() -> bool:
return (_repo_root() / '.git').exists()
def _repo_root() -> Path:
return Path(__file__).resolve().parent
def _do_git_update(sets_app) -> None:
from PySide6.QtWidgets import QMessageBox
parent = getattr(sets_app, 'window', None)
try:
# Fetch tags explicitly — git pull alone does not fetch tags that were
# created after the commit was already on the remote (common release pattern).
subprocess.run(
['git', 'fetch', '--tags'],
cwd=str(_repo_root()),
capture_output=True,
text=True,
timeout=30,
)
result = subprocess.run(
['git', 'pull'],
cwd=str(_repo_root()),
capture_output=True,
text=True,
timeout=60,
)
if result.returncode != 0:
QMessageBox.warning(
parent, 'Update failed',
f'git pull returned an error:\n\n{result.stderr.strip()}')
return
except FileNotFoundError:
QMessageBox.warning(
parent, 'Update failed',
'git is not available in PATH.\n'
'Please run "git pull" manually in the SETS-WARP folder.')
return
except Exception as e:
QMessageBox.warning(parent, 'Update failed', str(e))
return
_restart()
def _do_zip_update(sets_app, new_tag: str) -> None:
import shutil
import tempfile
import urllib.request
import zipfile
from PySide6.QtWidgets import QMessageBox
parent = getattr(sets_app, 'window', None)
zip_url = (f'https://github.com/{GITHUB_REPO}'
f'/archive/refs/tags/v{new_tag}.zip')
log.info(f'SETS-WARP updater: downloading {zip_url}')
try:
with urllib.request.urlopen(zip_url, timeout=120) as resp:
tmp = tempfile.NamedTemporaryFile(suffix='.zip', delete=False)
shutil.copyfileobj(resp, tmp)
tmp.close()
root = _repo_root()
with zipfile.ZipFile(tmp.name) as zf:
names = zf.namelist()
top = names[0].rstrip('/')
prefix = top + '/'
for member in names:
if not member.startswith(prefix):
continue
rel = member[len(prefix):]
if not rel:
continue
dest = root / rel
if member.endswith('/'):
dest.mkdir(parents=True, exist_ok=True)
else:
dest.parent.mkdir(parents=True, exist_ok=True)
with zf.open(member) as src, open(dest, 'wb') as dst:
shutil.copyfileobj(src, dst)
os.unlink(tmp.name)
log.info('SETS-WARP updater: extraction complete')
except Exception as e:
QMessageBox.warning(parent, 'Update failed', str(e))
return
_restart()
def _restart() -> None:
"""Restart the process in-place. Cross-platform."""
log.info('SETS-WARP updater: restarting...')
if sys.platform == 'win32':
subprocess.Popen([sys.executable] + sys.argv)
sys.exit(0)
else:
os.execv(sys.executable, [sys.executable] + sys.argv)