toot/tools/has_update

117 lines
3.4 KiB
Plaintext
Raw Normal View History

2024-09-18 11:04:27 +02:00
#!/usr/bin/env python3
2024-09-18 12:22:42 +02:00
# version: 0.2
# File: /srv/toot/tools/has_update
2024-09-18 11:04:27 +02:00
# Copyright (C) 2023 Cyrille Louarn <echolib+tools@a-lec.org>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>..
#----------------------------------------------------------------------
# XMPP: echolib (echolib@a-lec.org)
#
# Description:
# Search for release update using git
# ! Assume Project is insalled from git
#
# You may use with script (if you do not give argument to has_update)
# No output = no update
# Output update = v[latest] > v[current]
#
# Convenience -v argument (do not use with script): show both versions
#
# RC errors :
# 1 = Online Capture problem
# 2 = Installed Capture problem
#----------------------------------------------------------------------
import requests, subprocess, sys
# Set Project
#------------
owner = "mastodon" # Also used for sudo -u {owner}
name = "mastodon"
2024-09-18 11:09:46 +02:00
repo = f"https://api.github.com/repos/{owner}/{name}/releases/latest"
2024-09-18 11:04:27 +02:00
2024-09-18 11:09:46 +02:00
# VLH (Very Little Help)
2024-09-18 11:04:27 +02:00
helps = """Usage: has_update [argument]
2024-09-18 12:22:42 +02:00
! With no argument, a message is ouput ONLY if an update is available
2024-09-18 11:04:27 +02:00
-h : This help !
-v : Show both versions (online, and installed)
"""
2024-09-18 12:22:42 +02:00
2024-09-18 11:04:27 +02:00
#=====================================================#
# Check available version using Git API with requests #
#-----------------------------------------------------#
def get_available_release():
2024-09-18 11:09:46 +02:00
response = requests.get(repo)
2024-09-18 11:04:27 +02:00
if response.status_code == 200:
latest_release = response.json()
return latest_release['tag_name']
else:
print(f"Failed to get online lates release: {response.status_code}")
sys.exit(1)
#===========================================#
# Check installed version using git command #
# Assume Project is insalled from git #
#-------------------------------------------#
def get_installed_release():
try:
result = subprocess.run(
['sudo', '-u', owner, 'git', 'describe', '--tags', '--exact-match'],
stdout=subprocess.PIPE, # Get output
stderr=subprocess.PIPE, # Get error (if any)
text=True, # Set stdout + stderr
check=True # exception if failure
)
2024-09-18 12:22:42 +02:00
# Output if success
2024-09-18 11:04:27 +02:00
return result.stdout.strip()
2024-09-18 12:22:42 +02:00
# Command failed
2024-09-18 11:04:27 +02:00
except subprocess.CalledProcessError as e:
print("Failed to get installed release:", e.stderr.strip())
sys.exit(2)
#======#
# Main #
#------#
2024-09-18 12:22:42 +02:00
# With -h argument, show help only
2024-09-18 11:04:27 +02:00
if '-h' in sys.argv:
print(helps)
sys.exit(0)
2024-09-18 12:22:42 +02:00
2024-09-18 11:04:27 +02:00
# Get versions (online and local)
available_version = get_available_release()
installed_version = get_installed_release()
# With -v argument, show both versions
if '-v' in sys.argv:
print(f"{owner}:")
print(f"- Available: {available_version}\n- Installed: {installed_version}")
2024-09-18 11:04:27 +02:00
2024-09-18 12:22:42 +02:00
# Show message if a new version is available
2024-09-18 11:04:27 +02:00
elif available_version != installed_version:
print(f"{owner}: {installed_version} -> {available_version}")