Additional fixes to tools/changelog.py

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
Pedro Algarvio 2023-02-24 15:34:12 +00:00 committed by Pedro Algarvio
parent d5ac32b6f2
commit 84797fe353

View file

@ -14,9 +14,8 @@ import textwrap
from ptscripts import Context, command_group
import tools.utils
from tools.utils import REPO_ROOT, Version
REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent
CHANGELOG_LIKE_RE = re.compile(r"([\d]+)\.([a-z]+)$")
CHANGELOG_TYPES = (
"removed",
@ -39,7 +38,7 @@ changelog = command_group(
description=__doc__,
venv_config={
"requirements_files": [
tools.utils.REPO_ROOT
REPO_ROOT
/ "requirements"
/ "static"
/ "ci"
@ -147,7 +146,7 @@ def check_changelog_entries(ctx: Context, files: list[pathlib.Path]):
ctx.exit(exitcode)
def _get_changelog_contents(ctx: Context, version: str):
def _get_changelog_contents(ctx: Context, version: Version):
"""
Return the full changelog generated by towncrier.
"""
@ -165,7 +164,7 @@ def _get_changelog_contents(ctx: Context, version: str):
return ret.stdout.decode()
def _get_pkg_changelog_contents(ctx: Context, version: str):
def _get_pkg_changelog_contents(ctx: Context, version: Version):
"""
Return a version of the changelog entries suitable for packaged changelogs.
"""
@ -180,7 +179,7 @@ def _get_salt_version(ctx):
if ret.returncode:
ctx.error(ret.stderr.decode())
ctx.exit(1)
return ret.stdout.decode().strip()
return Version(ret.stdout.decode().strip())
@changelog.command(
@ -199,7 +198,7 @@ def _get_salt_version(ctx):
},
},
)
def update_rpm(ctx: Context, salt_version: str, draft: bool = False):
def update_rpm(ctx: Context, salt_version: Version, draft: bool = False):
if salt_version is None:
salt_version = _get_salt_version(ctx)
changes = _get_pkg_changelog_contents(ctx, salt_version)
@ -249,7 +248,7 @@ def update_rpm(ctx: Context, salt_version: str, draft: bool = False):
},
},
)
def update_deb(ctx: Context, salt_version: str, draft: bool = False):
def update_deb(ctx: Context, salt_version: Version, draft: bool = False):
if salt_version is None:
salt_version = _get_salt_version(ctx)
changes = _get_pkg_changelog_contents(ctx, salt_version)
@ -257,23 +256,25 @@ def update_deb(ctx: Context, salt_version: str, draft: bool = False):
dt = datetime.datetime.utcnow()
date = dt.strftime("%a, %d %b %Y %H:%M:%S +0000")
tmpchanges = "pkg/rpm/salt.spec.1"
with open(tmpchanges, "w") as wfp:
debian_changelog_path = "pkg/debian/changelog"
tmp_debian_changelog_path = f"{debian_changelog_path}.1"
with open(tmp_debian_changelog_path, "w") as wfp:
wfp.write(f"salt ({salt_version}) stable; urgency=medium\n\n")
wfp.write(formated)
wfp.write(
f"\n -- Salt Project Packaging <saltproject-packaging@vmware.com> {date}\n\n"
)
with open("pkg/debian/changelog") as rfp:
with open(debian_changelog_path) as rfp:
wfp.write(rfp.read())
try:
with open(tmpchanges) as rfp:
with open(tmp_debian_changelog_path) as rfp:
if draft:
ctx.info(rfp.read())
else:
with open("pkg/debian/changelog", "w") as wfp:
with open(debian_changelog_path, "w") as wfp:
wfp.write(rfp.read())
finally:
os.remove(tmpchanges)
os.remove(tmp_debian_changelog_path)
@changelog.command(
@ -296,30 +297,31 @@ def update_deb(ctx: Context, salt_version: str, draft: bool = False):
},
)
def update_release_notes(
ctx: Context, salt_version: str, draft: bool = False, release: bool = False
ctx: Context, salt_version: Version, draft: bool = False, release: bool = False
):
if salt_version is None:
salt_version = _get_salt_version(ctx)
if "+" in salt_version:
major_version = salt_version.split("+", 1)[0]
else:
major_version = salt_version
changes = _get_changelog_contents(ctx, salt_version)
changes = "\n".join(changes.split("\n")[2:])
release_notes_path = f"doc/topics/releases/{major_version}.md"
try:
with open(release_notes_path) as rfp:
existing = rfp.read()
except FileNotFoundError:
existing = textwrap.dedent(
f"""\
[](#release-{salt_version})
release_notes_path = "doc/topics/releases/{}.md".format(
".".join(str(part) for part in salt_version.release)
)
if not os.path.exists(release_notes_path):
pathlib.Path(release_notes_path).write_text(
textwrap.dedent(
f"""\
[](#release-{salt_version})
# Salt {salt_version} release notes - UNRELEASED
"""
# Salt {salt_version} release notes - UNRELEASED
"""
)
)
pathlib.Path(release_notes_path).touch()
ctx.run("git", "add", release_notes_path)
ctx.info(f"Created bare {release_notes_path} release notes file")
with open(release_notes_path) as rfp:
existing = rfp.read()
if release is True:
existing = existing.replace(" - UNRELEASED", "")
@ -356,7 +358,7 @@ def update_release_notes(
},
},
)
def generate_changelog_md(ctx: Context, salt_version: str, draft: bool = False):
def generate_changelog_md(ctx: Context, salt_version: Version, draft: bool = False):
if salt_version is None:
salt_version = _get_salt_version(ctx)
cmd = ["towncrier", "build", f"--version={salt_version}"]