|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 3 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import argparse |
| 8 | +import json |
| 9 | +import shutil |
| 10 | +import subprocess |
| 11 | +import tempfile |
| 12 | +from dataclasses import dataclass |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +@dataclass(frozen=True) |
| 17 | +class Template: |
| 18 | + name: str |
| 19 | + source: Path |
| 20 | + patches: tuple[Path, ...] |
| 21 | + destination: Path |
| 22 | + |
| 23 | + |
| 24 | +def load_templates(manifest_path: Path) -> list[Template]: |
| 25 | + payload = json.loads(manifest_path.read_text(encoding="utf-8")) |
| 26 | + if not isinstance(payload, dict): |
| 27 | + raise ValueError("manifest must be a JSON object") |
| 28 | + |
| 29 | + raw_templates = payload.get("templates") |
| 30 | + if not isinstance(raw_templates, list): |
| 31 | + raise ValueError("manifest.templates must be an array") |
| 32 | + |
| 33 | + templates: list[Template] = [] |
| 34 | + for index, raw in enumerate(raw_templates): |
| 35 | + if not isinstance(raw, dict): |
| 36 | + raise ValueError(f"manifest.templates[{index}] must be an object") |
| 37 | + |
| 38 | + name = _non_empty_string(raw.get("name"), f"templates[{index}].name") |
| 39 | + source = Path(_non_empty_string(raw.get("source"), f"templates[{index}].source")) |
| 40 | + destination = Path( |
| 41 | + _non_empty_string(raw.get("destination"), f"templates[{index}].destination") |
| 42 | + ) |
| 43 | + |
| 44 | + raw_patches = raw.get("patches", []) |
| 45 | + if not isinstance(raw_patches, list) or not all( |
| 46 | + isinstance(item, str) and item for item in raw_patches |
| 47 | + ): |
| 48 | + raise ValueError(f"templates[{index}].patches must be an array of paths") |
| 49 | + |
| 50 | + for path in (source, destination, *(Path(item) for item in raw_patches)): |
| 51 | + _validate_relative_path(path) |
| 52 | + |
| 53 | + templates.append( |
| 54 | + Template( |
| 55 | + name=name, |
| 56 | + source=source, |
| 57 | + patches=tuple(Path(item) for item in raw_patches), |
| 58 | + destination=destination, |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + return templates |
| 63 | + |
| 64 | + |
| 65 | +def render(template: Template, root: Path) -> bytes: |
| 66 | + source = _safe_path(root, template.source) |
| 67 | + if not source.is_file(): |
| 68 | + raise ValueError(f"{template.name}: source does not exist: {template.source}") |
| 69 | + |
| 70 | + with tempfile.TemporaryDirectory() as directory: |
| 71 | + working = Path(directory) / source.name |
| 72 | + shutil.copyfile(source, working) |
| 73 | + |
| 74 | + for patch_path in template.patches: |
| 75 | + patch = _safe_path(root, patch_path) |
| 76 | + if not patch.is_file(): |
| 77 | + raise ValueError(f"{template.name}: patch does not exist: {patch_path}") |
| 78 | + |
| 79 | + result = subprocess.run( |
| 80 | + ["patch", "--batch", "--forward", str(working), str(patch)], |
| 81 | + capture_output=True, |
| 82 | + text=True, |
| 83 | + check=False, |
| 84 | + ) |
| 85 | + if result.returncode != 0: |
| 86 | + details = (result.stderr or result.stdout).strip() |
| 87 | + raise ValueError( |
| 88 | + f"{template.name}: failed to apply {patch_path}: {details}" |
| 89 | + ) |
| 90 | + |
| 91 | + return working.read_bytes() |
| 92 | + |
| 93 | + |
| 94 | +def sync(templates: list[Template], root: Path) -> None: |
| 95 | + for template in templates: |
| 96 | + content = render(template, root) |
| 97 | + destination = _safe_path(root, template.destination) |
| 98 | + destination.parent.mkdir(parents=True, exist_ok=True) |
| 99 | + destination.write_bytes(content) |
| 100 | + |
| 101 | + |
| 102 | +def check(templates: list[Template], root: Path) -> None: |
| 103 | + drift: list[str] = [] |
| 104 | + for template in templates: |
| 105 | + expected = render(template, root) |
| 106 | + destination = _safe_path(root, template.destination) |
| 107 | + if not destination.is_file() or destination.read_bytes() != expected: |
| 108 | + drift.append(template.name) |
| 109 | + |
| 110 | + if drift: |
| 111 | + raise ValueError("rendered templates are out of date: " + ", ".join(drift)) |
| 112 | + |
| 113 | + |
| 114 | +def _validate_relative_path(path: Path) -> None: |
| 115 | + if path.is_absolute() or ".." in path.parts: |
| 116 | + raise ValueError(f"unsafe path: {path}") |
| 117 | + |
| 118 | + |
| 119 | +def _safe_path(root: Path, path: Path) -> Path: |
| 120 | + _validate_relative_path(path) |
| 121 | + resolved = (root / path).resolve() |
| 122 | + root_resolved = root.resolve() |
| 123 | + if resolved != root_resolved and root_resolved not in resolved.parents: |
| 124 | + raise ValueError(f"path escapes repository root: {path}") |
| 125 | + return resolved |
| 126 | + |
| 127 | + |
| 128 | +def _non_empty_string(value: object, path: str) -> str: |
| 129 | + if not isinstance(value, str) or not value: |
| 130 | + raise ValueError(f"{path} must be a non-empty string") |
| 131 | + return value |
| 132 | + |
| 133 | + |
| 134 | +def main() -> int: |
| 135 | + parser = argparse.ArgumentParser() |
| 136 | + parser.add_argument("command", choices=("sync", "check")) |
| 137 | + parser.add_argument("manifest", type=Path) |
| 138 | + args = parser.parse_args() |
| 139 | + |
| 140 | + root = Path.cwd() |
| 141 | + |
| 142 | + try: |
| 143 | + templates = load_templates(args.manifest) |
| 144 | + if args.command == "sync": |
| 145 | + sync(templates, root) |
| 146 | + else: |
| 147 | + check(templates, root) |
| 148 | + except ValueError as error: |
| 149 | + parser.error(str(error)) |
| 150 | + |
| 151 | + return 0 |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + raise SystemExit(main()) |
0 commit comments