#!/usr/bin/env python3 import os import re import yaml import tempfile import subprocess from email.utils import parsedate_to_datetime from debian.changelog import Changelog BASE_DIR = os.path.dirname(__file__) TEMPLATE = """ --- layout: post title: diffoscope {version} released author: {author} --- The diffoscope maintainers are pleased to announce the release of diffoscope version `{version}`. This version includes the following changes: ``` {changes} ``` You find out more by [visiting the project homepage](https://diffoscope.org). """ def main(): src = "https://salsa.debian.org/reproducible-builds/diffoscope.git" # Prefer a local checkout if it exists basepath = os.path.dirname(os.path.dirname(os.path.abspath(__file__))), candidates = [ os.path.join(*basepath, "diffoscope", ".git"), os.path.join(*basepath, "diffoscope", "diffoscope", ".git"), ] for candidate in candidates: if os.path.exists(candidate): src = candidate break with tempfile.TemporaryDirectory() as t: git_dir = os.path.join(t, "diffoscope") subprocess.check_call(("git", "clone", src, git_dir)) releases = get_releases(git_dir) debian_substvars = get_debian_substvars(git_dir) data = { "latest_release": { "date": int(releases[0]["date"].timestamp()), "version": releases[0]["version"], }, "contributors": get_contributors(git_dir), "description": debian_substvars["diffoscope:Description"], } with open(os.path.join(BASE_DIR, "_data", "diffoscope.yml"), "w") as f: print("# This file is automatically generated\n", file=f) yaml.dump(data, stream=f) for x in releases: date_fmt = x["date"].strftime("%Y-%m-%d") target = os.path.join( BASE_DIR, "_posts", f"{date_fmt}-diffoscope-{x['version']}-released.md" ) with open(target, "w") as f: print(TEMPLATE.format(**x).strip(), file=f) def get_contributors(git_dir): result = set() emails = set() for x in git(git_dir, "log", "--format=%aN|%aE").splitlines(): name, email = x.rsplit("|", 1) if name != "root" and email not in emails: emails.add(email) result.add(name) return list(sorted(result, key=lambda x: x.lower())) def get_releases(git_dir): result = [] changelog = Changelog() with open(os.path.join(git_dir, "debian", "changelog")) as f: changelog.parse_changelog(f) for x in changelog: if x.distributions == "UNRELEASED": continue changes = ( (re.sub(r"^ ", "", "\n".join(x.changes()), flags=re.MULTILINE)) .lstrip("\n") .rstrip("\n") ) result.append( { "date": parsedate_to_datetime(x.date), "author": x.author, "changes": changes, "version": str(x.version), } ) return result def get_debian_substvars(git_dir): val = subprocess.check_output( ("bin/diffoscope", "--list-debian-substvars"), cwd=git_dir ) data = {} for x in val.decode("utf-8").splitlines(): key, _, val = x.partition("=") data[key] = val.replace("${Newline}", " ") return data def git(git_dir, *args): cmd = ("git",) + args val = subprocess.check_output(cmd, env={"GIT_DIR": os.path.join(git_dir, ".git")}) return val.decode("utf-8") if __name__ == "__main__": main()