Skip to content

Instantly share code, notes, and snippets.

@mkpoli
Created November 2, 2024 16:04
Show Gist options
  • Select an option

  • Save mkpoli/e4811538a17dee96df0e056e55f699d9 to your computer and use it in GitHub Desktop.

Select an option

Save mkpoli/e4811538a17dee96df0e056e55f699d9 to your computer and use it in GitHub Desktop.
Update CHANGELOG [Unreleased] to version script
import difflib
import tomllib
import sys
from pathlib import Path
from datetime import datetime
project_file = Path("pyproject.toml")
with open(project_file, "rb") as file:
pyproject = tomllib.load(file)
CHANGELOG = Path("CHANGELOG.md")
# Replace Unreleased with current version number
def main():
current_version = pyproject.get("project", {}).get("version")
if not current_version:
sys.exit("No version found in pyproject.toml")
with CHANGELOG.open() as file:
content = file.read()
old_content = content
content = content.replace(
"## [Unreleased]",
f"## [{current_version}] - {datetime.now().strftime('%Y-%m-%d')}",
)
# print diff
diff = difflib.unified_diff(
old_content.splitlines(), content.splitlines(), lineterm=""
)
print("\n".join(diff))
with CHANGELOG.open("w") as file:
file.write(content)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment