|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Script to automatically update NEWS section in README files. |
| 5 | +Reads the first 10 news items from docs/NEWS.md and updates README.md and |
| 6 | +README_zh.md. |
| 7 | +""" |
| 8 | + |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def read_news_items(news_file: Path, max_items: int = 10) -> list[str]: |
| 13 | + """ |
| 14 | + Read news items from NEWS.md file. |
| 15 | +
|
| 16 | + Args: |
| 17 | + news_file (`Path`): |
| 18 | + Path to the NEWS.md file |
| 19 | + max_items (`int`, optional): |
| 20 | + Maximum number of items to read |
| 21 | +
|
| 22 | + Returns: |
| 23 | + `list[str]`: |
| 24 | + List of news items |
| 25 | + """ |
| 26 | + with open(news_file, "r", encoding="utf-8") as f: |
| 27 | + content = f.read() |
| 28 | + |
| 29 | + # Split by lines that start with "- **[" |
| 30 | + lines = content.strip().split("\n") |
| 31 | + news_items = [] |
| 32 | + |
| 33 | + for line in lines: |
| 34 | + if line.strip().startswith("- **["): |
| 35 | + news_items.append(line) |
| 36 | + if len(news_items) >= max_items: |
| 37 | + break |
| 38 | + |
| 39 | + return news_items |
| 40 | + |
| 41 | + |
| 42 | +def update_readme( |
| 43 | + readme_file: Path, |
| 44 | + news_items: list[str], |
| 45 | +) -> None: |
| 46 | + """ |
| 47 | + Update the NEWS section in README file using HTML comment markers. |
| 48 | +
|
| 49 | + Args: |
| 50 | + readme_file (`Path`): |
| 51 | + Path to the README file |
| 52 | + news_items (`list[str]`): |
| 53 | + List of news items to insert |
| 54 | + """ |
| 55 | + with open(readme_file, "r", encoding="utf-8") as f: |
| 56 | + content = f.read() |
| 57 | + |
| 58 | + # Use HTML comment markers to identify the NEWS section |
| 59 | + begin_marker = "<!-- BEGIN NEWS -->" |
| 60 | + end_marker = "<!-- END NEWS -->" |
| 61 | + |
| 62 | + if begin_marker not in content or end_marker not in content: |
| 63 | + print(f"⚠️ NEWS markers not found in {readme_file.name}") |
| 64 | + print( |
| 65 | + f" Please add '{begin_marker}' and '{end_marker}' to mark the " |
| 66 | + f"NEWS section", |
| 67 | + ) |
| 68 | + return |
| 69 | + |
| 70 | + # Find positions of markers |
| 71 | + begin_pos = content.find(begin_marker) |
| 72 | + end_pos = content.find(end_marker) |
| 73 | + |
| 74 | + if begin_pos == -1 or end_pos == -1 or begin_pos >= end_pos: |
| 75 | + print(f"❌ Invalid NEWS markers in {readme_file.name}") |
| 76 | + return |
| 77 | + |
| 78 | + # Create new NEWS content |
| 79 | + news_content = "\n".join(news_items) |
| 80 | + |
| 81 | + # Replace content between markers |
| 82 | + new_content = ( |
| 83 | + content[: begin_pos + len(begin_marker)] |
| 84 | + + "\n" |
| 85 | + + news_content |
| 86 | + + "\n" |
| 87 | + + content[end_pos:] |
| 88 | + ) |
| 89 | + |
| 90 | + with open(readme_file, "w", encoding="utf-8") as f: |
| 91 | + f.write(new_content) |
| 92 | + |
| 93 | + print(f"✅ Updated {readme_file.name}") |
| 94 | + |
| 95 | + |
| 96 | +def main() -> None: |
| 97 | + """Main function to update NEWS in README files.""" |
| 98 | + # Define paths |
| 99 | + repo_root = Path(__file__).parent.parent.parent |
| 100 | + news_file_en = repo_root / "docs" / "NEWS.md" |
| 101 | + news_file_zh = repo_root / "docs" / "NEWS_zh.md" |
| 102 | + readme_en = repo_root / "README.md" |
| 103 | + readme_zh = repo_root / "README_zh.md" |
| 104 | + |
| 105 | + # Update English README from NEWS.md |
| 106 | + if news_file_en.exists(): |
| 107 | + print(f"📖 Reading news items from {news_file_en}") |
| 108 | + news_items_en = read_news_items(news_file_en, max_items=10) |
| 109 | + print(f"📰 Found {len(news_items_en)} English news items") |
| 110 | + |
| 111 | + if news_items_en and readme_en.exists(): |
| 112 | + print(f"📝 Updating {readme_en.name}...") |
| 113 | + update_readme(readme_en, news_items_en) |
| 114 | + elif not news_items_en: |
| 115 | + print("⚠️ No English news items found") |
| 116 | + else: |
| 117 | + print(f"⚠️ {readme_en} not found") |
| 118 | + else: |
| 119 | + print(f"❌ NEWS.md not found at {news_file_en}") |
| 120 | + |
| 121 | + # Update Chinese README from NEWS_zh.md |
| 122 | + if news_file_zh.exists() and news_file_zh.stat().st_size > 0: |
| 123 | + print(f"📖 Reading news items from {news_file_zh}") |
| 124 | + news_items_zh = read_news_items(news_file_zh, max_items=10) |
| 125 | + print(f"📰 Found {len(news_items_zh)} Chinese news items") |
| 126 | + |
| 127 | + if news_items_zh and readme_zh.exists(): |
| 128 | + print(f"📝 Updating {readme_zh.name}...") |
| 129 | + update_readme(readme_zh, news_items_zh) |
| 130 | + elif not news_items_zh: |
| 131 | + print("⚠️ No Chinese news items found") |
| 132 | + else: |
| 133 | + print(f"⚠️ {readme_zh} not found") |
| 134 | + else: |
| 135 | + print( |
| 136 | + f"⚠️ NEWS_zh.md not found or empty at {news_file_zh}, " |
| 137 | + f"using English news for Chinese README", |
| 138 | + ) |
| 139 | + # Fallback: use English news for Chinese README if NEWS_zh.md |
| 140 | + # doesn't exist |
| 141 | + if news_file_en.exists() and readme_zh.exists(): |
| 142 | + print(f"📖 Reading news items from {news_file_en} (fallback)") |
| 143 | + news_items = read_news_items(news_file_en, max_items=10) |
| 144 | + if news_items: |
| 145 | + print(f"📝 Updating {readme_zh.name} with English news...") |
| 146 | + update_readme(readme_zh, news_items) |
| 147 | + |
| 148 | + print("✨ All done!") |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments