๐Ÿ“ฆ astral-sh / uv

๐Ÿ“„ reverse-changelog.py ยท 57 lines
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"""
Reverse the ordering of versions in a changelog file, i.e., when archiving a changelog.
"""

import re
import sys


def parse_changelog(content):
    """Parse the changelog content into individual version blocks."""
    # Use regex to split the content by version headers
    version_pattern = r"(?=## \d+\.\d+\.\d+)"
    version_blocks = re.split(version_pattern, content)

    # First item in the list is the header, which we want to preserve
    header = version_blocks[0]
    version_blocks = version_blocks[1:]

    return header, version_blocks


def reverse_changelog(content):
    """Reverse the order of version blocks in the changelog."""
    header, version_blocks = parse_changelog(content)

    # Reverse the version blocks
    reversed_blocks = version_blocks[::-1]

    # Combine the header and reversed blocks
    reversed_content = header + "".join(reversed_blocks)

    return reversed_content


def main():
    if len(sys.argv) < 2:
        print("Usage: reverse-changelog.py <changelog-file>")
        sys.exit(1)

    # Read the input file
    name = sys.argv[1]
    with open(name, "r") as file:
        content = file.read()

    # Reverse the changelog
    reversed_content = reverse_changelog(content)

    # Write the output to a new file
    with open(name, "w") as file:
        file.write(reversed_content)

    print(f"Updated {name}")


if __name__ == "__main__":
    main()