Skip to content

Commit

Permalink
FEATURE: check for software updates on the github server
Browse files Browse the repository at this point in the history
This is still work-in-progress
  • Loading branch information
amilcarlucas committed Dec 28, 2024
1 parent 00ab2c0 commit 4de8135
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/windows_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
134 changes: 134 additions & 0 deletions ardupilot_methodic_configurator/check_for_software_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/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

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:
response = requests.get(GITHUB_API_URL_LATEST_RELEASE, timeout=30)
response.raise_for_status()
return response.json()

Check failure on line 29 in ardupilot_methodic_configurator/check_for_software_updates.py

View workflow job for this annotation

GitHub Actions / mypy

ardupilot_methodic_configurator/check_for_software_updates.py#L29

[error] Returning Any from function declared to return


def get_latest_beta_build_info() -> dict:
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]

Check failure on line 37 in ardupilot_methodic_configurator/check_for_software_updates.py

View workflow job for this annotation

GitHub Actions / mypy

ardupilot_methodic_configurator/check_for_software_updates.py#L37

[error] Returning Any from function declared to return
return releases[1]

Check failure on line 38 in ardupilot_methodic_configurator/check_for_software_updates.py

View workflow job for this annotation

GitHub Actions / mypy

ardupilot_methodic_configurator/check_for_software_updates.py#L38

[error] Returning Any from function declared to 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:
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)
except Exception as e:
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:
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()

0 comments on commit 4de8135

Please sign in to comment.