diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 91fc823..b5d331b 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -25,6 +25,9 @@ jobs: python -m pip install poetry poetry config virtualenvs.in-project true poetry install + + - name: Prepare README + run: ./scripts/refactor_readme.py README.md - name: Build package run: poetry build diff --git a/pyproject.toml b/pyproject.toml index dbc0365..9336b62 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,7 @@ ignore = [ "D105", # docstrings in magic methods "S101", # assert statements + "S603", # false-positive subprocess call (https://github.com/astral-sh/ruff/issues/4045) "PLR2004", # "magic" values >.> "FBT", # boolean "traps" @@ -61,6 +62,10 @@ line-length = 100 "D", # documentation "INP001", # namespacing ] +"scripts/*" = [ + "T201", # use of "print" + "D", # documentation +] [build-system] requires = ["poetry-core"] diff --git a/scripts/refactor_readme.py b/scripts/refactor_readme.py new file mode 100755 index 0000000..ea6ac4d --- /dev/null +++ b/scripts/refactor_readme.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +"""Script to resolve relative URLs in README prior to release.""" + +from __future__ import annotations + +import re +import subprocess +import sys +from pathlib import Path + + +def main(args: list[str]) -> int: + if len(args) < 1: + print("No README path supplied.") + return 1 + + remote_url = ( + subprocess.run( + ["/usr/bin/env", "git", "remote", "get-url", "origin"], + check=True, + capture_output=True, + ) + .stdout.decode("utf-8") + .strip() + ) + + # Convert SSH remote URLs to HTTPS + remote_url = re.sub(r"^ssh://git@", "https://", remote_url) + + readme_path = Path(args[0]) + readme_content = readme_path.read_text("utf-8") + + new_content = re.sub( + r"(\[[^]]+]\()((?!https?:)[^)]+)(\))", + lambda m: m.group(1) + remote_url + "/blob/main/" + m.group(2) + m.group(3), + readme_content, + ) + + readme_path.write_text(new_content) + + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:]))