appImage-updater/update.py

131 lines
4.2 KiB
Python

import requests
import logging
import os
import argparse
from tqdm import tqdm
import re
githubApi = "https://api.github.com/repos"
applications = {
"freecad": { "org": "FreeCAD", # https://github.com/FreeCAD/FreeCAD-Bundle/releases
"repo": "FreeCAD-Bundle",
"assetRegex": "FreeCAD_weekly-builds-.*-x86_64-.*AppImage$" },
"bambustudio": { "org": "bambulab", # https://github.com/bambulab/BambuStudio/releases
"repo": "BambuStudio",
# "assetRegex": "Bambu_Studio_linux_ubuntu_24.*AppImage$" },
"assetRegex": "Bambu_Studio_ubuntu.*AppImage$" },
}
# get script path
scriptFolder = os.path.dirname(os.path.realpath(__file__))
# Cortesy: https://gist.github.com/yanqd0/c13ed29e29432e3cf3e7c38467f42f51
def download(url: str, fname: str, chunk_size=1024):
log.info("Downloading " + url)
resp = requests.get(url, stream=True)
total = int(resp.headers.get('content-length', 0))
with open(fname, 'wb') as file, tqdm(
desc=fname,
total=total,
unit='iB',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in resp.iter_content(chunk_size=chunk_size):
size = file.write(data)
bar.update(size)
def main(app):
org = applications[app]["org"]
repo = applications[app]["repo"]
# releaseTag = applications[app]["releaseTag"]
url = githubApi + "/" + org + "/" + repo + "/releases"
log.debug("url: " + url)
response = requests.get(url)
data = response.json()
# log.info("response: " + str(data))
assets = data[0]["assets"]
# log.info("Assets: " + str(assets))
assetName = None
assetUrl = None
for asset in assets:
# Eg. "FreeCAD_weekly-builds-38467-conda-Linux-x86_64-py311.AppImage"
# regex match
log.debug("Asset: " + asset["name"])
if re.match(applications[app]["assetRegex"], asset["name"]):
log.debug("Asset: " + str(asset["name"]))
assetName = asset["name"]
assetUrl = asset["browser_download_url"]
break
if assetName:
# if file exists
if os.path.isfile(scriptFolder + "/" + assetName):
log.info("Application " + app + " is already up-to-date (" + assetName + ")")
else: # Download it
# Eg. https://github.com/FreeCAD/FreeCAD-Bundle/releases/download/weekly-builds/FreeCAD_weekly-builds-38467-conda-Linux-x86_64-py311.AppImage
log.debug("url: " + assetUrl)
# Download to file with progress bar
download(assetUrl, scriptFolder + "/" + assetName + ".tmp")
# Rename
os.rename(scriptFolder + "/" + assetName + ".tmp", assetName)
# todo remove old appimages
# chmod
os.chmod(scriptFolder + "/" + assetName, 0o755)
# get user home directory
home = os.path.expanduser("~")
# link
if os.path.isfile(scriptFolder + "/" + app):
os.remove(scriptFolder + "/" + app)
os.symlink(scriptFolder + "/" + assetName, scriptFolder + "/" + app)
# Add link to start menu
desktopLink = home + "/.local/share/applications/" + app + ".desktop"
if os.path.isfile(desktopLink):
os.remove(desktopLink)
os.symlink(scriptFolder + "/" + app + ".desktop", desktopLink)
# log.info("Application " + app + " updated")
else:
log.error("No matching asset found in the latest release!")
exit(2)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--app", type=str, required=True, help="Application name")
parser.add_argument("-v", action='store_true', help="Show verbose log")
args = parser.parse_args()
if args.v:
loglevel = logging.DEBUG
else:
loglevel = logging.INFO
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', level=loglevel, datefmt='%Y-%m-%d %H:%M:%S')
log = logging.getLogger(__name__)
for app in applications:
if app == args.app:
main(app)
exit(0)
break
log.error("Application '" + args.app + "' not found")
log.error("Supported applications: '" + "', '".join((applications.keys())) + "'")
exit(1)