#!/usr/bin/env -S uv run --script --quiet # /// script # requires-python = ">=3.12" # /// """ Update the README.md file with a list of all the scripts in the current directory. Usage: ```bash ./update-readme ``` """ import ast import os import stat from pathlib import Path def get_docstring(path: Path) -> str: try: with open(path) as f: tree = ast.parse(f.read()) if not tree.body: return "" if isinstance(tree.body[0], ast.Expr) and isinstance( tree.body[0].value, ast.Constant ): return str(tree.body[0].value.value).strip() return "" except Exception: return "" def get_scripts() -> list[tuple[Path, str]]: scripts = [] for path in Path(".").iterdir(): if ( path.is_file() and path.suffix in {".py", ""} and not path.name.startswith(".") and is_executable(path) ): doc = get_docstring(path) scripts.append((path, doc)) return sorted(scripts) def is_executable(path: Path) -> bool: # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html#tag_13_27_03_04 return bool(os.stat(path).st_mode & stat.S_IXUSR) def update_readme() -> None: readme = Path("README.md") content = readme.read_text() parts = content.split("## scripts", 1) base_content = parts[0].strip() scripts = get_scripts() script_list = "\n\n## scripts\n\n" for script, _ in scripts: script_list += f"- [`{script.name}`](#{script.name})\n" script_list += "\n---\n\n" for i, (script, doc) in enumerate(scripts): script_list += f"### `{script.name}`\n\n{doc or 'no description'}\n\n" if i < len(scripts) - 1: script_list += "---\n\n" new_content = base_content + script_list if new_content != content: readme.write_text(new_content) if __name__ == "__main__": update_readme()