#!/usr/bin/env python3
"""Reject generated Python cache artifacts from a staged source offer."""

from __future__ import annotations

import pathlib
import sys


FORBIDDEN_SUFFIXES = (".pyc", ".pyo", ".pyd")


def main() -> int:
    if len(sys.argv) != 2:
        print(f"usage: {pathlib.Path(sys.argv[0]).name} <staged-source-root>", file=sys.stderr)
        return 64

    input_root = pathlib.Path(sys.argv[1])
    if not input_root.is_dir() or input_root.is_symlink():
        print(f"source offer audit root is not a regular directory: {input_root}", file=sys.stderr)
        return 1
    root = input_root.resolve()

    for path in root.rglob("*"):
        relative = path.relative_to(root)
        if any(part.casefold() == "__pycache__" for part in relative.parts):
            print(f"generated Python cache in source offer: {relative}", file=sys.stderr)
            return 1
        if path.name.casefold().endswith(FORBIDDEN_SUFFIXES):
            print(f"generated Python bytecode in source offer: {relative}", file=sys.stderr)
            return 1

    return 0


if __name__ == "__main__":
    raise SystemExit(main())
