#!/usr/bin/env python3 """Explicit software test profiles for live repo / compact detached / reference regen. Profiles (distinct from packaging handoff profiles): - ``live-repository``: full checkout with raw ``.git`` and reference originals. - ``review-compact-detached``: extracted review-compact tree (no BRD/PDF/legacy_rtl). - ``reference-regeneration``: local BRD regeneration / inventory refresh path. Override with ``PRODU2_TEST_PROFILE``. When unset, infer from git mode + tree shape. """ from __future__ import annotations import argparse import json import os import sys from pathlib import Path from typing import Any TOOLS = Path(__file__).resolve().parent ROOT = TOOLS.parent if str(TOOLS) not in sys.path: sys.path.insert(0, str(TOOLS)) from git_worktree_mode import ( # noqa: E402 DETACHED, LIVE_GIT, SNAPSHOT, detect_git_mode, ) LIVE_REPOSITORY = "live-repository" REVIEW_COMPACT_DETACHED = "review-compact-detached" REFERENCE_REGENERATION = "reference-regeneration" TEST_PROFILES = frozenset( {LIVE_REPOSITORY, REVIEW_COMPACT_DETACHED, REFERENCE_REGENERATION} ) ENV_TEST_PROFILE = "PRODU2_TEST_PROFILE" # Paths that live only in the full repository / reference volume — never required # under review-compact-detached. REFERENCE_ONLY_PATHS = ( "reference/original/legacy_rtl/prod2u_p4_normal_v3_top_legacy.v", "reference/original/legacy_rtl/prod2u_p4_normal_v3_top_legacy_tb.v", ) def normalize_profile(value: str | None) -> str | None: if value is None: return None text = value.strip().casefold().replace("_", "-") aliases = { "live": LIVE_REPOSITORY, "live-repo": LIVE_REPOSITORY, "live-repository": LIVE_REPOSITORY, "compact": REVIEW_COMPACT_DETACHED, "detached": REVIEW_COMPACT_DETACHED, "review-compact": REVIEW_COMPACT_DETACHED, "review-compact-detached": REVIEW_COMPACT_DETACHED, "reference": REFERENCE_REGENERATION, "reference-regen": REFERENCE_REGENERATION, "reference-regeneration": REFERENCE_REGENERATION, } return aliases.get(text) def detect_test_profile(root: Path | None = None) -> str: """Return active test profile from env or tree shape.""" override = normalize_profile(os.environ.get(ENV_TEST_PROFILE)) if override is not None: return override base = (root or ROOT).resolve() # Explicit regeneration requests (BRD present + inventory tooling) win only # when env asks; otherwise prefer git/compact shape. git_mode = detect_git_mode(base) if git_mode in {DETACHED, SNAPSHOT}: return REVIEW_COMPACT_DETACHED if git_mode == LIVE_GIT: return LIVE_REPOSITORY # Fallback: compact-shaped trees without .git. if (base / "source").is_dir() and (base / "meta").is_dir(): return REVIEW_COMPACT_DETACHED return LIVE_REPOSITORY def requires_reference_legacy_rtl(profile: str | None = None) -> bool: """legacy_rtl is excluded from review-compact; only live/reference need it.""" active = profile or detect_test_profile() return active in {LIVE_REPOSITORY, REFERENCE_REGENERATION} def allows_reference_regeneration(profile: str | None = None) -> bool: active = profile or detect_test_profile() return active == REFERENCE_REGENERATION def is_compact_detached(profile: str | None = None) -> bool: active = profile or detect_test_profile() return active == REVIEW_COMPACT_DETACHED def structure_required_paths(profile: str | None = None) -> tuple[str, ...]: """Canonical files that structure tests must find for ``profile``.""" common = ( "projects/prod2u-door-temp/project.json", "projects/prod2u-door-temp/requirements.zh-CN.md", "schemas/project.schema.json", "src/common/frame_rx.v", "src/common/frame_tx.v", "src/common/crc16_serial.v", "src/modules/door_motor_engine.v", "src/modules/temperature_reader.v", "src/project/project_adapter.v", "src/top/prod2u_p4_normal_v3_top.v", "src/generated/prod2u_door_temp_params.vh", "tests/sim/prod2u_p4_normal_v3_top_tb.v", "td/prod2u_p4_normal_v3.prj", "td/syn/prod2u_p4_normal_v3.tcl", "td/phy/prod2u_p4_normal_v3.tcl", "td/bitgen/prod2u_p4_normal_v3.tcl", "constraints/prod2u_p4_normal_v3.adc", "constraints/prod2u_p4_normal_v3.sdc", "tools/run_verification_matrix.py", ) active = profile or detect_test_profile() if requires_reference_legacy_rtl(active): return common + REFERENCE_ONLY_PATHS return common def describe_test_profile(root: Path | None = None) -> dict[str, Any]: base = (root or ROOT).resolve() profile = detect_test_profile(base) return { "schema": "cpld-test-profile-v1", "profile": profile, "env_override": os.environ.get(ENV_TEST_PROFILE) or None, "git_mode": detect_git_mode(base), "requires_reference_legacy_rtl": requires_reference_legacy_rtl(profile), "allows_reference_regeneration": allows_reference_regeneration(profile), "is_compact_detached": is_compact_detached(profile), "root": str(base), } def build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--root", type=Path, default=ROOT, help="repository or extracted CPLD/source root", ) parser.add_argument( "--json", action="store_true", help="print profile description JSON", ) return parser def main(argv: list[str] | None = None) -> int: args = build_parser().parse_args(argv) payload = describe_test_profile(args.root) if args.json: print(json.dumps(payload, ensure_ascii=False, indent=2)) else: print(payload["profile"]) return 0 if __name__ == "__main__": raise SystemExit(main())