2020-11-21 19:03:50 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-12-22 10:09:26 +00:00
|
|
|
|
2020-11-21 19:03:50 +00:00
|
|
|
from sys import argv, exit
|
|
|
|
|
2021-12-22 10:09:26 +00:00
|
|
|
from requests import get
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Arch Linux (possibly future Ubuntu releases)
|
|
|
|
from packaging.version import parse
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
# Debian
|
|
|
|
from setuptools._vendor.packaging.version import parse
|
|
|
|
|
|
|
|
|
|
|
|
assert (
|
|
|
|
len(argv) >= 3
|
|
|
|
), "Usage: check_github_for_new_release <repo> <version> [tag prefix]"
|
2020-11-21 19:03:50 +00:00
|
|
|
|
|
|
|
repo = argv[1]
|
|
|
|
current_version = argv[2]
|
2021-12-22 10:09:26 +00:00
|
|
|
if len(argv) >= 4:
|
|
|
|
tag_prefix = argv[3]
|
|
|
|
else:
|
|
|
|
tag_prefix = ""
|
2020-11-21 19:03:50 +00:00
|
|
|
|
|
|
|
try:
|
2021-12-22 10:09:26 +00:00
|
|
|
r = get(
|
|
|
|
"https://api.github.com/repos/{}/releases".format(repo),
|
|
|
|
headers={"Accept": "application/json"},
|
|
|
|
)
|
|
|
|
r.raise_for_status()
|
2020-11-21 19:03:50 +00:00
|
|
|
|
2021-12-22 10:09:26 +00:00
|
|
|
releases = r.json()
|
2020-11-21 19:03:50 +00:00
|
|
|
|
2021-12-22 10:09:26 +00:00
|
|
|
newest_release = None
|
2020-11-21 19:03:50 +00:00
|
|
|
|
2021-12-22 10:09:26 +00:00
|
|
|
for i in releases:
|
|
|
|
if i["tag_name"].startswith(tag_prefix):
|
|
|
|
if newest_release is None or parse(i["tag_name"]) > parse(
|
|
|
|
newest_release["tag_name"]
|
|
|
|
):
|
|
|
|
newest_release = i
|
2020-11-21 19:03:50 +00:00
|
|
|
|
2021-12-22 10:09:26 +00:00
|
|
|
assert newest_release is not None, "Could not determine latest release"
|
|
|
|
|
|
|
|
if parse(newest_release["tag_name"]) > parse(current_version):
|
|
|
|
print(
|
|
|
|
"There is a newer version available: {} (currently installed: {})".format(
|
|
|
|
newest_release["tag_name"], current_version
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if newest_release["prerelease"] or newest_release["draft"]:
|
|
|
|
print("Please note this is only a prerelease/draft.")
|
|
|
|
|
|
|
|
# Don't alert people for prereleases.
|
|
|
|
exit(0)
|
|
|
|
else:
|
|
|
|
exit(2)
|
2020-11-21 19:03:50 +00:00
|
|
|
else:
|
2021-12-22 10:09:26 +00:00
|
|
|
print("Currently installed version matches newest release on github")
|
|
|
|
exit(0)
|
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|
|
|
|
exit(3)
|