Files
ruff/scripts/transform_readme.py
Micha ReiserandAlex Waygood 0222c19dd8 Share ty configuration for Ruff’s standalone scripts (#30463)
## Summary

Use a shared ty override for Ruff’s standalone scripts now that PEP 723
scripts can inherit project settings. Fix the diagnostics in the two PR
automation scripts so they can use the same checks.

## Notes to reviewers

All 28 standalone scripts passed ty with locked dependencies, and the
changed-file hooks and mocked workflow checks passed.

---------

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2026-11-16 17:35:08 +00:00

90 lines
2.8 KiB
Python

# /// script
# requires-python = ">=3.12"
# dependencies = []
#
# [tool.uv]
# no-build = true
# exclude-newer = "P7D"
# require-build-hashes = true
# preview-features = ["build-dependency-hashes", "missing-exclude-newer-package-lock"]
# ///
"""Transform the README.md to support a specific deployment target.
By default, we assume that our README.md will be rendered on GitHub. However, different
targets have different strategies for rendering light- and dark-mode images. This script
adjusts the images in the README.md to support the given target.
"""
from __future__ import annotations
import argparse
from pathlib import Path
URL = "https://user-images.githubusercontent.com/1309177/{}.svg"
URL_LIGHT = URL.format("232603516-4fb4892d-585c-4b20-b810-3db9161831e4")
URL_DARK = URL.format("232603514-c95e9b0f-6b31-43de-9a80-9e844173fd6a")
# https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#specifying-the-theme-an-image-is-shown-to
GITHUB = f"""
<p align="center">
<picture align="center">
<source media="(prefers-color-scheme: dark)" srcset="{URL_DARK}">
<source media="(prefers-color-scheme: light)" srcset="{URL_LIGHT}">
<img alt="Shows a bar chart with benchmark results." src="{URL_LIGHT}">
</picture>
</p>
"""
# https://github.com/pypi/warehouse/issues/11251
PYPI = f"""
<p align="center">
<img alt="Shows a bar chart with benchmark results." src="{URL_LIGHT}">
</p>
"""
# https://squidfunk.github.io/mkdocs-material/reference/images/#light-and-dark-mode
MK_DOCS = f"""
<p align="center">
<img alt="Shows a bar chart with benchmark results." src="{URL_LIGHT}#only-light">
</p>
<p align="center">
<img alt="Shows a bar chart with benchmark results." src="{URL_DARK}#only-dark">
</p>
"""
def main(target: str) -> None:
"""Modify the README.md to support the given target."""
with Path("README.md").open(encoding="utf8") as fp:
content = fp.read()
if GITHUB not in content:
msg = "README.md is not in the expected format."
raise ValueError(msg)
if target == "pypi":
with Path("README.md").open("w", encoding="utf8") as fp:
fp.write(content.replace(GITHUB, PYPI))
elif target == "mkdocs":
with Path("README.md").open("w", encoding="utf8") as fp:
fp.write(content.replace(GITHUB, MK_DOCS))
else:
msg = f"Unknown target: {target}"
raise ValueError(msg)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Modify the README.md to support a specific deployment target.",
)
parser.add_argument(
"--target",
type=str,
required=True,
choices=("pypi", "mkdocs"),
)
args = parser.parse_args()
main(target=args.target)