|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import os |
| 5 | +from datetime import date, datetime |
| 6 | +from pathlib import Path |
| 7 | +from zoneinfo import ZoneInfo |
| 8 | + |
| 9 | + |
| 10 | +README = Path("README.md") |
| 11 | +START_MARKER = "<!-- SPONSOR_START expires=2026-08-28 timezone=Asia/Shanghai -->" |
| 12 | +END_MARKER = "<!-- SPONSOR_END -->" |
| 13 | +EXPIRE_ON = date.fromisoformat(os.environ.get("SPONSOR_EXPIRE_ON", "2026-08-28")) |
| 14 | +TIMEZONE = ZoneInfo(os.environ.get("SPONSOR_TIMEZONE", "Asia/Shanghai")) |
| 15 | + |
| 16 | + |
| 17 | +def current_date() -> date: |
| 18 | + override = os.environ.get("SPONSOR_TODAY") |
| 19 | + if override: |
| 20 | + return date.fromisoformat(override) |
| 21 | + return datetime.now(TIMEZONE).date() |
| 22 | + |
| 23 | + |
| 24 | +def main() -> None: |
| 25 | + today = current_date() |
| 26 | + if today < EXPIRE_ON: |
| 27 | + print(f"Sponsor block is still active until {EXPIRE_ON}; today is {today}.") |
| 28 | + return |
| 29 | + |
| 30 | + text = README.read_text(encoding="utf-8") |
| 31 | + start = text.find(START_MARKER) |
| 32 | + end = text.find(END_MARKER) |
| 33 | + |
| 34 | + if start == -1 and end == -1: |
| 35 | + print("Sponsor block markers are absent; nothing to remove.") |
| 36 | + return |
| 37 | + if start == -1 or end == -1 or end < start: |
| 38 | + raise SystemExit("Sponsor block markers are incomplete or out of order.") |
| 39 | + |
| 40 | + end += len(END_MARKER) |
| 41 | + updated = text[:start].rstrip() + "\n\n" + text[end:].lstrip() |
| 42 | + |
| 43 | + if updated == text: |
| 44 | + print("Sponsor block is already removed.") |
| 45 | + return |
| 46 | + |
| 47 | + README.write_text(updated, encoding="utf-8") |
| 48 | + print(f"Removed sponsor block on {today}.") |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main() |
0 commit comments