Files
soleprint/soleprint/station/tools/keymatrix/endpoint.py
2026-09-24 00:58:19 -03:00

56 lines
1.6 KiB
Python

"""
Which endpoint am I running on.
Same shape as `dm/dmapp/dmcore/config.py:resolve_device()` -- env var, then a
config file, then the hostname -- with one extension the fleet forces:
**WSL Debian inside ethbox is also called `ethbox`.** `peripheral-matrix/
endpoints.csv` already lists `ethbox-windows` and `ethbox-wsl` as separate rows
because they have entirely different probe surfaces, and a hostname cannot tell
them apart. `/proc/sys/kernel/osrelease` can.
"""
import os
import platform
import socket
from pathlib import Path
CONFIG_NAME = "keymatrix.json"
def is_wsl():
try:
return "microsoft" in Path("/proc/sys/kernel/osrelease").read_text().lower()
except OSError:
return False
def resolve_endpoint(config_path=None):
name = os.environ.get("KEYMATRIX_ENDPOINT")
if not name:
path = Path(config_path) if config_path else Path(__file__).parent / CONFIG_NAME
if path.exists():
import json
try:
name = json.loads(path.read_text()).get("endpoint") or ""
except ValueError:
name = ""
if not name:
name = socket.gethostname().split(".")[0]
if is_wsl():
name += "-wsl"
return name
def platform_kind():
"""What probe backend suits this machine, if none was asked for."""
if os.environ.get("KEYMATRIX_PROBE"):
return os.environ["KEYMATRIX_PROBE"]
if platform.system() == "Windows":
return "ahk"
if is_wsl():
return "wsl"
if os.environ.get("XDG_CURRENT_DESKTOP", "").upper().find("GNOME") >= 0:
return "gnome"
return "unknown"