Skip to content

Commit

Permalink
Final Work on Updates and Main Windows name change
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownpersonog committed Jul 18, 2024
1 parent 5b2f83b commit 9051181
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_item_type(self, item_path):
class StudyMaterialManager(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Study Material Manager")
self.setWindowTitle("Fedrock")
self.setWindowIcon(get_icon('logo'))
self.setGeometry(100, 100, 1200, 800)
self.notes = {}
Expand Down Expand Up @@ -178,7 +178,7 @@ def create_toolbar(self):
upload_drive_action = QAction(get_icon("cloud-upload"), "Upload to Google Drive", self)
upload_drive_action.triggered.connect(self.upload_to_drive)
toolbar.addAction(upload_drive_action)

update_action = QAction(get_icon("update"), "Check for Updates", self)
update_action.triggered.connect(lambda: self.check_for_updates_action())
toolbar.addAction(update_action)
Expand Down
77 changes: 38 additions & 39 deletions updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import requests
import shutil
from subprocess import Popen
from PyQt5.QtWidgets import QMessageBox, QApplication
from PyQt5.QtWidgets import QMessageBox, QApplication, QFileDialog

GITHUB_REPO_OWNER = 'unknownpersonog'
GITHUB_REPO_NAME = 'fedrock'
Expand All @@ -14,28 +14,25 @@ class UpdateManager:
@staticmethod
def check_for_updates(current_version):
try:
# Fetch latest release info from GitHub API
response = requests.get(GITHUB_API_URL)
if response.status_code == 200:
latest_release = response.json()
latest_version_str = latest_release['tag_name'] # Assuming tag_name follows versioning convention
latest_version_str = latest_release['tag_name']

# Parse version strings into comparable components
current_version_parts = UpdateManager.parse_version(current_version)
latest_version_parts = UpdateManager.parse_version(latest_version_str)

# Compare versions
if latest_version_parts > current_version_parts:
# Prompt user to confirm update
reply = QMessageBox.question(None, "Update Available", "An update is available. Do you want to update?",
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_replace_executable(download_url):
if UpdateManager.download_and_save_executable(download_url):
return True
else:
return False # User declined update
return False
else:
QMessageBox.information(None, "Update Check", "You are already using the latest version.")
else:
Expand All @@ -46,14 +43,12 @@ def check_for_updates(current_version):

@staticmethod
def parse_version(version_str):
# Example version format: vyear.month.day.build
version_str = version_str.lstrip('v') # Remove 'v' prefix
version_str = version_str.lstrip('v')
parts = version_str.split('.')
return tuple(map(int, parts)) # Convert parts to integers for comparison
return tuple(map(int, parts))

@staticmethod
def get_download_url_for_os(latest_release):
# Determine which OS the application is running on
if platform.system() == 'Windows':
asset_name = 'main_windows.exe'
elif platform.system() == 'Linux':
Expand All @@ -62,7 +57,6 @@ def get_download_url_for_os(latest_release):
QMessageBox.warning(None, "Update", "Unsupported operating system.")
return None

# Find the correct asset for the current OS
for asset in latest_release['assets']:
if asset['name'] == asset_name:
return asset['browser_download_url']
Expand All @@ -71,35 +65,40 @@ def get_download_url_for_os(latest_release):
return None

@staticmethod
def download_and_replace_executable(download_url):
def download_and_save_executable(download_url):
try:
response = requests.get(download_url, stream=True)
response.raise_for_status()

# Save the new executable to a temporary file
new_executable_path = './update_new'
with open(new_executable_path, 'wb') as out_file:

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)

# Replace the current executable and set executable permissions
UpdateManager.replace_executable(new_executable_path)

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

@staticmethod
def replace_executable(new_executable_path):
current_executable = sys.executable
try:
# Remove the old executable and replace it with the new one
os.remove(current_executable)
shutil.move(new_executable_path, current_executable)

# Set executable permissions for the new executable
if platform.system() != 'Windows':
os.chmod(current_executable, 0o755) # Ensure executable on Linux

QMessageBox.information(None, "Successful Update", f"Update was successful, please restart application!")
except Exception as e:
QMessageBox.critical(None, "Update", f"Failed to replace executable: {e}")
QMessageBox.critical(None, "Update", f"Failed to download update: {e}")
return False

0 comments on commit 9051181

Please sign in to comment.