Skip to content

Instantly share code, notes, and snippets.

@getchoo
Created January 22, 2023 00:30
Show Gist options
  • Select an option

  • Save getchoo/4d8cb60bcc4bc625adc99bbc93813a9a to your computer and use it in GitHub Desktop.

Select an option

Save getchoo/4d8cb60bcc4bc625adc99bbc93813a9a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import requests
import re
import toml
PKGDIR = os.getenv("HOME") + "/pkgbuilds"
REGEX_VER = r'pkgver=(.+)'
REGEX_REL = r'pkgrel=(.+)'
API_HEADER = { 'Authorization': 'Bearer ' } # put your github key here
class UpdateInfo:
def __init__(self, file):
with open(file) as f:
self.data = toml.load(f)
self.pkgbuild = os.path.join(os.path.dirname(file), "PKGBUILD")
def __get_val(self, key):
try:
res = self.data[key]
except:
res = None
return res
def setup(self):
self.name = self.__get_val("name")
self.link = self.__get_val("link")
self.api_info = self.__get_val("api_info")
self.json_key = self.__get_val("json_key")
self.regex = self.__get_val("regex")
queue = []
for dirpath, dirnames, filenames in os.walk(PKGDIR):
for file in filenames:
if "update.toml" == file:
queue.append(os.path.join(dirpath, file))
if not queue:
exit("no update information found")
errors = {}
for file in queue:
try:
info = UpdateInfo(file)
info.setup()
resp = requests.get(info.link, headers=API_HEADER)
if resp.status_code != 200:
raise Exception("api request failed")
if info.json_key is None:
latest_ver = requests.get(info.link, headers=API_HEADER).json()[info.api_info]
else:
latest_ver = requests.get(
info.link).json()[info.json_key][info.api_info]
if info.regex is not None:
latest_ver = latest_ver.replace(info.regex[0],
info.regex[1])
if latest_ver.startswith("v"):
latest_ver = latest_ver[1:len(latest_ver)]
with open(info.pkgbuild, 'r') as f:
cur_ver = re.findall(REGEX_VER, txt := f.read())[0]
if cur_ver is None:
raise Exception(f"{info.name}: failed to find package version!")
if latest_ver == cur_ver:
print(f"{info.name}: up to date!")
continue
print(f"{info.name}: {cur_ver} -> {latest_ver}")
# newfile = re.sub(REGEX_VER, f"pkgver={latest_ver}", txt)
# newfile = re.sub(REGEX_REL, "pkgrel=1", newfile)
# with open(info.pkgbuild, "w") as f:
# f.write(newfile)
except Exception as e:
errors[info.name] = e
continue
if errors:
for name, error in errors.items():
print(f"error in checking {name}: {repr(error)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment