Source code for codegen.output
import fnmatch
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterable, Sequence
from pathlib import Path
from codegen.spec import GeneratedFile
[docs]
def write_files(
files: Sequence[GeneratedFile],
out_dir: Path,
*,
force: bool = False,
force_paths: Iterable[str] | None = None,
) -> int:
forced: tuple[str, ...] = tuple(force_paths or ())
written = 0
def _is_forced(path: str) -> bool:
return any(fnmatch.fnmatch(path, pattern) for pattern in forced)
for f in files:
target = out_dir / f.path
should_overwrite = (
f.if_exists == "overwrite" or force or _is_forced(f.path)
)
if not should_overwrite and target.exists():
continue
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(f.content)
if f.executable:
mode = target.stat().st_mode | 0o111
target.chmod(mode)
written += 1
return written