| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import enum |
| 4 |
import json |
| 5 |
import platform |
| 6 |
from abc import ABC, abstractmethod |
| 7 |
from importlib import metadata |
| 8 |
from pathlib import Path |
| 9 |
from typing import TYPE_CHECKING, TypedDict |
| 10 |
|
| 11 |
import briefcase |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
# avoid circular imports |
| 15 |
from briefcase.commands.run import RunCommand |
| 16 |
from briefcase.config import AppConfig |
| 17 |
|
| 18 |
|
| 19 |
def _is_editable_pep610(dist_name: str) -> bool: |
| 20 |
"""Check if briefcase is installed as editable build. |
| 21 |
|
| 22 |
The check requires, that the tool that installs briefcase support PEP610 (eg. pip |
| 23 |
since v20.1). |
| 24 |
""" |
| 25 |
try: |
| 26 |
dist = metadata.distribution(dist_name) |
| 27 |
except metadata.PackageNotFoundError: |
| 28 |
raise |
| 29 |
|
| 30 |
direct_url = dist.read_text("direct_url.json") |
| 31 |
if direct_url is None: |
| 32 |
return False |
| 33 |
|
| 34 |
try: |
| 35 |
data = json.loads(direct_url) |
| 36 |
return data.get("dir_info", {}).get("editable", False) |
| 37 |
except Exception: |
| 38 |
return False |
| 39 |
|
| 40 |
|
| 41 |
IS_EDITABLE = _is_editable_pep610("briefcase") |
| 42 |
REPO_ROOT = Path(__file__).parent.parent.parent.parent if IS_EDITABLE else None |
| 43 |
|
| 44 |
|
| 45 |
def get_debugger_requirement(package_name: str, extras: str = ""): |
| 46 |
"""Get the requirement of a debugger support package. |
| 47 |
|
| 48 |
On editable installs of briefcase the path to the local package is used, to simplify |
| 49 |
the development of the debugger support packages. On normal installs the local |
| 50 |
version is not available, so the package from pypi is used, that corresponds to the |
| 51 |
version of briefcase. |
| 52 |
|
| 53 |
:param package_name: The name of the debugger support package. |
| 54 |
:param extras: Optional extras to add to the package requirement. Including square |
| 55 |
brackets. E.g. "[debugpy]". |
| 56 |
:return: The package requirement. |
| 57 |
""" |
| 58 |
if IS_EDITABLE and REPO_ROOT is not None: |
| 59 |
local_path = REPO_ROOT / "debugger" |
| 60 |
if local_path.exists() and local_path.is_dir(): |
| 61 |
return f"{local_path}{extras}" |
| 62 |
return f"{package_name}{extras}=={briefcase.__version__}" |
| 63 |
|
| 64 |
|
| 65 |
class AppPathMappings(TypedDict): |
| 66 |
device_sys_path_regex: str |
| 67 |
device_subfolders: list[str] |
| 68 |
host_folders: list[str] |
| 69 |
|
| 70 |
|
| 71 |
class AppPackagesPathMappings(TypedDict): |
| 72 |
sys_path_regex: str |
| 73 |
host_folder: str |
| 74 |
|
| 75 |
|
| 76 |
class DebuggerConfig(TypedDict): |
| 77 |
debugger: str |
| 78 |
host: str |
| 79 |
port: int |
| 80 |
host_os: str |
| 81 |
app_path_mappings: AppPathMappings | None |
| 82 |
app_packages_path_mappings: AppPackagesPathMappings | None |
| 83 |
|
| 84 |
|
| 85 |
class DebuggerConnectionMode(str, enum.Enum): |
| 86 |
SERVER = "server" |
| 87 |
CLIENT = "client" |
| 88 |
|
| 89 |
|
| 90 |
class BaseDebugger(ABC): |
| 91 |
"""Definition for a plugin that defines a new Briefcase debugger.""" |
| 92 |
|
| 93 |
@property |
| 94 |
@abstractmethod |
| 95 |
def name(self) -> str: |
| 96 |
"""Return the name debugger.""" |
| 97 |
|
| 98 |
@property |
| 99 |
@abstractmethod |
| 100 |
def connection_mode(self) -> DebuggerConnectionMode: |
| 101 |
"""Return the connection mode of the debugger.""" |
| 102 |
|
| 103 |
@property |
| 104 |
@abstractmethod |
| 105 |
def debugger_support_pkg(self) -> str: |
| 106 |
"""Get the name of the debugger support package.""" |
| 107 |
|
| 108 |
def get_env_config( |
| 109 |
self, |
| 110 |
cmd: RunCommand, |
| 111 |
app: AppConfig, |
| 112 |
) -> str: |
| 113 |
"""Get the environment config to start the debugger. |
| 114 |
|
| 115 |
:param cmd: The command that starts the debugger |
| 116 |
:param app: The app to be debugged |
| 117 |
:returns: The remote debugger configuration |
| 118 |
""" |
| 119 |
config = DebuggerConfig( |
| 120 |
debugger=app.debugger.name, |
| 121 |
host=app.debugger_host, |
| 122 |
port=app.debugger_port, |
| 123 |
host_os=platform.system(), |
| 124 |
app_path_mappings=cmd.debugger_app_path_mappings(app), |
| 125 |
app_packages_path_mappings=cmd.debugger_app_packages_path_mapping(app), |
| 126 |
) |
| 127 |
return json.dumps(config) |