diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-08-02 22:30:45 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-08-02 22:30:45 +0100 |
commit | 1bf87aaa088d975e6272f3e1a9722bf722b910e5 (patch) | |
tree | a67dd03bac3b8d3a0b01b7c1c62ace2f1c0092f9 | |
parent | 8003801e0bee1aa8e45fc0fb3f0743228959c23f (diff) | |
download | spacebeans-1bf87aaa088d975e6272f3e1a9722bf722b910e5.tar.gz spacebeans-1bf87aaa088d975e6272f3e1a9722bf722b910e5.zip |
Tool to generate a releases atom file
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | tools/atom.py | 99 |
2 files changed, 100 insertions, 0 deletions
@@ -4,4 +4,5 @@ *~ *.swp out/ +tools/releases.xml diff --git a/tools/atom.py b/tools/atom.py new file mode 100755 index 0000000..c3b3da4 --- /dev/null +++ b/tools/atom.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 + +import re +from datetime import datetime, timezone, date + +release = re.compile("^## Release ([0-9]+\\.[0-9]+\\.[0-9]+) - ([0-9]+-[0-9]+-[0-9]+)$") + +title = "SpaceBeans releases" +base_url = "https://www.usebox.net/jjm/spacebeans/" +feed_file = "releases.xml" +author = "jjm" +tz = timezone.utc + + +def get_entries(changes): + entries = [] + + with open(changes, "rt") as fd: + lines = fd.readlines() + + i = 0 + while i < len(lines): + line = lines[i] + m = release.match(line) + if m: + version = m.group(1) + updated_on = datetime.fromisoformat(m.group(2)).astimezone(tz) + body = "" + i += 1 + while i < len(lines) and (lines[i] == "" or lines[i][0] != "#"): + body += lines[i] + i += 1 + + entries.append(dict(version=version, updated_on=updated_on, body=body)) + else: + i += 1 + + entries.sort(key=lambda o: o["updated_on"], reverse=True) + return entries + + +def to_atom(post): + return """\ +<entry> + <title>{title}</title> + <link rel="alternate" href="{url}"/> + <id>{url}</id> + <updated>{updated_on}</updated> + <content type="xhtml"> + <div xmlns="http://www.w3.org/1999/xhtml"> + <pre>{body}</pre> + </div> + </content> +</entry>\n""".format( + title="Released SpaceBeans " + post["version"], + url=base_url + "#" + post["version"], + updated_on=datetime.combine( + post["updated_on"], datetime.min.time() + ).isoformat(), + body=post["body"].rstrip() + "\n", + ) + + +def gen_atom(entries): + + updated_on = datetime(1900, 1, 1, tzinfo=tz) + posts_atom = [] + for post in entries: + if post["updated_on"] > updated_on: + updated_on = post["updated_on"] + + posts_atom.append(to_atom(post)) + + return "\n".join( + [ + """<?xml version="1.0" encoding="utf-8"?>""", + """<feed xmlns="http://www.w3.org/2005/Atom">""", + """<title>%s</title>""" % title, + """<link href="%s"/>""" % base_url, + """<link rel="self" href="%s"/>""" % (base_url + feed_file), + """<updated>%s</updated>""" % updated_on.isoformat(), + """<id>%s</id>""" % base_url, + """<author>""", + """ <name>%s</name>""" % author, + """</author>""", + ] + + posts_atom + + ["</feed>"] + ) + + +def main(): + entries = get_entries("../CHANGES.md") + with open(feed_file, "wt") as fd: + fd.write(gen_atom(entries)) + + +if __name__ == "__main__": + main() |