|
| 1 | +import re |
| 2 | +import sys |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | + |
| 6 | +def parse_changelog(version: str) -> Optional[str]: |
| 7 | + """Parse CHANGELOG.md and return the content for the specified version. |
| 8 | +
|
| 9 | + Args: |
| 10 | + version: Version string to find in the changelog (e.g. "2024.12.16") |
| 11 | +
|
| 12 | + Returns: |
| 13 | + String containing the changelog entry for the specified version, |
| 14 | + or None if version not found |
| 15 | + """ |
| 16 | + with open("CHANGELOG.md", "r") as f: |
| 17 | + content = f.read() |
| 18 | + |
| 19 | + # Pattern to match version headers |
| 20 | + version_header_pattern = r"## \[([\d.]+)\]" |
| 21 | + |
| 22 | + # Find all version headers and their positions |
| 23 | + version_matches = list(re.finditer(version_header_pattern, content)) |
| 24 | + |
| 25 | + # Find the index of our target version |
| 26 | + target_index = None |
| 27 | + for i, match in enumerate(version_matches): |
| 28 | + if match.group(1) == version: |
| 29 | + target_index = i |
| 30 | + break |
| 31 | + |
| 32 | + if target_index is None: |
| 33 | + print(f"No changelog entry found for version {version}") |
| 34 | + return None |
| 35 | + |
| 36 | + # Get the start position (right after the version header) |
| 37 | + start_pos = version_matches[target_index].end() |
| 38 | + |
| 39 | + # Get the end position (start of next version or end of file) |
| 40 | + if target_index + 1 < len(version_matches): |
| 41 | + end_pos = version_matches[target_index + 1].start() |
| 42 | + else: |
| 43 | + end_pos = len(content) |
| 44 | + |
| 45 | + # Extract the content between these positions |
| 46 | + changelog_content = content[start_pos:end_pos] |
| 47 | + |
| 48 | + # Clean up the content |
| 49 | + # Remove leading/trailing whitespace and empty lines while preserving internal formatting |
| 50 | + cleaned_lines = [] |
| 51 | + for line in changelog_content.split('\n'): |
| 52 | + if line.strip() or cleaned_lines: # Keep empty lines only after we've started collecting content |
| 53 | + cleaned_lines.append(line) |
| 54 | + |
| 55 | + # Remove trailing empty lines |
| 56 | + while cleaned_lines and not cleaned_lines[-1].strip(): |
| 57 | + cleaned_lines.pop() |
| 58 | + |
| 59 | + return '\n'.join(cleaned_lines) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + if len(sys.argv) != 2: |
| 64 | + print("Usage: python parse_changelog.py VERSION") |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | + version = sys.argv[1] |
| 68 | + changelog_content = parse_changelog(version) |
| 69 | + |
| 70 | + if changelog_content: |
| 71 | + with open("RELEASE_NOTES.md", "w") as f: |
| 72 | + f.write(changelog_content) |
| 73 | + print("Successfully extracted changelog content") |
| 74 | + else: |
| 75 | + print("Failed to extract changelog content") |
| 76 | + sys.exit(1) |
0 commit comments