Files
revng-revng/python/scripts/revng
T
Giacomo Vercesi 62c1b44a54 revng: allow execution while in a venv
If a venv is running, the `revng` script should re-exec to allow the
correct modules to be loaded and allow running the `main` function of
the respective modules.
2026-01-29 14:54:24 +01:00

36 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python3
#
# This file is distributed under the MIT License. See LICENSE.md for details.
#
import os
import sys
import sysconfig
from pathlib import Path
if sys.prefix != sys.base_prefix:
# In venv, re-exec to the base executable
interpreter_path = os.path.realpath(sys.executable)
os.execv(interpreter_path, [interpreter_path, __file__, *sys.argv[1:]])
libpath = Path(sysconfig.get_path("purelib")).relative_to(sysconfig.get_config_var("base"))
new_path = (Path(__file__) / "../.." / libpath).resolve()
assert new_path.is_dir()
if str(new_path) not in sys.path:
if "PYTHONPATH" in os.environ:
os.environ["PYTHONPATH"] = f"{new_path!s}:{os.environ['PYTHONPATH']}"
else:
os.environ["PYTHONPATH"] = str(new_path)
sys.path.insert(0, str(new_path))
if __name__ == "__main__":
name = os.path.basename(sys.argv[0])
assert name in ("revng", "revng2")
if name == "revng":
from revng.internal.cli.revng import main
else:
from revng.internal.cli.revng2 import main
sys.exit(main())