Fix linter warts

This commit is contained in:
Pedro Algarvio 2023-01-27 03:47:31 +00:00 committed by Megan Wilhite
parent d81df5bbe5
commit f9ed1fd313
2 changed files with 86 additions and 46 deletions

View file

@ -43,6 +43,7 @@ jobs:
- name: Pip Install Tools Requirements
run: |
pip3 install -r $(pwd)/requirements/static/ci/py3.10/tools.txt
pip3 install -r $(pwd)/requirements/static/ci/py3.10/changelog.txt
- name: Build RPM
env:

View file

@ -5,16 +5,12 @@ These commands are used manage Salt's changelog.
from __future__ import annotations
import datetime
import fnmatch
import logging
import os
import pathlib
import shutil
import subprocess
import sys
import textwrap
import yaml
from ptscripts import Context, command_group
log = logging.getLogger(__name__)
@ -29,7 +25,11 @@ def changelog(version):
"""
Return the full changelog generated by towncrier.
"""
proc = subprocess.run(["towncrier", "build", "--draft", f"--version={version}"], stdout=subprocess.PIPE)
proc = subprocess.run(
["towncrier", "build", "--draft", f"--version={version}"],
stdout=subprocess.PIPE,
check=True,
)
return proc.stdout.decode()
@ -39,39 +39,65 @@ def pkg_changelog(version):
"""
changes = changelog(version)
changes = "\n".join(changes.split("\n")[2:])
changes = changes.replace(textwrap.dedent("""
changes = changes.replace(
textwrap.dedent(
"""
Removed
-------
"""), "")
changes = changes.replace(textwrap.dedent("""
"""
),
"",
)
changes = changes.replace(
textwrap.dedent(
"""
Deprecated
----------
"""), "")
changes = changes.replace(textwrap.dedent("""
"""
),
"",
)
changes = changes.replace(
textwrap.dedent(
"""
Changed
-------
"""), "")
changes = changes.replace(textwrap.dedent("""
"""
),
"",
)
changes = changes.replace(
textwrap.dedent(
"""
Fixed
-----
"""), "")
changes = changes.replace(textwrap.dedent("""
"""
),
"",
)
changes = changes.replace(
textwrap.dedent(
"""
Added
-----
"""), "")
"""
),
"",
)
return changes
def version():
proc = subprocess.run(["python3", "-m", "salt.version"], stdout=subprocess.PIPE, check=True)
proc = subprocess.run(
["python3", "-m", "salt.version"], stdout=subprocess.PIPE, check=True
)
return proc.stdout.decode().strip()
DEFAULT_VERSION = version()
@cl.command(
name="update-rpm",
@ -82,27 +108,35 @@ DEFAULT_VERSION = version()
"it will be discovered by running 'python3 salt/version.py'."
),
"nargs": "?",
"default": DEFAULT_VERSION,
"default": None,
},
"draft": {
"help": (
"Do not make any changes, instead output what would be changed."
),
"help": ("Do not make any changes, instead output what would be changed."),
"type": "bool",
"default": False,
},
},
)
def update_rpm(ctx: Context, salt_version: str, draft: bool):
proc = subprocess.run(["towncrier", "build", "--draft", f"--version={salt_version}"], stdout=subprocess.PIPE)
changes = pgk_changelog(salt_version)
proc = subprocess.run(["sed", f"s/Version: .*/Version: {salt_version}/g", "pkg/rpm/salt.spec"], stdout=subprocess.PIPE, check=True)
if salt_version is None:
salt_version = version()
proc = subprocess.run(
["towncrier", "build", "--draft", f"--version={salt_version}"],
stdout=subprocess.PIPE,
check=True,
)
changes = pkg_changelog(salt_version)
proc = subprocess.run(
["sed", f"s/Version: .*/Version: {salt_version}/g", "pkg/rpm/salt.spec"],
stdout=subprocess.PIPE,
check=True,
)
orig = proc.stdout.decode()
dt = datetime.datetime.utcnow()
date = dt.strftime("%a %b %d %Y")
header = f"* {date} Salt Project Packaging <saltproject-packaging@vmware.com> - {salt_version}\n"
parts = orig.split('%changelog')
parts = orig.split("%changelog")
tmpspec = "pkg/rpm/salt.spec.1"
with open(tmpspec, "w") as wfp:
wfp.write(parts[0])
@ -111,7 +145,7 @@ def update_rpm(ctx: Context, salt_version: str, draft: bool):
wfp.write(changes)
wfp.write(parts[1])
try:
with open(tmpspec, "r") as rfp:
with open(tmpspec) as rfp:
if draft:
ctx.info(rfp.read())
else:
@ -130,31 +164,33 @@ def update_rpm(ctx: Context, salt_version: str, draft: bool):
"it will be discovered by running 'python3 salt/version.py'."
),
"nargs": "?",
"default": DEFAULT_VERSION,
"default": None,
},
"draft": {
"help": (
"Do not make any changes, instead output what would be changed."
),
"help": ("Do not make any changes, instead output what would be changed."),
"type": "bool",
"default": False,
},
},
)
def update_deb(ctx: Context, salt_version: str, draft: bool):
if salt_version is None:
salt_version = version()
changes = pkg_changelog(salt_version)
formated = "\n".join([f" {_.replace('-', '*', 1)}" for _ in changes.split('\n')])
formated = "\n".join([f" {_.replace('-', '*', 1)}" for _ in changes.split("\n")])
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:
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", "r") as rfp:
wfp.write(
f"\n -- Salt Project Packaging <saltproject-packaging@vmware.com> {date}\n\n"
)
with open("pkg/debian/changelog") as rfp:
wfp.write(rfp.read())
try:
with open(tmpchanges, "r") as rfp:
with open(tmpchanges) as rfp:
if draft:
ctx.info(rfp.read())
else:
@ -163,6 +199,7 @@ def update_deb(ctx: Context, salt_version: str, draft: bool):
finally:
os.remove(tmpchanges)
@cl.command(
name="update-release-notes",
arguments={
@ -172,27 +209,27 @@ def update_deb(ctx: Context, salt_version: str, draft: bool):
"it will be discovered by running 'python3 salt/version.py'."
),
"nargs": "?",
"default": DEFAULT_VERSION,
"default": None,
},
"draft": {
"help": (
"Do not make any changes, instead output what would be changed."
),
"help": ("Do not make any changes, instead output what would be changed."),
"type": "bool",
"default": False,
},
},
)
def update_release_notes(ctx: Context, salt_version: str, draft: bool):
if "+" in version:
if salt_version is None:
salt_version = version()
if "+" in salt_version:
major_version = salt_version.split("+", 1)[0]
else:
major_vesrion = salt_versionversion
major_vesrion = salt_version
changes = changelog(salt_version)
changes = "\n".join(changes.split("\n")[2:])
tmpnotes = f"doc/topics/releases/{version}.rst.tmp"
try:
with open(f"doc/topics/releases/{major_version}.rst", "r") as rfp:
with open(f"doc/topics/releases/{major_version}.rst") as rfp:
existing = rfp.read()
except FileNotFoundError:
existing = ""
@ -200,14 +237,14 @@ def update_release_notes(ctx: Context, salt_version: str, draft: bool):
wfp.write(existing)
wfp.write(changes)
try:
with open(tmpnotes, "r") as rfp:
with open(tmpnotes) as rfp:
if draft:
ctx.info(rfp.read())
else:
with open(f"doc/topics/releases/{version}.rst", "w") as wfp:
wfp.write(rfp.read())
finally:
os.remove(tmpchanges)
os.remove(tmpnotes)
@cl.command(
@ -219,7 +256,7 @@ def update_release_notes(ctx: Context, salt_version: str, draft: bool):
"it will be discovered by running 'python3 salt/version.py'."
),
"nargs": "?",
"default": DEFAULT_VERSION,
"default": None,
},
"draft": {
"help": (
@ -231,9 +268,11 @@ def update_release_notes(ctx: Context, salt_version: str, draft: bool):
},
},
)
def update_release_notes(ctx: Context, salt_version: str, draft: bool):
def generate_changelog_md(ctx: Context, salt_version: str, draft: bool):
if salt_version is None:
salt_version = version()
cmd = ["towncrier", "build", "--version={version}"]
if draft:
cmd += ["--draft"]
proc = subprocess.run(cmd, stdout=subprocess.PIPE)
proc = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
return proc.stdout.decode()