|
| 1 | +#!/bin/python3 |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import re |
| 6 | +from collections import OrderedDict |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +def combine_sections_in_folder(folder_path): |
| 12 | + """Parser for changelog files following the https://keepachangelog.com |
| 13 | + format. |
| 14 | + """ |
| 15 | + allowed_section_headers = [ |
| 16 | + "Added", |
| 17 | + "Changed", |
| 18 | + "Deprecated", |
| 19 | + "Removed", |
| 20 | + "Fixed", |
| 21 | + "Security", |
| 22 | + ] |
| 23 | + sections = OrderedDict() |
| 24 | + |
| 25 | + for filename in os.listdir(folder_path): |
| 26 | + if filename.endswith(".md") and filename != "README.md": |
| 27 | + filepath = os.path.join(folder_path, filename) |
| 28 | + with open(filepath, "r") as file: |
| 29 | + current_section = None |
| 30 | + for line in file: |
| 31 | + # find all headings starting with # (they should always |
| 32 | + # start with ### but we seem to sometimes use # or ##) |
| 33 | + match = re.match(r"^#+ (.*)", line) |
| 34 | + if match: |
| 35 | + section_header = match.group(1).strip().capitalize() |
| 36 | + if section_header not in allowed_section_headers: |
| 37 | + logger.warning( |
| 38 | + f"warning: section_header {section_header} " |
| 39 | + f"in file {filename} is invalid, " |
| 40 | + "see https://keepachangelog.com for a list of " |
| 41 | + "allowed types." |
| 42 | + ) |
| 43 | + current_section = sections.get(section_header, []) |
| 44 | + elif current_section is not None and line.strip(): |
| 45 | + current_section.append(line) |
| 46 | + sections[section_header] = current_section |
| 47 | + |
| 48 | + combined_md = "" |
| 49 | + for section_header, lines in sections.items(): |
| 50 | + combined_md += "### " + section_header + "\n\n" |
| 51 | + combined_md += "".join(lines) + "\n" |
| 52 | + |
| 53 | + return combined_md |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + folder_path = "." |
| 58 | + combined_md = combine_sections_in_folder(folder_path) |
| 59 | + print(combined_md) |
0 commit comments