58 lines
1.3 KiB
Python
58 lines
1.3 KiB
Python
import os
|
|
import re
|
|
import argparse
|
|
from pathlib import Path
|
|
from tempfile import mkstemp
|
|
|
|
VERSION_HEADER = re.compile(r"\[\d+.\d+.\d+\]")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("version")
|
|
args = parser.parse_args()
|
|
|
|
changelog = get_changelog(args.version)
|
|
outfilename = save_changelog(args.version, changelog)
|
|
|
|
print(outfilename)
|
|
|
|
|
|
def get_changelog(version: str) -> list[str]:
|
|
script_dir = Path(__file__).absolute().parent.parent
|
|
changelog = script_dir / "CHANGELOG.md"
|
|
|
|
with changelog.open("r") as infile:
|
|
lines = infile.readlines()
|
|
|
|
start = -1
|
|
end = -1
|
|
|
|
for i, line in enumerate(lines):
|
|
if start == -1:
|
|
match = re.search(version, line)
|
|
if match is None:
|
|
continue
|
|
start = i
|
|
else:
|
|
match = VERSION_HEADER.search(line)
|
|
if match is not None:
|
|
end = i
|
|
break
|
|
|
|
if start == -1:
|
|
exit(1)
|
|
|
|
return lines[start:end] if end > -1 else lines[start:]
|
|
|
|
|
|
def save_changelog(version: str, lines: list[str]) -> str:
|
|
fd, name = mkstemp(prefix=f"wapp-changelog-{version}-", text=True)
|
|
os.write(fd, "".join(lines).encode("utf-8"))
|
|
os.close(fd)
|
|
return name
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|