|
| 1 | +import os |
| 2 | +import re |
| 3 | +import glob |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +# Calculate the path to the repository root (2 levels up from this script) |
| 7 | +REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) |
| 8 | +README_PATH = os.path.join(REPO_ROOT, "README.md") |
| 9 | +contributors_dir = os.path.join(REPO_ROOT, "reports", "contributors") |
| 10 | +progress_dir = os.path.join(REPO_ROOT, "reports", "progress") |
| 11 | + |
| 12 | + |
| 13 | +def find_latest_image(directory): |
| 14 | + """Find the latest image file in the given directory based on filename.""" |
| 15 | + pattern = os.path.join(directory, "*.png") |
| 16 | + files = glob.glob(pattern) |
| 17 | + if not files: |
| 18 | + return None |
| 19 | + # Sort the files by name (which should sort by date if the format is consistent) |
| 20 | + latest_file = sorted(files)[-1] |
| 21 | + # Return the path relative to the REPO_ROOT |
| 22 | + return os.path.relpath(latest_file, REPO_ROOT).replace("\\", "/") |
| 23 | + |
| 24 | + |
| 25 | +def update_readme(): |
| 26 | + """Update the README.md file with the latest report images.""" |
| 27 | + today = datetime.now().strftime("%Y-%m-%d") |
| 28 | + latest_contributors_img = find_latest_image(contributors_dir) |
| 29 | + latest_progress_img = find_latest_image(progress_dir) |
| 30 | + |
| 31 | + # Check if both image files exist |
| 32 | + if not latest_contributors_img or not latest_progress_img: |
| 33 | + print("Warning: One or both image files not found.") |
| 34 | + if latest_contributors_img: |
| 35 | + print(f"Found contributors image: {latest_contributors_img}") |
| 36 | + if latest_progress_img: |
| 37 | + print(f"Found progress image: {latest_progress_img}") |
| 38 | + return False |
| 39 | + |
| 40 | + # Read the current README content |
| 41 | + with open(README_PATH, "r", encoding="utf-8") as file: |
| 42 | + content = file.read() |
| 43 | + |
| 44 | + # Define the stats section |
| 45 | + stats_section = f"""## آمار ترجمه |
| 46 | +
|
| 47 | +### مشارکتهای این هفته کاربران |
| 48 | +
|
| 49 | + |
| 50 | +
|
| 51 | +(بهروزرسانی: {today}) |
| 52 | +
|
| 53 | +### پیشرفت ترجمه |
| 54 | +
|
| 55 | + |
| 56 | +
|
| 57 | +(بهروزرسانی: {today})""" |
| 58 | + |
| 59 | + # Check if stats section already exists with markers |
| 60 | + if "<!-- STATS_START -->" in content and "<!-- STATS_END -->" in content: |
| 61 | + # Replace the existing stats section between markers |
| 62 | + pattern = r"<!-- STATS_START -->.*?<!-- STATS_END -->" |
| 63 | + updated_content = re.sub( |
| 64 | + pattern, |
| 65 | + f"<!-- STATS_START -->\n{stats_section}\n<!-- STATS_END -->", |
| 66 | + content, |
| 67 | + flags=re.DOTALL, |
| 68 | + ) |
| 69 | + elif "## آمار ترجمه" in content: |
| 70 | + # If old format exists without markers, replace that section |
| 71 | + pattern = r"## آمار ترجمه.*?(?=\n##|\Z)" |
| 72 | + updated_content = re.sub( |
| 73 | + pattern, |
| 74 | + f"<!-- STATS_START -->\n{stats_section}\n<!-- STATS_END -->", |
| 75 | + content, |
| 76 | + flags=re.DOTALL, |
| 77 | + ) |
| 78 | + else: |
| 79 | + # Append the stats section to the end of the README |
| 80 | + updated_content = ( |
| 81 | + content |
| 82 | + + "\n\n<!-- STATS_START -->\n" |
| 83 | + + stats_section |
| 84 | + + "\n<!-- STATS_END -->" |
| 85 | + ) |
| 86 | + |
| 87 | + # Write the updated content back to the README |
| 88 | + with open(README_PATH, "w", encoding="utf-8") as file: |
| 89 | + file.write(updated_content) |
| 90 | + |
| 91 | + print(f"README.md updated successfully with:") |
| 92 | + print(f"- Contributors image: {latest_contributors_img}") |
| 93 | + print(f"- Progress image: {latest_progress_img}") |
| 94 | + print(f"- Update date: {today}") |
| 95 | + return True |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + update_readme() |
0 commit comments