]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🔨 Add script to generate variants of files (#12405)
authorSebastián Ramírez <tiangolo@gmail.com>
Tue, 8 Oct 2024 11:01:17 +0000 (13:01 +0200)
committerGitHub <noreply@github.com>
Tue, 8 Oct 2024 11:01:17 +0000 (11:01 +0000)
scripts/docs.py

index e5c423d58ab95c2a132dcef2ab68c18588f91ab6..f26f96d8567ef4e45e1996ba13285e41b96556ce 100644 (file)
@@ -15,6 +15,7 @@ import mkdocs.utils
 import typer
 import yaml
 from jinja2 import Template
+from ruff.__main__ import find_ruff_bin
 
 logging.basicConfig(level=logging.INFO)
 
@@ -382,5 +383,41 @@ def langs_json():
     print(json.dumps(langs))
 
 
+@app.command()
+def generate_docs_src_versions_for_file(file_path: Path) -> None:
+    target_versions = ["py39", "py310"]
+    base_content = file_path.read_text(encoding="utf-8")
+    previous_content = {base_content}
+    for target_version in target_versions:
+        version_result = subprocess.run(
+            [
+                find_ruff_bin(),
+                "check",
+                "--target-version",
+                target_version,
+                "--fix",
+                "--unsafe-fixes",
+                "-",
+            ],
+            input=base_content.encode("utf-8"),
+            capture_output=True,
+        )
+        content_target = version_result.stdout.decode("utf-8")
+        format_result = subprocess.run(
+            [find_ruff_bin(), "format", "-"],
+            input=content_target.encode("utf-8"),
+            capture_output=True,
+        )
+        content_format = format_result.stdout.decode("utf-8")
+        if content_format in previous_content:
+            continue
+        previous_content.add(content_format)
+        version_file = file_path.with_name(
+            file_path.name.replace(".py", f"_{target_version}.py")
+        )
+        logging.info(f"Writing to {version_file}")
+        version_file.write_text(content_format, encoding="utf-8")
+
+
 if __name__ == "__main__":
     app()