Skip to content

Commit

Permalink
FIX: automatically update taplo PyPI version (#26)
Browse files Browse the repository at this point in the history
* BREAK: switch to `pyproject.toml` for hook definition
* MAINT: remove `.version` file
  • Loading branch information
redeboer authored Aug 26, 2024
1 parent 8481809 commit cc96256
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 20 deletions.
12 changes: 3 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ jobs:
with:
fetch-depth: 0
- uses: actions/setup-python@v5
- run: pip install pre-commit-mirror-maker
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/install.sh | sh
- run: git config user.name 'Github Actions'
- run: git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
- run: |
pre-commit-mirror . \
--description="A TOML toolkit written in Rust" \
--entry="taplo format" \
--id=taplo \
--language=python \
--package-name=taplo \
--types=toml
- run: uv run --no-project .github/workflows/update.py
- run: |
git remote set-url origin https://x-access-token:${{ secrets.PAT }}@github.com/${{ github.repository }}
for tag in $(git tag); do
Expand Down
100 changes: 100 additions & 0 deletions .github/workflows/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Run with https://docs.astral.sh/uv/guides/scripts:
.. code-block:: shell
uv run --no-project .github/workflows/update.py
"""
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "packaging",
# "tomlkit",
# ]
# ///

import json
import subprocess
import urllib.request
from pathlib import Path

import tomlkit
from packaging import requirements, version

REPO_DIR = Path(__file__).parent.parent.parent.absolute()


def main() -> int:
git_tags = set(get_existing_tags())
pypi_versions = set(get_package_versions("taplo"))
pypi_tags = {f"v{t}" for t in pypi_versions}
missing_tags = pypi_tags - git_tags
if not missing_tags:
print("No new versions found")
return 0
for tag in sorted(missing_tags):
print(f"Updating to {tag}")
update_files(tag)
stage_commit_and_tag(tag)
return 0


def get_existing_tags() -> list[str]:
tags = git("tag", "--list").splitlines()
return sorted(t for t in tags if t.startswith("v"))


def get_package_versions(package_name: str) -> list[str]:
# https://github.com/pre-commit/pre-commit-mirror-maker/blob/6d69b0e/pre_commit_mirror_maker/languages.py#L23-L27
pypi_name = requirements.Requirement(package_name).name
url = f"https://pypi.org/pypi/{pypi_name}/json"
response = json.load(urllib.request.urlopen(url))
return sorted(response["releases"], key=lambda k: version.parse(k))


def update_files(new_tag: str) -> None:
with open(REPO_DIR / "pyproject.toml") as f:
pyproject = tomlkit.parse(f.read())
current_version = _get_current_taplo_version(pyproject)
new_version = _to_version(new_tag)
_replace_in_pyproject(pyproject, new_version)
_replace_in_readme(
current_tag=f"v{current_version}",
new_tag=new_tag,
)


def _get_current_taplo_version(pyproject: tomlkit.TOMLDocument) -> str:
dependencies: list[str] = pyproject["project"]["dependencies"]
return dependencies[0].split("==")[1]


def _to_version(tag: str) -> str:
return tag.replace("v", "", 1)


def _replace_in_pyproject(pyproject: tomlkit.TOMLDocument, new_version: str) -> None:
pyproject["project"]["dependencies"] = [f"taplo=={new_version}"]
with open(REPO_DIR / "pyproject.toml", "w") as f:
tomlkit.dump(pyproject, f)


def _replace_in_readme(current_tag: str, new_tag: str) -> None:
with open(REPO_DIR / "README.md") as f:
readme = f.read()
new_readme = readme.replace(current_tag, new_tag)
with open(REPO_DIR / "README.md", "w") as f:
f.write(new_readme)


def stage_commit_and_tag(tag: str) -> None:
git("add", "pyproject.toml", "README.md")
git("commit", "-m", f"MAINT: upgrade to to taplo {tag}")
git("tag", tag)


def git(*cmd: str) -> str:
output = subprocess.check_output(("git", "-C", REPO_DIR) + cmd)
return output.decode()


if __name__ == "__main__":
raise SystemExit(main())
1 change: 0 additions & 1 deletion .version

This file was deleted.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
dependencies = ["taplo==0.9.3"]
name = "pre_commit_placeholder_package"
requires-python = ">=3.8"
version = "0.0.0"
10 changes: 0 additions & 10 deletions setup.py

This file was deleted.

0 comments on commit cc96256

Please sign in to comment.