Debian Codegraph @

docs/changelog_formatter.py

raw
1 #!/usr/bin/env python3
2
3 import re
4 import sys
5
6 import click
7 import pyperclip
8 from colorama import Fore, init
9
10 # Initialize colorama for color support in terminal
11 init(autoreset=True)
12
13 # Regular expression pattern to match the required lines
14 PATTERN = re.compile(r"^\*\s*(.*?)\s+by\s+@[\w-]+\s+in\s+https://github\.com/[\w-]+/[\w-]+/pull/(\d+)")
15
16
17 def read_changes_file(filename):
18 try:
19 with open(filename) as f:
20 return f.readlines()
21 except FileNotFoundError:
22 print(f"Error: {filename} file not found.")
23 sys.exit(1)
24
25
26 def read_from_clipboard():
27 text = pyperclip.paste()
28 return text.splitlines()
29
30
31 def process_line(line):
32 line = line.strip()
33
34 # Skip lines containing '[pre-commit.ci]'
35 if "[pre-commit.ci]" in line:
36 return None
37
38 # Skip lines starting with '## What's Changed'
39 if line.startswith("## What's Changed"):
40 return None
41
42 # Stop processing if '## New Contributors' is encountered
43 if line.startswith("## New Contributors"):
44 return "STOP_PROCESSING"
45
46 # Skip lines that don't start with '* '
47 if not line.startswith("* "):
48 return None
49
50 match = PATTERN.match(line)
51 if match:
52 description, pr_number = match.groups()
53 return f"- {description} (#{pr_number})"
54 return None
55
56
57 @click.command()
58 @click.option(
59 "--source",
60 "-s",
61 type=click.Path(exists=True),
62 help="Source file to read from. If not provided, reads from clipboard.",
63 )
64 @click.option(
65 "--dest",
66 "-d",
67 type=click.File("w"),
68 default="-",
69 help="Destination file to write to. Defaults to standard output.",
70 )
71 @click.option(
72 "--clipboard",
73 "-c",
74 is_flag=True,
75 help="Read input from clipboard explicitly.",
76 )
77 def main(source, dest, clipboard):
78 # Determine the source of input
79 if clipboard or (not source and not sys.stdin.isatty()):
80 # Read from clipboard
81 lines = read_from_clipboard()
82 elif source:
83 # Read from specified file
84 lines = read_changes_file(source)
85 else:
86 # Default: read from clipboard
87 lines = read_from_clipboard()
88
89 output_lines = []
90 for line in lines:
91 output_line = process_line(line)
92 if output_line == "STOP_PROCESSING":
93 break
94 if output_line:
95 output_lines.append(output_line)
96
97 output_text = "\n".join(output_lines)
98
99 # Prepare the header
100 version = "x.y.z"
101 underline = "=" * len(version)
102
103 header = f"""
104 .. _version-{version}:
105
106 {version}
107 {underline}
108
109 :release-date: <YYYY-MM-DD>
110 :release-by: <FULL NAME>
111
112 What's Changed
113 ~~~~~~~~~~~~~~
114 """
115
116 # Combine header and output
117 final_output = header + output_text
118
119 # Write output to destination
120 if dest.name == "<stdout>":
121 print(Fore.GREEN + "Copy the following text to Changelog.rst:")
122 print(Fore.YELLOW + header)
123 print(Fore.CYAN + output_text)
124 else:
125 dest.write(final_output + "\n")
126 dest.close()
127
128
129 if __name__ == "__main__":
130 main()