Skip to content

Files

Latest commit

969a25d · Oct 17, 2024

History

History

docker_python_nodejs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Aug 21, 2024
Oct 8, 2024
Oct 17, 2024
Aug 21, 2024
Aug 21, 2024
Aug 21, 2024
Aug 21, 2024
Oct 8, 2024
Aug 21, 2024
Oct 8, 2024
import logging
import re
from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from .versions import BuildVersion, SupportedVersion

logger = logging.getLogger("dpn")


def _format_md_table(columns: list[str], rows: list[list[str]]) -> str:
    head = f"{' | '.join(columns)}\n{' | '.join(['---'] * len(columns))}"
    body = "\n".join([" | ".join(row) for row in rows])
    return f"{head}\n{body}\n"


def _replace(name: str, replacement: str, document: str) -> str:
    start = f"<!-- {name}_START -->\n"
    end = f"<!-- {name}_END -->"
    repl = f"{start}\n{replacement}\n{end}"
    return re.sub(f"{start}(.*?){end}", repl, document, flags=re.MULTILINE | re.DOTALL)


def update_dynamic_readme(
    versions: "list[BuildVersion]",
    python_versions: "list[SupportedVersion]",
    node_versions: "list[SupportedVersion]",
    dry_run: bool = False,
) -> None:
    """Read out current README, format fresh README, write back possible changes"""
    readme_path = Path("README.md")
    readme = readme_path.read_text()

    readme_new = format_readme(versions, python_versions, node_versions, readme)
    if readme == readme_new:
        logger.debug("Regenerated readme matches existing")
        return

    if not dry_run:
        readme_path.write_text(readme_new)
    else:
        print(readme_new)


def format_readme(
    versions: "list[BuildVersion]",
    python_versions: "list[SupportedVersion]",
    node_versions: "list[SupportedVersion]",
    readme: str,
) -> str:
    """Format fresh README based on passed in version. Replaces the whole table with new versions."""
    tags_table = format_tags(versions)
    readme_fresh = _replace("TAGS", tags_table, readme)

    supported_versions_table = format_supported_versions(python_versions, node_versions)
    return _replace("SUPPORTED_VERSIONS", supported_versions_table, readme_fresh)


def format_tags(versions: "list[BuildVersion]") -> str:
    headings = ["Tag", "Python version", "Node.js version", "Distro"]
    rows = [[f"`{v.key}`", v.python_canonical, v.nodejs_canonical, v.distro] for v in versions]
    return _format_md_table(headings, rows)


def format_supported_versions(
    python_versions: "list[SupportedVersion]",
    node_versions: "list[SupportedVersion]",
) -> str:
    def _as_rows(versions: "list[SupportedVersion]") -> list[list[str]]:
        return [[ver.version, ver.start, ver.end] for ver in sorted(versions, key=lambda x: x.start, reverse=True)]

    python_table = _format_md_table(["Python version", "Start", "End"], _as_rows(python_versions))
    node_table = _format_md_table(["Node.js version", "Start", "End"], _as_rows(node_versions))

    return f"{python_table}\n{node_table}"