Skip to content

Commit

Permalink
tools/create_release_notes: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Apr 20, 2023
1 parent 0e47f45 commit 23f098c
Showing 1 changed file with 57 additions and 74 deletions.
131 changes: 57 additions & 74 deletions tools/create_release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,55 @@
"""

from pathlib import Path
from os.path import relpath
from glob import glob
from subprocess import check_call, check_output, CalledProcessError
from shutil import which
import datetime


def get_releases(source_path: Path):
"""
Get all releases defined by release note files
"""
release_notes = source_path / "release_notes"
releases = []
for idx, file_name in enumerate(sorted(glob(str(release_notes / "*.rst")), reverse=True)):
releases.append(Release(file_name, is_latest=idx == 0))
return releases
from datetime import datetime


def create_release_notes():
"""
Create monolithic release notes file from several input files
"""
source_path = Path(__file__).parent.parent / "docs"

check_call(["towncrier", "build", "--keep"], cwd=source_path.parent)

releases = get_releases(source_path)
latest_release = releases[0]

with (source_path / "release_notes.inc").open("w", encoding="utf-8") as fptr:
fptr.write(
"`Commits since last release "
f"<https://github.com/VUnit/vunit/compare/{latest_release.tag!s}...master>`__"
root = Path(__file__).parent.parent
docsroot = root / "docs"

check_call(["towncrier", "build", "--keep"], cwd=root)

# Get all releases defined by release note files
releases = [
Release(file_name, is_latest=idx == 0)
for idx, file_name in enumerate(sorted((docsroot / "release_notes").glob("*.rst"), reverse=True))
]

content = (
"`Commits since last release "
f"<https://github.com/VUnit/vunit/compare/v{releases[0].name!s}...master>`__"
"\n\n"
)

for idx, release in enumerate(releases):
title = f":vunit_commit:`{release.name!s} <v{release.name!s}>` - {release.date.strftime('%Y-%m-%d')}"

if idx == 0:
title += " (latest)"
content += ".. _release:latest:\n\n"

content += (
f".. _release:{release.name}:\n\n"
f"{title}\n{'-' * len(title)}\n\n"
f"`Download from PyPI <https://pypi.python.org/pypi/vunit_hdl/{release.name!s}/>`__"
)

fptr.write("\n\n")

for idx, release in enumerate(releases):
is_last = idx == len(releases) - 1

if release.is_latest:
fptr.write(".. _release:latest:\n\n")

fptr.write(f".. _release:{release.name}:\n\n")
if idx != len(releases) - 1:
content += (
f" | `Commits since previous release "
f"<https://github.com/VUnit/vunit/compare/v{releases[idx + 1].name!s}...v{release.name!s}>`__"
)

title = f":vunit_commit:`{release.name!s} <{release.tag!s}>` - {release.date.strftime('%Y-%m-%d')!s}"
if release.is_latest:
title += " (latest)"
fptr.write(title + "\n")
fptr.write("-" * len(title) + "\n\n")
content += f"\n\n.. include:: release_notes/{release.name}{release.suffix}\n\n\n"

fptr.write(f"\n`Download from PyPI <https://pypi.python.org/pypi/vunit_hdl/{release.name!s}/>`__")

if not is_last:
fptr.write(
f" | `Commits since previous release "
f"<https://github.com/VUnit/vunit/compare/{releases[idx + 1].tag!s}...{release.tag!s}>`__"
)

fptr.write(f"\n\n.. include:: {relpath(release.file_name, source_path)!s}\n\n")
with open(str(docsroot / "release_notes.inc"), "w", encoding="utf-8") as fptr:
fptr.write(content)


class Release(object):
Expand All @@ -77,33 +66,27 @@ class Release(object):
"""

def __init__(self, file_name, is_latest):
self.file_name = file_name
self.name = str(Path(file_name).with_suffix("").name)
self.tag = "v" + self.name
self.is_latest = is_latest
self.suffix = file_name.suffix
self.name = file_name.stem
tag = "v" + self.name

try:
self.date = _get_date(self.tag)
git = which("git")
if git is None:
raise BaseException("'git' is required!")

try:
self._get_date(git, tag)
except CalledProcessError:
if self.is_latest:
# Release tag for latest release not yet created, assume HEAD will become release
print(f"Release tag {self.tag!s} not created yet, use HEAD for date")
self.date = _get_date("HEAD")
else:
if not is_latest:
raise

with Path(file_name).open("r", encoding="utf-8") as fptr:
self.notes = fptr.read()


def _get_date(commit):
"""
Get date
"""
git = which("git")
if git is None:
raise BaseException("'git' is required!")
date_str = check_output([git, "log", "-1", "--format=%ci", commit]).decode().strip()
date_str = " ".join(date_str.split(" ")[0:2])
return datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(f"Release tag {tag} not created yet, use HEAD for date")
self._get_date(git, "HEAD")

def _get_date(self, git, ref):
"""
Get date
"""
self.date = datetime.strptime(
" ".join(check_output([git, "log", "-1", "--format=%ci", ref]).decode().strip().split(" ")[0:2]),
"%Y-%m-%d %H:%M:%S",
)

0 comments on commit 23f098c

Please sign in to comment.