test_profiles.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. """Explicit software test profiles for live repo / compact detached / reference regen.
  3. Profiles (distinct from packaging handoff profiles):
  4. - ``live-repository``: full checkout with raw ``.git`` and reference originals.
  5. - ``review-compact-detached``: extracted review-compact tree (no BRD/PDF/legacy_rtl).
  6. - ``reference-regeneration``: local BRD regeneration / inventory refresh path.
  7. Override with ``PRODU2_TEST_PROFILE``. When unset, infer from git mode + tree shape.
  8. """
  9. from __future__ import annotations
  10. import argparse
  11. import json
  12. import os
  13. import sys
  14. from pathlib import Path
  15. from typing import Any
  16. TOOLS = Path(__file__).resolve().parent
  17. ROOT = TOOLS.parent
  18. if str(TOOLS) not in sys.path:
  19. sys.path.insert(0, str(TOOLS))
  20. from git_worktree_mode import ( # noqa: E402
  21. DETACHED,
  22. LIVE_GIT,
  23. SNAPSHOT,
  24. detect_git_mode,
  25. )
  26. LIVE_REPOSITORY = "live-repository"
  27. REVIEW_COMPACT_DETACHED = "review-compact-detached"
  28. REFERENCE_REGENERATION = "reference-regeneration"
  29. TEST_PROFILES = frozenset(
  30. {LIVE_REPOSITORY, REVIEW_COMPACT_DETACHED, REFERENCE_REGENERATION}
  31. )
  32. ENV_TEST_PROFILE = "PRODU2_TEST_PROFILE"
  33. # Paths that live only in the full repository / reference volume — never required
  34. # under review-compact-detached.
  35. REFERENCE_ONLY_PATHS = (
  36. "reference/original/legacy_rtl/prod2u_p4_normal_v3_top_legacy.v",
  37. "reference/original/legacy_rtl/prod2u_p4_normal_v3_top_legacy_tb.v",
  38. )
  39. def normalize_profile(value: str | None) -> str | None:
  40. if value is None:
  41. return None
  42. text = value.strip().casefold().replace("_", "-")
  43. aliases = {
  44. "live": LIVE_REPOSITORY,
  45. "live-repo": LIVE_REPOSITORY,
  46. "live-repository": LIVE_REPOSITORY,
  47. "compact": REVIEW_COMPACT_DETACHED,
  48. "detached": REVIEW_COMPACT_DETACHED,
  49. "review-compact": REVIEW_COMPACT_DETACHED,
  50. "review-compact-detached": REVIEW_COMPACT_DETACHED,
  51. "reference": REFERENCE_REGENERATION,
  52. "reference-regen": REFERENCE_REGENERATION,
  53. "reference-regeneration": REFERENCE_REGENERATION,
  54. }
  55. return aliases.get(text)
  56. def detect_test_profile(root: Path | None = None) -> str:
  57. """Return active test profile from env or tree shape."""
  58. override = normalize_profile(os.environ.get(ENV_TEST_PROFILE))
  59. if override is not None:
  60. return override
  61. base = (root or ROOT).resolve()
  62. # Explicit regeneration requests (BRD present + inventory tooling) win only
  63. # when env asks; otherwise prefer git/compact shape.
  64. git_mode = detect_git_mode(base)
  65. if git_mode in {DETACHED, SNAPSHOT}:
  66. return REVIEW_COMPACT_DETACHED
  67. if git_mode == LIVE_GIT:
  68. return LIVE_REPOSITORY
  69. # Fallback: compact-shaped trees without .git.
  70. if (base / "source").is_dir() and (base / "meta").is_dir():
  71. return REVIEW_COMPACT_DETACHED
  72. return LIVE_REPOSITORY
  73. def requires_reference_legacy_rtl(profile: str | None = None) -> bool:
  74. """legacy_rtl is excluded from review-compact; only live/reference need it."""
  75. active = profile or detect_test_profile()
  76. return active in {LIVE_REPOSITORY, REFERENCE_REGENERATION}
  77. def allows_reference_regeneration(profile: str | None = None) -> bool:
  78. active = profile or detect_test_profile()
  79. return active == REFERENCE_REGENERATION
  80. def is_compact_detached(profile: str | None = None) -> bool:
  81. active = profile or detect_test_profile()
  82. return active == REVIEW_COMPACT_DETACHED
  83. def structure_required_paths(profile: str | None = None) -> tuple[str, ...]:
  84. """Canonical files that structure tests must find for ``profile``."""
  85. common = (
  86. "projects/prod2u-door-temp/project.json",
  87. "projects/prod2u-door-temp/requirements.zh-CN.md",
  88. "schemas/project.schema.json",
  89. "src/common/frame_rx.v",
  90. "src/common/frame_tx.v",
  91. "src/common/crc16_serial.v",
  92. "src/modules/door_motor_engine.v",
  93. "src/modules/temperature_reader.v",
  94. "src/project/project_adapter.v",
  95. "src/top/prod2u_p4_normal_v3_top.v",
  96. "src/generated/prod2u_door_temp_params.vh",
  97. "tests/sim/prod2u_p4_normal_v3_top_tb.v",
  98. "td/prod2u_p4_normal_v3.prj",
  99. "td/syn/prod2u_p4_normal_v3.tcl",
  100. "td/phy/prod2u_p4_normal_v3.tcl",
  101. "td/bitgen/prod2u_p4_normal_v3.tcl",
  102. "constraints/prod2u_p4_normal_v3.adc",
  103. "constraints/prod2u_p4_normal_v3.sdc",
  104. "tools/run_verification_matrix.py",
  105. )
  106. active = profile or detect_test_profile()
  107. if requires_reference_legacy_rtl(active):
  108. return common + REFERENCE_ONLY_PATHS
  109. return common
  110. def describe_test_profile(root: Path | None = None) -> dict[str, Any]:
  111. base = (root or ROOT).resolve()
  112. profile = detect_test_profile(base)
  113. return {
  114. "schema": "cpld-test-profile-v1",
  115. "profile": profile,
  116. "env_override": os.environ.get(ENV_TEST_PROFILE) or None,
  117. "git_mode": detect_git_mode(base),
  118. "requires_reference_legacy_rtl": requires_reference_legacy_rtl(profile),
  119. "allows_reference_regeneration": allows_reference_regeneration(profile),
  120. "is_compact_detached": is_compact_detached(profile),
  121. "root": str(base),
  122. }
  123. def build_parser() -> argparse.ArgumentParser:
  124. parser = argparse.ArgumentParser(description=__doc__)
  125. parser.add_argument(
  126. "--root",
  127. type=Path,
  128. default=ROOT,
  129. help="repository or extracted CPLD/source root",
  130. )
  131. parser.add_argument(
  132. "--json",
  133. action="store_true",
  134. help="print profile description JSON",
  135. )
  136. return parser
  137. def main(argv: list[str] | None = None) -> int:
  138. args = build_parser().parse_args(argv)
  139. payload = describe_test_profile(args.root)
  140. if args.json:
  141. print(json.dumps(payload, ensure_ascii=False, indent=2))
  142. else:
  143. print(payload["profile"])
  144. return 0
  145. if __name__ == "__main__":
  146. raise SystemExit(main())