| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- """LD-safe Python / env helpers for Prod2U verify lanes.
- OSS CAD Suite's real interpreter lives at ``libexec/python3.11`` and needs
- ``libpython`` from ``$OSS/lib``. The durable launcher is ``py3bin/python3.11``
- (or the verify venv symlink to it), which invokes ld-linux with an explicit
- ``--library-path`` and must **not** rely on a global ``LD_LIBRARY_PATH``
- (TD Anlogic libs must stay out of OSS/Python/cocotb/Verilator).
- When a process is started via that wrapper, CPython still reports
- ``sys.executable`` as the bare ``libexec`` binary. CLI tests that spawn
- ``[sys.executable, ...]`` then exit 127 after poison-strip. Rewrite
- ``sys.executable`` (and prefer ``PRODU2_VERIFY_PYTHON``) so children re-enter
- the LD-safe wrapper without reintroducing TD library pollution.
- """
- from __future__ import annotations
- import os
- import sys
- from pathlib import Path
- from typing import Mapping, MutableMapping
- # Keys that must not leak from Anlogic TD / host into OSS verify children.
- POISON_ENV_KEYS: tuple[str, ...] = (
- "LD_LIBRARY_PATH",
- "LD_PRELOAD",
- "QT_PLUGIN_PATH",
- "PYTHONHOME",
- "PYTHONPATH",
- "PYTHONEXECUTABLE",
- "PERL5LIB",
- "PERL_LOCAL_LIB_ROOT",
- "PERL_MB_OPT",
- "PERL_MM_OPT",
- )
- def strip_poison_env(env: MutableMapping[str, str] | None = None) -> MutableMapping[str, str]:
- """Remove TD/host poison keys from ``env`` (or ``os.environ``)."""
- target: MutableMapping[str, str] = os.environ if env is None else env
- for key in POISON_ENV_KEYS:
- target.pop(key, None)
- return target
- def _is_libexec_python(path: Path) -> bool:
- return "libexec" in path.as_posix()
- def safe_python() -> str:
- """Return an LD-safe Python launcher path for verify/CLI subprocesses."""
- override = os.environ.get("PRODU2_VERIFY_PYTHON", "").strip()
- if override:
- candidate = Path(override).expanduser()
- if candidate.is_file():
- return str(candidate)
- exe = Path(sys.executable)
- if not _is_libexec_python(exe):
- return str(exe)
- # libexec/python3.11 -> py3bin/python3.11 (OSS CAD durable wrapper)
- sibling = exe.parent.parent / "py3bin" / exe.name
- if sibling.is_file():
- return str(sibling)
- venv = os.environ.get("PRODU2_VERIFY_VENV", "").strip()
- if venv:
- for name in ("python", "python3"):
- candidate = Path(venv).expanduser() / "bin" / name
- if candidate.is_file():
- return str(candidate)
- default_venv = Path("/data/prod2u/venvs/verify/bin/python")
- if default_venv.is_file():
- return str(default_venv)
- default_py3bin = Path("/data/prod2u/oss-cad-suite/latest/py3bin/python3.11")
- if default_py3bin.is_file():
- return str(default_py3bin)
- return str(exe)
- def ensure_safe_sys_executable() -> str:
- """Point ``sys.executable`` at the LD-safe wrapper when it is bare libexec."""
- current = Path(sys.executable)
- if not _is_libexec_python(current):
- return str(current)
- safe = safe_python()
- if Path(safe).is_file() and Path(safe).as_posix() != current.as_posix():
- sys.executable = safe
- return sys.executable
- def verify_child_env(extra: Mapping[str, str] | None = None) -> dict[str, str]:
- """Env for verify-python children: poison stripped, safe interpreter advertised."""
- env = strip_poison_env(os.environ.copy())
- env["PRODU2_VERIFY_PYTHON"] = safe_python()
- if extra:
- env.update(extra)
- return env
|