|
| 1 | +""" |
| 2 | +Reverse the ordering of versions in a changelog file, i.e., when archiving a changelog. |
| 3 | +""" |
| 4 | + |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +def parse_changelog(content): |
| 10 | + """Parse the changelog content into individual version blocks.""" |
| 11 | + # Use regex to split the content by version headers |
| 12 | + version_pattern = r"(?=## \d+\.\d+\.\d+)" |
| 13 | + version_blocks = re.split(version_pattern, content) |
| 14 | + |
| 15 | + # First item in the list is the header, which we want to preserve |
| 16 | + header = version_blocks[0] |
| 17 | + version_blocks = version_blocks[1:] |
| 18 | + |
| 19 | + return header, version_blocks |
| 20 | + |
| 21 | + |
| 22 | +def reverse_changelog(content): |
| 23 | + """Reverse the order of version blocks in the changelog.""" |
| 24 | + header, version_blocks = parse_changelog(content) |
| 25 | + |
| 26 | + # Reverse the version blocks |
| 27 | + reversed_blocks = version_blocks[::-1] |
| 28 | + |
| 29 | + # Combine the header and reversed blocks |
| 30 | + reversed_content = header + "".join(reversed_blocks) |
| 31 | + |
| 32 | + return reversed_content |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + if len(sys.argv) < 2: |
| 37 | + print("Usage: reverse-changelog.py <changelog-file>") |
| 38 | + sys.exit(1) |
| 39 | + |
| 40 | + # Read the input file |
| 41 | + name = sys.argv[1] |
| 42 | + with open(name, "r") as file: |
| 43 | + content = file.read() |
| 44 | + |
| 45 | + # Reverse the changelog |
| 46 | + reversed_content = reverse_changelog(content) |
| 47 | + |
| 48 | + # Write the output to a new file |
| 49 | + with open(name, "w") as file: |
| 50 | + file.write(reversed_content) |
| 51 | + |
| 52 | + print(f"Updated {name}") |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + main() |
0 commit comments