Plain is headed towards 1.0! Subscribe for development updates →

 1import os
 2import platform
 3import subprocess
 4import sys
 5from pathlib import Path
 6
 7from . import pdb
 8
 9
10def set_breakpoint_hook():
11    """
12    If a `plain dev` process is running, set a
13    breakpoint hook to trigger a remote debugger.
14    """
15
16    if not os.environ.get("PLAIN_DEV"):
17        # Only want to set the breakpoint hook if
18        # we're in a process managed by `plain dev`
19        return
20
21    def _breakpoint():
22        system = platform.system()
23
24        if system == "Darwin":
25            pwd = Path.cwd()
26            script = f"""
27            tell application "Terminal"
28                activate
29                do script "cd {pwd} && plain dev debug"
30            end tell
31            """
32            subprocess.run(["osascript", "-e", script])
33        else:
34            raise OSError("Unsupported operating system")
35
36        pdb.set_trace(
37            # Make sure the debugger starts outside of this
38            frame=sys._getframe().f_back,
39        )
40
41    sys.breakpointhook = _breakpoint