parse-changelog.py 1005 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env python3
  2. from pathlib import Path
  3. from argparse import ArgumentParser
  4. def parse_latest_changelog(text: str) -> str:
  5. """Read the contents between the first `##` and the second `##`"""
  6. recording = False
  7. contents: list[str] = []
  8. for line in text.splitlines():
  9. if line.strip().startswith("## ") and recording is False:
  10. recording = True
  11. elif line.strip().startswith("## ") and recording is True:
  12. break
  13. if recording:
  14. contents.append(line)
  15. return "\n".join(contents[1:])
  16. def get_changelog() -> str:
  17. parser = ArgumentParser(description="Get the latest change log from CHANG_LOGS.md")
  18. parser.add_argument("changelog_file", help="The path of CHANGE_LOGS.md")
  19. args = parser.parse_args()
  20. with Path(args.changelog_file).open() as f:
  21. return f.read()
  22. if __name__ == '__main__':
  23. changelog = get_changelog()
  24. latest_changelog = parse_latest_changelog(changelog)
  25. print(latest_changelog)