tool_env.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """LD-safe Python / env helpers for Prod2U verify lanes.
  2. OSS CAD Suite's real interpreter lives at ``libexec/python3.11`` and needs
  3. ``libpython`` from ``$OSS/lib``. The durable launcher is ``py3bin/python3.11``
  4. (or the verify venv symlink to it), which invokes ld-linux with an explicit
  5. ``--library-path`` and must **not** rely on a global ``LD_LIBRARY_PATH``
  6. (TD Anlogic libs must stay out of OSS/Python/cocotb/Verilator).
  7. When a process is started via that wrapper, CPython still reports
  8. ``sys.executable`` as the bare ``libexec`` binary. CLI tests that spawn
  9. ``[sys.executable, ...]`` then exit 127 after poison-strip. Rewrite
  10. ``sys.executable`` (and prefer ``PRODU2_VERIFY_PYTHON``) so children re-enter
  11. the LD-safe wrapper without reintroducing TD library pollution.
  12. """
  13. from __future__ import annotations
  14. import os
  15. import sys
  16. from pathlib import Path
  17. from typing import Mapping, MutableMapping
  18. # Keys that must not leak from Anlogic TD / host into OSS verify children.
  19. POISON_ENV_KEYS: tuple[str, ...] = (
  20. "LD_LIBRARY_PATH",
  21. "LD_PRELOAD",
  22. "QT_PLUGIN_PATH",
  23. "PYTHONHOME",
  24. "PYTHONPATH",
  25. "PYTHONEXECUTABLE",
  26. "PERL5LIB",
  27. "PERL_LOCAL_LIB_ROOT",
  28. "PERL_MB_OPT",
  29. "PERL_MM_OPT",
  30. )
  31. def strip_poison_env(env: MutableMapping[str, str] | None = None) -> MutableMapping[str, str]:
  32. """Remove TD/host poison keys from ``env`` (or ``os.environ``)."""
  33. target: MutableMapping[str, str] = os.environ if env is None else env
  34. for key in POISON_ENV_KEYS:
  35. target.pop(key, None)
  36. return target
  37. def _is_libexec_python(path: Path) -> bool:
  38. return "libexec" in path.as_posix()
  39. def safe_python() -> str:
  40. """Return an LD-safe Python launcher path for verify/CLI subprocesses."""
  41. override = os.environ.get("PRODU2_VERIFY_PYTHON", "").strip()
  42. if override:
  43. candidate = Path(override).expanduser()
  44. if candidate.is_file():
  45. return str(candidate)
  46. exe = Path(sys.executable)
  47. if not _is_libexec_python(exe):
  48. return str(exe)
  49. # libexec/python3.11 -> py3bin/python3.11 (OSS CAD durable wrapper)
  50. sibling = exe.parent.parent / "py3bin" / exe.name
  51. if sibling.is_file():
  52. return str(sibling)
  53. venv = os.environ.get("PRODU2_VERIFY_VENV", "").strip()
  54. if venv:
  55. for name in ("python", "python3"):
  56. candidate = Path(venv).expanduser() / "bin" / name
  57. if candidate.is_file():
  58. return str(candidate)
  59. default_venv = Path("/data/prod2u/venvs/verify/bin/python")
  60. if default_venv.is_file():
  61. return str(default_venv)
  62. default_py3bin = Path("/data/prod2u/oss-cad-suite/latest/py3bin/python3.11")
  63. if default_py3bin.is_file():
  64. return str(default_py3bin)
  65. return str(exe)
  66. def ensure_safe_sys_executable() -> str:
  67. """Point ``sys.executable`` at the LD-safe wrapper when it is bare libexec."""
  68. current = Path(sys.executable)
  69. if not _is_libexec_python(current):
  70. return str(current)
  71. safe = safe_python()
  72. if Path(safe).is_file() and Path(safe).as_posix() != current.as_posix():
  73. sys.executable = safe
  74. return sys.executable
  75. def verify_child_env(extra: Mapping[str, str] | None = None) -> dict[str, str]:
  76. """Env for verify-python children: poison stripped, safe interpreter advertised."""
  77. env = strip_poison_env(os.environ.copy())
  78. env["PRODU2_VERIFY_PYTHON"] = safe_python()
  79. if extra:
  80. env.update(extra)
  81. return env