-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.py
More file actions
72 lines (59 loc) · 2.22 KB
/
Copy pathbootstrap.py
File metadata and controls
72 lines (59 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Ensure the ``lldb`` Python module is importable, re-exec'ing if needed.
The lldb module only imports under the specific interpreter lldb was built
against (on macOS, Apple's ``/usr/bin/python3``). VSCode may launch this adapter
with any python, so on import failure we locate lldb's module dir via
``lldb -P`` and re-exec under an interpreter that can load it.
"""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
_GUARD = "BTRC_DAP_BOOTSTRAPPED"
_PROBE_TIMEOUT_SECONDS = 15
def _can_run_adapter(python: str, env: dict[str, str]) -> bool:
try:
probe = subprocess.run(
[python, "-c", "import lldb; import adapter"],
env=env,
capture_output=True,
timeout=_PROBE_TIMEOUT_SECONDS,
)
except (OSError, subprocess.TimeoutExpired):
return False
return probe.returncode == 0
def ensure_lldb() -> None:
try:
import lldb # noqa: F401
return
except ImportError:
pass
if os.environ.get(_GUARD):
sys.stderr.write(
"btrc debug adapter: the lldb Python module is unavailable under "
f"{sys.executable}. Install Xcode/llvm command-line tools.\n"
)
sys.exit(1)
lldb_exe = shutil.which("lldb") or "/usr/bin/lldb"
try:
pypath = subprocess.check_output(
[lldb_exe, "-P"],
text=True,
stderr=subprocess.DEVNULL,
timeout=_PROBE_TIMEOUT_SECONDS,
).strip()
except (OSError, subprocess.SubprocessError) as error:
sys.stderr.write(f"btrc debug adapter: cannot locate lldb ({error}).\n")
sys.exit(1)
script = os.path.abspath(sys.argv[0])
script_dir = os.path.dirname(script)
for py in ("/usr/bin/python3", shutil.which("python3"), sys.executable):
if not py:
continue
env = dict(os.environ)
env[_GUARD] = "1"
env["PYTHONPATH"] = os.pathsep.join(p for p in (pypath, script_dir, env.get("PYTHONPATH", "")) if p)
if _can_run_adapter(py, env):
os.execve(py, [py, script, *sys.argv[1:]], env)
sys.stderr.write("btrc debug adapter: no Python interpreter could import both lldb and the adapter.\n")
sys.exit(1)