Files
autopkg/main.py
phantom cdc72f1557
All checks were successful
Build Docker Image / build (push) Successful in 27s
Implement version checking and basic refactoring
2025-11-29 13:58:22 -06:00

102 lines
3.0 KiB
Python

import os
import re
import logging
from configparser import ConfigParser
import requests
from logger import setup_log
from package import Package
## Autopkg Apps
apps = {
"bitcoinknots": {
"url": "https://api.github.com/repos/bitcoinknots/bitcoin/releases/latest",
"versioningScheme": r"^v(\d+)\.(\d+)\.(\d+)$"
},
"electrs": {"url": "https://api.github.com/repos/romanz/electrs/releases/latest"}
}
class Autopkg:
def __init__(self):
# Logging Setup
setup_log()
logging.info("Running autopkg (c) 2025 phantom <phantom@shadeouts.net> https://shadeouts.net/")
# Environment Setup
target = os.getenv("APKG_TARGET")
os.environ["APKG_RUNTIME_DIR"] = os.path.dirname(os.path.abspath(__file__)) # Set runtime directory
match target:
case "prod":
self.datafile = "/etc/autopkg/data.ini"
case "test" | "debug":
logging.warning("Running autopkg in testing mode!")
self.datafile = os.getenv("APKG_RUNTIME_DIR") + "/test/data.ini"
if os.path.exists(self.datafile): os.remove(self.datafile)
case _:
raise ValueError("APKG_TARGET environment variable not setup correctly!")
## Config File Setup
self.data = ConfigParser()
self.data.read(self.datafile)
## Setup file if not present
if "Versions" not in self.data:
self.data.add_section("Versions")
self.write_data()
# Write datafile changes
def write_data(self):
with open(self.datafile, "w") as f:
self.data.write(f)
# Main Application
def main(self):
# Handle apps (get versions)
for app, info in apps.items():
ini_version = self.data.get("Versions", app, fallback=None) # Version previously packaged from data.ini
response = requests.get(info["url"]) # Fetch data from API
# Parse through JSON data
if response.status_code == 200:
json = response.json()
tag = json["tag_name"] # App version from API
pre_release = json["prerelease"]
# Package new versions if available
if tag != ini_version and not pre_release:
logging.info(f"Found new version ({tag}) for {app}, packaging..")
try:
if Package(app, tag).build():
ini_version = str(tag)
self.write_data()
except Exception as e:
logging.error(f"Error starting docker container for {app}")
def version_is_higher(tag: str, info: dict) -> (str, None):
regular_scheme = r"^v(\d+)\.(\d+)\.(\d+)$"
try:
version = re.match(info["versioningScheme"], tag)
except:
version = re.match(regular_scheme)
if __name__ == "__main__":
# Call main function
Autopkg().main()