Skip to content

Commit

Permalink
Independent generation of latest.json file
Browse files Browse the repository at this point in the history
The new script loads releases from PyPI, independently of the changelog
generation script that used to handle this. If there is a prerelease
newer than the current stable version, that will be the `unstable`
version, instead of an alias to the current stable version.

Leans on `packaging.version` to do the heavy lifting (parsing version
specifiers into sortable `Version` objects). Fortunately we already get
`packaging` and `requests` for free when installing `_sopel` to document
it and its plugins.

Co-authored-by: James <snoopjedi@gmail.com>
  • Loading branch information
dgw and SnoopJ committed Jun 29, 2024
1 parent 9501668 commit 81876cf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
9 changes: 0 additions & 9 deletions document_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ def main(argv=None):

print("Done! {} versions documented.".format(len(versions.keys())))

latest = list(versions.keys())[0]
with open('latest.json', 'w') as f:
f.write("{{\n" # double braces so Python doesn't get confused
" \"version\": \"{version}\",\n"
" \"unstable\": \"{version}\",\n"
" \"release_notes\": \"https://sopel.chat/changelog/{version}/\"\n"
"}}".format(version=latest))

print("Generated latest.json file pointing to version {}.".format(latest))

if __name__ == '__main__':
main()
50 changes: 50 additions & 0 deletions generate_latest_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
"""
Sopel update-check JSON generation utility
This script creates a `latest.json` file representing the most recent stable and
unstable `sopel` releases published to PyPI, using PyPI's own JSON API.
Copyright 2024 dgw, technobabbl.es
Licensed under the Eiffel Forum License 2.
https://sopel.chat
"""
import json
import sys

from packaging.version import parse as parse_version
import requests


def main(argv=None):
response = requests.get('https://pypi.org/pypi/sopel/json')
response.raise_for_status()
data = response.json()
releases = sorted(parse_version(v) for v in data['releases'].keys())

latest = releases[-1]
stable = latest
if latest.is_prerelease:
stable_releases_rev = (ver for ver in reversed(releases[:-1]) if not ver.is_prerelease)
try:
stable = next(stable_releases_rev)
except StopIteration:
print("ERROR: Latest version is a prerelease, but no earlier stable version found!")
sys.exit(1)

with open('latest.json', 'w') as f:
sopel_version_info = {
"version": str(stable),
"unstable": str(latest),
"release_notes": f"https://sopel.chat/changelog/{stable}/",
"unstable_notes": f"https://github.com/sopel-irc/sopel/releases/v{latest}",
}
json.dump(sopel_version_info, f, indent=4)

print("Generated latest.json file pointing to {} (stable) + {} (unstable)."
.format(stable, latest))


if __name__ == '__main__':
main()
5 changes: 4 additions & 1 deletion netlify-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ set -e

echo "Starting custom build script..."

echo "Generating changelogs & latest.json file"
echo "Generating changelogs"
python3 document_versions.py --news=_sopel/NEWS

echo "Installing Sopel globally for plugin autodoc script"
pip3 install ./_sopel

echo "Generating latest.json file"
python3 generate_latest_json.py

echo "Generating plugin command/config pages"
python3 document_sopel_plugins.py --sopel=_sopel

Expand Down

0 comments on commit 81876cf

Please sign in to comment.