1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
"""
Extract release notes for the given tag
"""
from __future__ import absolute_import, unicode_literals
import argparse
import io
import logging
import os
import re
import sys
from packaging import version as versiontool
logger = logging.getLogger(__name__)
GENERIC_MESSAGE = """
This release was automatically generated by the release pipeline.
""".strip()
def iterate_until_version(infile, version):
"""Read lines from `infile` until we have parsed a version heading in the
form of::
---------
v##.##.##
---------
at which point the next line from `infile` will be the first line after the
heading."""
history = []
ruler = re.compile("^-+$")
for line in infile:
line = line.rstrip()
history.append(line)
if len(history) < 3:
continue
try:
line_version = versiontool.parse(history[-2].strip().rstrip("v"))
except versiontool.InvalidVersion:
continue
if (ruler.match(history[-3]) and
ruler.match(history[-1]) and
(version is None or
line_version.base_version == version.base_version)):
break
if len(history) > 3:
for buffered_line in history[:-3]:
yield buffered_line
history = history[-3:]
for buffered_line in history:
yield buffered_line
def get_note_text(infile_path, versionstr):
try:
version = versiontool.parse(versionstr)
except versiontool.InvalidVersion:
return GENERIC_MESSAGE
with io.open(infile_path, "r", encoding="utf-8") as infile:
for _ in iterate_until_version(infile, version):
pass
lines = list(iterate_until_version(infile, None))
content = "\n".join(lines[:-3])
return re.sub(r"(\S)\n(\S)", r"\1\2", content).strip()
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("infile_path")
parser.add_argument("version")
parser.add_argument("-o", "--outfile-path", default="-")
args = parser.parse_args()
if args.outfile_path == "-":
args.outfile_path = os.dup(sys.stdout.fileno())
with io.open(args.outfile_path, "w", encoding="utf-8") as outfile:
outfile.write(get_note_text(args.infile_path, args.version))
outfile.write("\n")
if __name__ == "__main__":
main()
|