#!/usr/bin/env python3 r"""Aggregate UPDATES.md files below an explicitly selected directory into local HTML.""" from __future__ import annotations import argparse import html import os from datetime import datetime from pathlib import Path DEFAULT_OUTPUT = Path(__file__).resolve().parent.parent / "output/updates-aggregate.html" IGNORED_DIRS = {".git", ".hg", ".svn", ".venv", "node_modules"} def find_update_files(folder: Path) -> list[Path]: """Return the folder's UPDATES.md files in alphabetical path order.""" matches: list[Path] = [] for current_dir, dir_names, file_names in os.walk(folder): dir_names[:] = sorted( (name for name in dir_names if name not in IGNORED_DIRS), key=str.casefold, ) if "UPDATES.md" in file_names: matches.append(Path(current_dir) / "UPDATES.md") return sorted(matches, key=lambda path: str(path).casefold()) def build_html(projects_dir: Path) -> str: folders = sorted( (path for path in projects_dir.iterdir() if path.is_dir()), key=lambda path: path.name.casefold(), ) sections: list[str] = [] nav_items: list[str] = [] total_files = 0 for index, folder in enumerate(folders): update_files = find_update_files(folder) total_files += len(update_files) anchor = f"folder-{index}" background_class = " red-background" if index % 2 else "" nav_items.append( f'
  • {html.escape(folder.name)}' f" ({len(update_files)})
  • " ) file_blocks: list[str] = [] for update_file in update_files: relative_path = update_file.relative_to(projects_dir) content = update_file.read_text(encoding="utf-8-sig", errors="replace") file_blocks.append( '
    ' f"

    {html.escape(str(relative_path))}

    " f"
    {html.escape(content)}
    " "
    " ) if not file_blocks: file_blocks.append('

    No UPDATES.md files found.

    ') sections.append( f'
    ' f"

    {html.escape(folder.name)}

    " + "".join(file_blocks) + "
    " ) generated = datetime.now().astimezone().strftime("%Y-%m-%d %H:%M %Z") return f""" Project Updates

    Project Updates

    {total_files} UPDATES.md files across {len(folders)} top-level folders.

    Generated {html.escape(generated)} from the configured projects directory.

    {"".join(sections)}
    """ def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--projects-dir", type=Path, required=True, help="Explicit directory to scan; its contents will appear in the local output.", ) parser.add_argument( "--output", type=Path, default=DEFAULT_OUTPUT, help=f"HTML output file (default: {DEFAULT_OUTPUT})", ) args = parser.parse_args() if not args.projects_dir.is_dir(): parser.error(f"Projects directory does not exist: {args.projects_dir}") output = args.output.resolve() output.parent.mkdir(parents=True, exist_ok=True) output.write_text(build_html(args.projects_dir.resolve()), encoding="utf-8") print(f"Wrote {output}") if __name__ == "__main__": main()