| 1 |
import re |
| 2 |
import sys |
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
import debugpy |
| 6 |
|
| 7 |
from briefcase_debugger.config import DebuggerConfig |
| 8 |
|
| 9 |
|
| 10 |
def find_first_matching_path(regex: str) -> str: |
| 11 |
"""Returns the first element of sys.paths that matches regex, otherwise None.""" |
| 12 |
for path in sys.path: |
| 13 |
if re.search(regex, path): |
| 14 |
return path |
| 15 |
raise ValueError(f"No sys.path entry matches regex '{regex}'") |
| 16 |
|
| 17 |
|
| 18 |
def load_path_mappings(config: DebuggerConfig, verbose: bool) -> list[tuple[str, str]]: |
| 19 |
app_path_mappings = config.get("app_path_mappings", None) |
| 20 |
app_packages_path_mappings = config.get("app_packages_path_mappings", None) |
| 21 |
|
| 22 |
mappings_list = [] |
| 23 |
if app_path_mappings: |
| 24 |
device_app_folder = find_first_matching_path( |
| 25 |
app_path_mappings["device_sys_path_regex"] |
| 26 |
) |
| 27 |
for app_subfolder_device, app_subfolder_host in zip( |
| 28 |
app_path_mappings["device_subfolders"], |
| 29 |
app_path_mappings["host_folders"], |
| 30 |
strict=False, |
| 31 |
): |
| 32 |
mappings_list.append( |
| 33 |
( |
| 34 |
app_subfolder_host, |
| 35 |
str(Path(device_app_folder) / app_subfolder_device), |
| 36 |
) |
| 37 |
) |
| 38 |
if app_packages_path_mappings: |
| 39 |
device_app_packages_folder = find_first_matching_path( |
| 40 |
app_packages_path_mappings["sys_path_regex"] |
| 41 |
) |
| 42 |
mappings_list.append( |
| 43 |
( |
| 44 |
app_packages_path_mappings["host_folder"], |
| 45 |
str(Path(device_app_packages_folder)), |
| 46 |
) |
| 47 |
) |
| 48 |
|
| 49 |
if verbose: |
| 50 |
print("Extracted path mappings:") |
| 51 |
for idx, mapping in enumerate(mappings_list): |
| 52 |
print(f"[{idx}] host = {mapping[0]}") |
| 53 |
print(f"[{idx}] device = {mapping[1]}") |
| 54 |
|
| 55 |
return mappings_list |
| 56 |
|
| 57 |
|
| 58 |
def start_debugpy(config: DebuggerConfig, verbose: bool): |
| 59 |
host = config["host"] |
| 60 |
port = config["port"] |
| 61 |
path_mappings = load_path_mappings(config, verbose) |
| 62 |
|
| 63 |
# Starting remote debugger... |
| 64 |
print(f"Starting debugpy in server mode at {host}:{port}...") |
| 65 |
debugpy.listen((host, port), in_process_debug_adapter=True) |
| 66 |
|
| 67 |
if verbose: |
| 68 |
# pydevd is dynamically loaded and only available after debugpy is started |
| 69 |
import pydevd |
| 70 |
|
| 71 |
pydevd.DebugInfoHolder.DEBUG_TRACE_LEVEL = 3 |
| 72 |
|
| 73 |
if len(path_mappings) > 0: |
| 74 |
if verbose: |
| 75 |
print("Adding path mappings...") |
| 76 |
|
| 77 |
# pydevd is dynamically loaded and only available after a debugger has connected |
| 78 |
import pydevd_file_utils |
| 79 |
|
| 80 |
pydevd_file_utils.setup_client_server_paths(path_mappings) |
| 81 |
|
| 82 |
print("The debugpy server started. Waiting for debugger to attach...") |
| 83 |
print( |
| 84 |
f""" |
| 85 |
To connect to debugpy using VS Code add the following configuration to '.vscode/launch.json': |
| 86 |
{{ |
| 87 |
"version": "0.2.0", |
| 88 |
"configurations": [ |
| 89 |
{{ |
| 90 |
"name": "Briefcase: Attach (Connect)", |
| 91 |
"type": "debugpy", |
| 92 |
"request": "attach", |
| 93 |
"connect": {{ |
| 94 |
"host": "{host}", |
| 95 |
"port": {port} |
| 96 |
}}, |
| 97 |
"justMyCode": false |
| 98 |
}} |
| 99 |
] |
| 100 |
}} |
| 101 |
|
| 102 |
For more information see: https://briefcase.beeware.org/en/stable/how-to/debugging/vscode/#bundled-app |
| 103 |
""" |
| 104 |
) |
| 105 |
debugpy.wait_for_client() |
| 106 |
|
| 107 |
print("Debugger attached.") |
| 108 |
print("-" * 75) |