117 lines
3.4 KiB
Python
117 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
# version: 0.2
|
|
# File: /srv/toot/tools/has_update
|
|
|
|
# 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"
|
|
repo = f"https://api.github.com/repos/{owner}/{name}/releases/latest"
|
|
|
|
|
|
# VLH (Very Little Help)
|
|
helps = """Usage: has_update [argument]
|
|
! With no argument, a message is ouput ONLY if an update is available
|
|
|
|
-h : This help !
|
|
-v : Show both versions (online, and installed)
|
|
"""
|
|
|
|
|
|
#=====================================================#
|
|
# Check available version using Git API with requests #
|
|
#-----------------------------------------------------#
|
|
def get_available_release():
|
|
response = requests.get(repo)
|
|
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
|
|
)
|
|
|
|
# Output if success
|
|
return result.stdout.strip()
|
|
|
|
# Command failed
|
|
except subprocess.CalledProcessError as e:
|
|
print("Failed to get installed release:", e.stderr.strip())
|
|
sys.exit(2)
|
|
|
|
|
|
#======#
|
|
# Main #
|
|
#------#
|
|
|
|
# With -h argument, show help only
|
|
if '-h' in sys.argv:
|
|
print(helps)
|
|
sys.exit(0)
|
|
|
|
|
|
# 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}")
|
|
|
|
# Show message if a new version is available
|
|
elif available_version != installed_version:
|
|
print(f"{owner}: {installed_version} -> {available_version}")
|
|
|