Commit ·
67c7b31
1
Parent(s): 9119b9e
feat: Windows 自動アップデート機能を実装
Browse files- utils/updater.py を新規追加
- fetch_latest_release_info(): GitHub Releases API でアセット URL を取得
- download_file(): 進捗コールバック付きダウンロード
- launch_windows_updater(): バッチスクリプト経由で旧EXE置換・新EXE起動
- ui/main_window.py の _UpdateDialog を拡張
- Windows 凍結アプリのみ「⬇ 自動アップデート」ボタンを表示
- _DownloadThread で非同期ダウンロード + プログレスバー表示
- ダウンロード完了後に確認ダイアログ → updater 起動 → アプリ終了
- macOS / 開発環境は従来通りブラウザで開くのみ
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- ui/main_window.py +154 -33
- utils/updater.py +106 -0
ui/main_window.py
CHANGED
|
@@ -3,7 +3,7 @@ from __future__ import annotations
|
|
| 3 |
from PyQt6.QtWidgets import (
|
| 4 |
QMainWindow, QWidget, QHBoxLayout, QVBoxLayout,
|
| 5 |
QPushButton, QLabel, QStackedWidget, QFrame, QSizePolicy,
|
| 6 |
-
QDialog
|
| 7 |
)
|
| 8 |
from PyQt6.QtCore import Qt, QSize, QThread, pyqtSignal
|
| 9 |
from PyQt6.QtGui import QFont, QDesktopServices
|
|
@@ -34,60 +34,181 @@ SIDEBAR_W = 160
|
|
| 34 |
class _UpdateDialog(QDialog):
|
| 35 |
def __init__(self, tag: str, url: str, parent=None):
|
| 36 |
super().__init__(parent)
|
| 37 |
-
self._url
|
|
|
|
| 38 |
self.setWindowTitle("アップデートのお知らせ")
|
| 39 |
-
self.setFixedWidth(
|
|
|
|
| 40 |
|
|
|
|
|
|
|
| 41 |
layout = QVBoxLayout(self)
|
| 42 |
layout.setSpacing(12)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
title = QLabel(f"🆕 新しいバージョン {tag} が利用可能です")
|
| 46 |
-
from PyQt6.QtGui import QFont
|
| 47 |
title.setFont(QFont("", 12, QFont.Weight.Bold))
|
| 48 |
layout.addWidget(title)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
"
|
| 53 |
-
" ① 「設定」画面の「バックアップ」ボタンでデータを保存する\n"
|
| 54 |
-
" ② 旧バージョンの EXE ファイルを削除する\n"
|
| 55 |
-
" ③ 新しい EXE をダウンロードして起動する\n\n"
|
| 56 |
-
"※ バックアップを取らずに更新すると、まれにデータが引き継げない場合があります。"
|
| 57 |
)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
"background:#fffbeb; border:1px solid #fcd34d; "
|
| 61 |
-
"border-radius:6px; padding:10px;
|
| 62 |
)
|
| 63 |
-
layout.addWidget(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# ボタン行
|
| 66 |
btn_row = QHBoxLayout()
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
"QPushButton {
|
| 75 |
-
"
|
| 76 |
-
" QPushButton:hover { background:#1d4ed8; }"
|
| 77 |
)
|
| 78 |
-
|
| 79 |
|
| 80 |
-
btn_row.addWidget(
|
| 81 |
btn_row.addStretch()
|
| 82 |
-
btn_row.addWidget(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
layout.addLayout(btn_row)
|
| 84 |
|
| 85 |
-
def
|
| 86 |
-
from PyQt6.QtGui import QDesktopServices
|
| 87 |
-
from PyQt6.QtCore import QUrl
|
| 88 |
QDesktopServices.openUrl(QUrl(self._url))
|
| 89 |
self.accept()
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# ── アップデートチェッカー(バックグラウンドスレッド)──────────────────────
|
| 93 |
|
|
|
|
| 3 |
from PyQt6.QtWidgets import (
|
| 4 |
QMainWindow, QWidget, QHBoxLayout, QVBoxLayout,
|
| 5 |
QPushButton, QLabel, QStackedWidget, QFrame, QSizePolicy,
|
| 6 |
+
QDialog, QProgressBar, QMessageBox
|
| 7 |
)
|
| 8 |
from PyQt6.QtCore import Qt, QSize, QThread, pyqtSignal
|
| 9 |
from PyQt6.QtGui import QFont, QDesktopServices
|
|
|
|
| 34 |
class _UpdateDialog(QDialog):
|
| 35 |
def __init__(self, tag: str, url: str, parent=None):
|
| 36 |
super().__init__(parent)
|
| 37 |
+
self._url = url
|
| 38 |
+
self._tag = tag
|
| 39 |
self.setWindowTitle("アップデートのお知らせ")
|
| 40 |
+
self.setFixedWidth(480)
|
| 41 |
+
self._build_ui()
|
| 42 |
|
| 43 |
+
def _build_ui(self):
|
| 44 |
+
import sys
|
| 45 |
layout = QVBoxLayout(self)
|
| 46 |
layout.setSpacing(12)
|
| 47 |
|
| 48 |
+
title = QLabel(f"🆕 新しいバージョン {self._tag} が利用可能です")
|
|
|
|
|
|
|
| 49 |
title.setFont(QFont("", 12, QFont.Weight.Bold))
|
| 50 |
layout.addWidget(title)
|
| 51 |
|
| 52 |
+
warn = QLabel(
|
| 53 |
+
"⚠️ アップデート前に必ずバックアップを取ってください。\n"
|
| 54 |
+
"「設定」→「バックアップを保存...」からデータをバックアップできます。"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
+
warn.setWordWrap(True)
|
| 57 |
+
warn.setStyleSheet(
|
| 58 |
"background:#fffbeb; border:1px solid #fcd34d; "
|
| 59 |
+
"border-radius:6px; padding:10px;"
|
| 60 |
)
|
| 61 |
+
layout.addWidget(warn)
|
| 62 |
+
|
| 63 |
+
# 進捗エリア(ダウンロード中のみ表示)
|
| 64 |
+
self._progress_label = QLabel("")
|
| 65 |
+
self._progress_label.setVisible(False)
|
| 66 |
+
layout.addWidget(self._progress_label)
|
| 67 |
+
|
| 68 |
+
self._progress_bar = QProgressBar()
|
| 69 |
+
self._progress_bar.setVisible(False)
|
| 70 |
+
self._progress_bar.setRange(0, 100)
|
| 71 |
+
layout.addWidget(self._progress_bar)
|
| 72 |
|
| 73 |
# ボタン行
|
| 74 |
btn_row = QHBoxLayout()
|
| 75 |
+
self._btn_later = QPushButton("後で")
|
| 76 |
+
self._btn_later.setFixedHeight(34)
|
| 77 |
+
self._btn_later.clicked.connect(self.reject)
|
| 78 |
+
|
| 79 |
+
self._btn_browser = QPushButton("ブラウザで開く")
|
| 80 |
+
self._btn_browser.setFixedHeight(34)
|
| 81 |
+
self._btn_browser.setStyleSheet(
|
| 82 |
+
"QPushButton { border:1px solid #d1d5db; border-radius:5px; padding:0 14px; }"
|
| 83 |
+
" QPushButton:hover { background:#f3f4f6; }"
|
|
|
|
| 84 |
)
|
| 85 |
+
self._btn_browser.clicked.connect(self._open_browser)
|
| 86 |
|
| 87 |
+
btn_row.addWidget(self._btn_later)
|
| 88 |
btn_row.addStretch()
|
| 89 |
+
btn_row.addWidget(self._btn_browser)
|
| 90 |
+
|
| 91 |
+
# Windows 凍結アプリのみ自動アップデートボタンを表示
|
| 92 |
+
is_frozen_windows = (
|
| 93 |
+
getattr(sys, "frozen", False) and sys.platform == "win32"
|
| 94 |
+
)
|
| 95 |
+
if is_frozen_windows:
|
| 96 |
+
self._btn_auto = QPushButton("⬇ 自動アップデート")
|
| 97 |
+
self._btn_auto.setFixedHeight(34)
|
| 98 |
+
self._btn_auto.setStyleSheet(
|
| 99 |
+
"QPushButton { background:#2563eb; color:white; border-radius:5px; "
|
| 100 |
+
"padding:0 16px; font-weight:bold; }"
|
| 101 |
+
" QPushButton:hover { background:#1d4ed8; }"
|
| 102 |
+
)
|
| 103 |
+
self._btn_auto.clicked.connect(self._start_auto_update)
|
| 104 |
+
btn_row.addWidget(self._btn_auto)
|
| 105 |
+
|
| 106 |
layout.addLayout(btn_row)
|
| 107 |
|
| 108 |
+
def _open_browser(self):
|
|
|
|
|
|
|
| 109 |
QDesktopServices.openUrl(QUrl(self._url))
|
| 110 |
self.accept()
|
| 111 |
|
| 112 |
+
def _start_auto_update(self):
|
| 113 |
+
"""ダウンロードスレッドを起動してプログレスバーを表示する"""
|
| 114 |
+
self._btn_auto.setEnabled(False)
|
| 115 |
+
self._btn_browser.setEnabled(False)
|
| 116 |
+
self._btn_later.setEnabled(False)
|
| 117 |
+
|
| 118 |
+
self._progress_label.setText("リリース情報を取得中...")
|
| 119 |
+
self._progress_label.setVisible(True)
|
| 120 |
+
self._progress_bar.setValue(0)
|
| 121 |
+
self._progress_bar.setVisible(True)
|
| 122 |
+
|
| 123 |
+
self._dl_thread = _DownloadThread()
|
| 124 |
+
self._dl_thread.progress.connect(self._on_progress)
|
| 125 |
+
self._dl_thread.finished_ok.connect(self._on_download_done)
|
| 126 |
+
self._dl_thread.finished_err.connect(self._on_download_error)
|
| 127 |
+
self._dl_thread.start()
|
| 128 |
+
|
| 129 |
+
def _on_progress(self, downloaded: int, total: int, label: str):
|
| 130 |
+
self._progress_label.setText(label)
|
| 131 |
+
if total > 0:
|
| 132 |
+
self._progress_bar.setValue(int(downloaded * 100 / total))
|
| 133 |
+
else:
|
| 134 |
+
self._progress_bar.setRange(0, 0) # indeterminate
|
| 135 |
+
|
| 136 |
+
def _on_download_done(self, new_exe: str):
|
| 137 |
+
self._progress_bar.setValue(100)
|
| 138 |
+
self._progress_bar.setRange(0, 100)
|
| 139 |
+
self._progress_label.setText("✅ ダウンロード完了")
|
| 140 |
+
|
| 141 |
+
reply = QMessageBox.question(
|
| 142 |
+
self, "アップデート適用",
|
| 143 |
+
"ダウンロードが完了しました。\n"
|
| 144 |
+
"アプリを終了してアップデートを適用しますか?\n\n"
|
| 145 |
+
"(アプリが自動的に再起動されます)",
|
| 146 |
+
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
| 147 |
+
)
|
| 148 |
+
if reply != QMessageBox.StandardButton.Yes:
|
| 149 |
+
self._btn_later.setEnabled(True)
|
| 150 |
+
self._btn_browser.setEnabled(True)
|
| 151 |
+
return
|
| 152 |
+
|
| 153 |
+
from utils.updater import get_current_exe, launch_windows_updater
|
| 154 |
+
old_exe = get_current_exe()
|
| 155 |
+
if not old_exe:
|
| 156 |
+
QMessageBox.critical(self, "エラー", "現在の EXE パスを取得できませんでした。")
|
| 157 |
+
return
|
| 158 |
+
|
| 159 |
+
if not launch_windows_updater(old_exe, new_exe):
|
| 160 |
+
QMessageBox.critical(self, "エラー", "アップデーターの起動に失敗しました。\n手動でアップデートしてください。")
|
| 161 |
+
return
|
| 162 |
+
|
| 163 |
+
from PyQt6.QtWidgets import QApplication
|
| 164 |
+
QApplication.instance().quit()
|
| 165 |
+
|
| 166 |
+
def _on_download_error(self, msg: str):
|
| 167 |
+
self._progress_bar.setVisible(False)
|
| 168 |
+
self._progress_label.setText(f"❌ エラー: {msg}")
|
| 169 |
+
self._btn_later.setEnabled(True)
|
| 170 |
+
self._btn_browser.setEnabled(True)
|
| 171 |
+
self._btn_auto.setEnabled(True)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
# ── ダウンロードスレッド ───────────────────────────────────────────────────
|
| 175 |
+
|
| 176 |
+
class _DownloadThread(QThread):
|
| 177 |
+
progress = pyqtSignal(int, int, str) # (downloaded, total, label)
|
| 178 |
+
finished_ok = pyqtSignal(str) # new_exe_path
|
| 179 |
+
finished_err = pyqtSignal(str) # error message
|
| 180 |
+
|
| 181 |
+
def run(self):
|
| 182 |
+
import tempfile
|
| 183 |
+
try:
|
| 184 |
+
from utils.updater import fetch_latest_release_info, download_file
|
| 185 |
+
self.progress.emit(0, 0, "リリース情報を取得中...")
|
| 186 |
+
info = fetch_latest_release_info()
|
| 187 |
+
exe_url = info.get("exe_url", "")
|
| 188 |
+
if not exe_url:
|
| 189 |
+
self.finished_err.emit("ダウンロード URL が見つかりませんでした。")
|
| 190 |
+
return
|
| 191 |
+
|
| 192 |
+
fd, tmp_path = tempfile.mkstemp(suffix=".exe", prefix="SDU-Shift_new_")
|
| 193 |
+
import os
|
| 194 |
+
os.close(fd)
|
| 195 |
+
|
| 196 |
+
def progress_cb(downloaded, total):
|
| 197 |
+
if total:
|
| 198 |
+
mb_dl = downloaded / 1_048_576
|
| 199 |
+
mb_tot = total / 1_048_576
|
| 200 |
+
label = f"ダウンロード中... {mb_dl:.1f} / {mb_tot:.1f} MB"
|
| 201 |
+
else:
|
| 202 |
+
mb_dl = downloaded / 1_048_576
|
| 203 |
+
label = f"ダウンロード中... {mb_dl:.1f} MB"
|
| 204 |
+
self.progress.emit(downloaded, total, label)
|
| 205 |
+
|
| 206 |
+
download_file(exe_url, tmp_path, progress_cb)
|
| 207 |
+
self.finished_ok.emit(tmp_path)
|
| 208 |
+
|
| 209 |
+
except Exception as e:
|
| 210 |
+
self.finished_err.emit(str(e))
|
| 211 |
+
|
| 212 |
|
| 213 |
# ── アップデートチェッカー(バックグラウンドスレッド)──────────────────────
|
| 214 |
|
utils/updater.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""自動アップデートユーティリティ"""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
import json
|
| 6 |
+
import urllib.request
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def fetch_latest_release_info() -> dict:
|
| 11 |
+
"""
|
| 12 |
+
GitHub Releases API から最新リリース情報を取得する。
|
| 13 |
+
戻り値: {"tag": str, "html_url": str, "exe_url": str, "mac_url": str}
|
| 14 |
+
失敗時は空文字列をセットして返す。
|
| 15 |
+
"""
|
| 16 |
+
api_url = "https://api.github.com/repos/Richiesss/shift-tool/releases/latest"
|
| 17 |
+
req = urllib.request.Request(
|
| 18 |
+
api_url, headers={"User-Agent": "SDU-Shift-Updater"})
|
| 19 |
+
with urllib.request.urlopen(req, timeout=10) as resp:
|
| 20 |
+
data = json.loads(resp.read())
|
| 21 |
+
|
| 22 |
+
tag = data.get("tag_name", "")
|
| 23 |
+
html_url = data.get("html_url", "")
|
| 24 |
+
exe_url = ""
|
| 25 |
+
mac_url = ""
|
| 26 |
+
for asset in data.get("assets", []):
|
| 27 |
+
name = asset.get("name", "")
|
| 28 |
+
dl = asset.get("browser_download_url", "")
|
| 29 |
+
if name == "SDU-Shift.exe":
|
| 30 |
+
exe_url = dl
|
| 31 |
+
elif name == "SDU-Shift-mac.zip":
|
| 32 |
+
mac_url = dl
|
| 33 |
+
|
| 34 |
+
return {"tag": tag, "html_url": html_url, "exe_url": exe_url, "mac_url": mac_url}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def download_file(url: str, dest: str, progress_cb=None):
|
| 38 |
+
"""
|
| 39 |
+
url をファイル dest にダウンロードする。
|
| 40 |
+
progress_cb(downloaded: int, total: int) を随時呼び出す(total=0 は不明)。
|
| 41 |
+
"""
|
| 42 |
+
req = urllib.request.Request(url, headers={"User-Agent": "SDU-Shift-Updater"})
|
| 43 |
+
with urllib.request.urlopen(req, timeout=120) as resp:
|
| 44 |
+
total = int(resp.headers.get("Content-Length", 0))
|
| 45 |
+
downloaded = 0
|
| 46 |
+
chunk_size = 65536 # 64 KB
|
| 47 |
+
with open(dest, "wb") as f:
|
| 48 |
+
while True:
|
| 49 |
+
chunk = resp.read(chunk_size)
|
| 50 |
+
if not chunk:
|
| 51 |
+
break
|
| 52 |
+
f.write(chunk)
|
| 53 |
+
downloaded += len(chunk)
|
| 54 |
+
if progress_cb:
|
| 55 |
+
progress_cb(downloaded, total)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def get_current_exe() -> str:
|
| 59 |
+
"""凍結アプリの EXE パスを返す。未凍結なら空文字列。"""
|
| 60 |
+
if getattr(sys, "frozen", False):
|
| 61 |
+
return sys.executable
|
| 62 |
+
return ""
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def launch_windows_updater(old_exe: str, new_exe: str) -> bool:
|
| 66 |
+
"""
|
| 67 |
+
バッチスクリプトを生成して現プロセス終了後に:
|
| 68 |
+
1. 旧 EXE を削除
|
| 69 |
+
2. 新 EXE を旧 EXE のパスに移動
|
| 70 |
+
3. 新 EXE を起動
|
| 71 |
+
4. バッチ自己削除
|
| 72 |
+
を行う。失敗時は False を返す。
|
| 73 |
+
"""
|
| 74 |
+
import tempfile
|
| 75 |
+
import subprocess
|
| 76 |
+
|
| 77 |
+
pid = os.getpid()
|
| 78 |
+
try:
|
| 79 |
+
fd, bat_path = tempfile.mkstemp(suffix=".bat")
|
| 80 |
+
os.close(fd)
|
| 81 |
+
with open(bat_path, "w", encoding="cp932") as f:
|
| 82 |
+
f.write(f"""@echo off
|
| 83 |
+
chcp 932 > nul
|
| 84 |
+
:wait
|
| 85 |
+
tasklist /fi "PID eq {pid}" 2>nul | find "{pid}" >nul
|
| 86 |
+
if not errorlevel 1 (
|
| 87 |
+
timeout /t 1 /nobreak > nul
|
| 88 |
+
goto wait
|
| 89 |
+
)
|
| 90 |
+
timeout /t 1 /nobreak > nul
|
| 91 |
+
del /f /q "{old_exe}"
|
| 92 |
+
move /Y "{new_exe}" "{old_exe}"
|
| 93 |
+
start "" "{old_exe}"
|
| 94 |
+
del "%~f0"
|
| 95 |
+
""")
|
| 96 |
+
subprocess.Popen(
|
| 97 |
+
["cmd.exe", "/c", bat_path],
|
| 98 |
+
creationflags=(
|
| 99 |
+
subprocess.DETACHED_PROCESS |
|
| 100 |
+
subprocess.CREATE_NEW_PROCESS_GROUP
|
| 101 |
+
),
|
| 102 |
+
close_fds=True,
|
| 103 |
+
)
|
| 104 |
+
return True
|
| 105 |
+
except Exception:
|
| 106 |
+
return False
|