-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.py
104 lines (88 loc) · 4.18 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
import os
import sys
import platform
import requests
import shutil
from subprocess import Popen
from PyQt5.QtWidgets import QMessageBox, QApplication, QFileDialog
GITHUB_REPO_OWNER = 'unknownpersonog'
GITHUB_REPO_NAME = 'fedrock'
GITHUB_API_URL = f'https://api.github.com/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/releases/latest'
class UpdateManager:
@staticmethod
def check_for_updates(current_version):
try:
response = requests.get(GITHUB_API_URL)
if response.status_code == 200:
latest_release = response.json()
latest_version_str = latest_release['tag_name']
current_version_parts = UpdateManager.parse_version(current_version)
latest_version_parts = UpdateManager.parse_version(latest_version_str)
if latest_version_parts > current_version_parts:
reply = QMessageBox.question(None, "Update Available",
"An update is available. Do you want to update?",
QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
download_url = UpdateManager.get_download_url_for_os(latest_release)
if download_url:
if UpdateManager.download_and_save_executable(download_url):
return True
else:
return False
else:
QMessageBox.information(None, "Update Check", "You are already using the latest version.")
else:
QMessageBox.warning(None, "Update Check", "Failed to check for updates.")
except Exception as e:
QMessageBox.critical(None, "Update Check", f"Error checking for updates: {e}")
return False
@staticmethod
def parse_version(version_str):
version_str = version_str.lstrip('v')
parts = version_str.split('.')
return tuple(map(int, parts))
@staticmethod
def get_download_url_for_os(latest_release):
if platform.system() == 'Windows':
asset_name = 'main_windows.exe'
elif platform.system() == 'Linux':
asset_name = 'main_linux.bin'
else:
QMessageBox.warning(None, "Update", "Unsupported operating system.")
return None
for asset in latest_release['assets']:
if asset['name'] == asset_name:
return asset['browser_download_url']
QMessageBox.warning(None, "Update", f"No executable found for {platform.system()}.")
return None
@staticmethod
def download_and_save_executable(download_url):
try:
response = requests.get(download_url, stream=True)
response.raise_for_status()
if platform.system() == 'Windows':
default_filename = 'Fedrock.exe'
file_filter = "Executable (*.exe)"
elif platform.system() == 'Linux':
default_filename = 'Fedrock'
file_filter = "Executable (*)"
else:
QMessageBox.warning(None, "Update", "Unsupported operating system.")
return False
save_path, _ = QFileDialog.getSaveFileName(
None, "Save Updated Executable", default_filename, file_filter
)
if not save_path:
QMessageBox.information(None, "Update Cancelled", "Update was cancelled.")
return False
with open(save_path, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
if platform.system() == 'Linux':
os.chmod(save_path, 0o755) # Make the file executable on Linux
QMessageBox.information(None, "Successful Update",
f"Update was successful. The new version has been saved as:\n{save_path}\n\n"
"Please restart the application using this new file.")
return True
except Exception as e:
QMessageBox.critical(None, "Update", f"Failed to download update: {e}")
return False