-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: check for software updates on the github server
This is still work-in-progress
- Loading branch information
1 parent
00ab2c0
commit 519d03d
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ jobs: | |
uses: "0xDylan/[email protected]" | ||
if: startsWith(github.ref, 'refs/tags/v') | ||
with: | ||
automatic_release_tag: "latest-stable" | ||
prerelease: false | ||
files: windows/Output/*.* | ||
repo_token: "${{ secrets.GITHUB_TOKEN }}" |
135 changes: 135 additions & 0 deletions
135
ardupilot_methodic_configurator/check_for_software_updates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Check for software updates and install them if available. | ||
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator | ||
SPDX-FileCopyrightText: 2024 Amilcar Lucas | ||
SPDX-License-Identifier: GPL-3.0-or-later | ||
""" | ||
|
||
import os | ||
import platform | ||
import sys | ||
from typing import Any | ||
|
||
import requests | ||
|
||
from ardupilot_methodic_configurator import __version__ as current_version | ||
|
||
# Constants | ||
GITHUB_API_URL_LATEST_RELEASE = "https://api.github.com/repos/ArduPilot/MethodicConfigurator/releases/latest" | ||
GITHUB_API_URL_LATEST_BETA_BUILD = "https://api.github.com/repos/ArduPilot/MethodicConfigurator/releases" | ||
|
||
|
||
def get_latest_stable_release_info() -> dict[str, Any]: | ||
response = requests.get(GITHUB_API_URL_LATEST_RELEASE, timeout=30) | ||
response.raise_for_status() | ||
return response.json() # type: ignore[no-any-return] | ||
|
||
|
||
def get_latest_beta_build_info() -> dict[str, Any]: | ||
response = requests.get(GITHUB_API_URL_LATEST_BETA_BUILD, timeout=30) | ||
response.raise_for_status() | ||
releases = response.json() | ||
if releases[0]["prerelease"]: # If the latest build is a prerelease, return it | ||
return releases[0] # type: ignore[no-any-return] | ||
return releases[1] # type: ignore[no-any-return] | ||
|
||
|
||
def get_git_commit_hash() -> str: | ||
git_hash_file = os.path.join(os.path.dirname(__file__), "git_hash.txt") | ||
if os.path.exists(git_hash_file): | ||
with open(git_hash_file, encoding="utf-8") as file: | ||
return file.read().strip() | ||
return "" | ||
|
||
|
||
def print_changes_between_versions(latest_stable_release: dict, latest_beta_build: dict) -> None: | ||
print("Changes between the latest stable version and the latest beta build:") | ||
print(f"Latest stable version: {latest_stable_release['tag_name']}") | ||
print(f"Latest build version: {latest_beta_build['tag_name']}") | ||
print("Changes:") | ||
print(latest_beta_build["body"]) | ||
|
||
|
||
def download_and_install_inno_setup_release(download_url: str, file_name: str) -> bool: | ||
print("Downloading and installing new version for Windows...") | ||
try: | ||
response = requests.get(download_url, timeout=30) | ||
response.raise_for_status() | ||
|
||
with open(file_name, "wb") as file: | ||
file.write(response.content) | ||
|
||
os.system(f"start {file_name}") # noqa: S605 | ||
|
||
return True | ||
|
||
except Exception as e: # pylint: disable=broad-exception-caught | ||
print(f"Installation failed: {e}") | ||
return False | ||
|
||
|
||
def download_and_install_pip_release() -> int: | ||
print("Updating via pip for Linux and MacOS...") | ||
return os.system("pip install --upgrade ardupilot_methodic_configurator") # noqa: S605, S607 | ||
|
||
|
||
def restart_program() -> None: | ||
os.execv(sys.executable, ["python", *sys.argv]) # noqa: S606 | ||
|
||
|
||
def main() -> None: | ||
print(f"Running ArduPilot Methodic Configurator version: {current_version}") | ||
latest_stable_release = get_latest_stable_release_info() | ||
latest_build = get_latest_beta_build_info() | ||
|
||
print_changes_between_versions(latest_stable_release, latest_build) | ||
|
||
latest_stable_version = latest_stable_release["tag_name"] | ||
if latest_stable_version.startswith("v"): | ||
latest_stable_version = latest_stable_version[1:] | ||
|
||
if current_version != latest_stable_version: | ||
print(f"New version available: {latest_stable_version}") | ||
install_success = False | ||
|
||
if platform.system() == "Windows": | ||
choice = ( | ||
input("Do you want to update to the tagged version or the latest version? (tagged/latest): ").strip().lower() | ||
) | ||
try: | ||
if choice == "tagged": | ||
download_url = latest_stable_version["assets"][0]["browser_download_url"] | ||
file_name = latest_stable_version["assets"][0]["name"] | ||
elif choice == "latest": | ||
download_url = latest_build["assets"][0]["browser_download_url"] | ||
file_name = latest_build["assets"][0]["name"] | ||
except (KeyError, IndexError) as e: | ||
print(f"Error accessing latest build information: {e}") | ||
return | ||
|
||
try: | ||
install_success = download_and_install_inno_setup_release(download_url, file_name) # pylint: disable=possibly-used-before-assignment | ||
except Exception as e: # pylint: disable=broad-exception-caught | ||
print(f"Error downloading and installing Inno Setup release: {e}") | ||
install_success = False | ||
else: | ||
try: | ||
install_success = bool(download_and_install_pip_release()) | ||
except Exception as e: # pylint: disable=broad-exception-caught | ||
print(f"Error downloading and installing pip release: {e}") | ||
install_success = False | ||
|
||
if install_success: | ||
print("Restarting program...") | ||
# restart_program() | ||
else: | ||
print("You are already running the latest version.") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |