get-releases-json.py 1.34 KB
Newer Older
1 2
#!/usr/bin/env python3

3
import argparse
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import json
import subprocess

from distutils.version import StrictVersion


def get_versions():

    output = subprocess.check_output(
        ["git", "tag", "-l", "--format=%(creatordate:iso-strict)|%(refname:short)"]
    )
    tags = []

    for line in output.decode().splitlines():
        try:
            date, tag = line.split("|")
        except (ValueError):
            continue

        if not date or not tag:
            continue

        tags.append({"id": tag, "date": date})
27 28 29 30 31 32 33 34 35
    valid = []
    for tag in tags:
        try:
            StrictVersion(tag["id"])
            valid.append(tag)
        except ValueError:
            continue

    return sorted(valid, key=lambda tag: StrictVersion(tag["id"]), reverse=True)
36 37


38
def main(latest=False):
39
    versions = get_versions()
40 41 42 43 44
    if latest:
        print(versions[0]["id"])
    else:
        data = {"count": len(versions), "releases": versions}
        print(json.dumps(data, sort_keys=True, indent=2))
45 46 47


if __name__ == "__main__":
48 49 50 51 52 53 54 55 56 57 58 59 60
    parser = argparse.ArgumentParser(
        """
        Compile release data and output in in JSON format
        """
    )
    parser.add_argument(
        "-l",
        "--latest",
        action="store_true",
        help="Only print the latest version then exit",
    )
    args = parser.parse_args()
    main(latest=args.latest)