tools/ public with a local config, private tools to .tth/tools/
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b7b397b9b1
commit
845a228209
27 files changed
+5312
-4
No files matched your search
Executable
+173
@@ -0,0 +1,173 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Local config of the MeshCore tools: which boards and nodes you have.
|
||||
|
||||
The tools in this directory carry no board serial, MAC or host of anyone's
|
||||
bench. They take them from ONE file beside the repo:
|
||||
|
||||
~/.config/meshcore-tools/config.ini (or $MESHCORE_TOOLS_CONFIG)
|
||||
|
||||
Copy tools/config.example.ini there and describe your boards. Without the
|
||||
file every tool still works with full serial numbers, MACs and port paths --
|
||||
only names like `b1` need it. No secrets go in here: admin passwords stay in
|
||||
platformio.local.ini.
|
||||
|
||||
From Python:
|
||||
|
||||
from mctools_config import serial_for, mac_for, boards
|
||||
serial_for("b1") # -> "0123456789ABCDEF", or the input unchanged
|
||||
|
||||
From shell (the same answers, one per line, exit 1 when there is none):
|
||||
|
||||
tools/mctools_config.py path
|
||||
tools/mctools_config.py serial b1 # alias, number or serial
|
||||
tools/mctools_config.py mac b1 # BLE MAC of that board
|
||||
tools/mctools_config.py get b1 protect # any field of the board
|
||||
tools/mctools_config.py list [--role bench] [--has protect] [--field serial]
|
||||
tools/mctools_config.py name 0123456789ABCDEF # board name for a serial or MAC
|
||||
|
||||
Board section, every field optional:
|
||||
|
||||
[board.b1]
|
||||
serial = 0123456789ABCDEF ; USB serial number (/dev/serial/by-id)
|
||||
mac = AA:BB:CC:DD:EE:01 ; BLE MAC of the application
|
||||
aliases = 1 ; other names that mean this board
|
||||
role = bench ; bench | live | probe -- tools and hooks decide by it
|
||||
protect = keeps an identity ; flashing tools refuse it unless told to
|
||||
refuse = an observer radio ; console tools refuse to open it at all
|
||||
host = lab-pc ; where it is plugged in (informative)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import configparser
|
||||
import os
|
||||
import re
|
||||
import shlex
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ENV = "MESHCORE_TOOLS_CONFIG"
|
||||
DEFAULT = Path.home() / ".config/meshcore-tools/config.ini"
|
||||
EXAMPLE = Path(__file__).resolve().parent / "config.example.ini"
|
||||
FIELDS = ("serial", "mac", "aliases", "role", "protect", "refuse", "host")
|
||||
|
||||
|
||||
def path() -> Path:
|
||||
return Path(os.environ.get(ENV) or DEFAULT)
|
||||
|
||||
|
||||
def boards() -> dict[str, dict[str, str]]:
|
||||
"""{name: {field: value}} from the config; empty without a config."""
|
||||
cp = configparser.ConfigParser(interpolation=None, inline_comment_prefixes=(";", "#"))
|
||||
cp.read(path())
|
||||
out = {}
|
||||
for sec in cp.sections():
|
||||
if sec.startswith("board."):
|
||||
out[sec[6:]] = {k: v.strip() for k, v in cp[sec].items()}
|
||||
return out
|
||||
|
||||
|
||||
def find(key: str) -> tuple[str, dict[str, str]] | None:
|
||||
"""The board a name, alias, serial number or MAC belongs to."""
|
||||
k = key.strip()
|
||||
for name, b in boards().items():
|
||||
aliases = [a.strip() for a in b.get("aliases", "").split(",") if a.strip()]
|
||||
if k == name or k in aliases:
|
||||
return name, b
|
||||
if b.get("serial") and k.upper() == b["serial"].upper():
|
||||
return name, b
|
||||
if b.get("mac") and k.upper() == b["mac"].upper():
|
||||
return name, b
|
||||
return None
|
||||
|
||||
|
||||
def serial_for(key: str) -> str:
|
||||
"""USB serial of a board name/alias, or the input unchanged (a serial or a
|
||||
port path passes straight through)."""
|
||||
hit = find(key)
|
||||
return hit[1].get("serial", key) if hit else key
|
||||
|
||||
|
||||
def mac_for(key: str) -> str | None:
|
||||
hit = find(key)
|
||||
return hit[1].get("mac") if hit else None
|
||||
|
||||
|
||||
def field(key: str, name: str) -> str | None:
|
||||
hit = find(key)
|
||||
return hit[1].get(name) if hit else None
|
||||
|
||||
|
||||
def by_role(role: str | None = None) -> dict[str, dict[str, str]]:
|
||||
return {n: b for n, b in boards().items() if role is None or b.get("role") == role}
|
||||
|
||||
|
||||
def unknown_alias_hint(key: str) -> str:
|
||||
return f"{key!r} is no board in {path()} (template: {EXAMPLE})"
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser(
|
||||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
|
||||
)
|
||||
sub = ap.add_subparsers(dest="cmd", required=True)
|
||||
sub.add_parser("path", help="the config file in use")
|
||||
sub.add_parser("hook-vars", help="shell assignments LIVE_IDS / BENCH_IDS (regexes) for a guard hook")
|
||||
for c in ("serial", "mac", "name"):
|
||||
sub.add_parser(c).add_argument("key")
|
||||
g = sub.add_parser("get")
|
||||
g.add_argument("key")
|
||||
g.add_argument("field", choices=FIELDS)
|
||||
lst = sub.add_parser("list")
|
||||
lst.add_argument("--role")
|
||||
lst.add_argument("--has", choices=FIELDS, help="only boards that set this field")
|
||||
lst.add_argument("--field", choices=FIELDS, help="print only this field (skip boards without it)")
|
||||
a = ap.parse_args()
|
||||
|
||||
if a.cmd == "path":
|
||||
print(path())
|
||||
return 0 if path().exists() else 1
|
||||
if a.cmd == "hook-vars":
|
||||
# live: MACs and names; bench: serials, MACs and names as whole words
|
||||
def ere(text: str) -> str: # POSIX ERE (grep -E), not Python re
|
||||
return re.sub(r"([.^$*+?()\[\]{}|\\])", r"\\\1", text)
|
||||
|
||||
def ids(role: str) -> str:
|
||||
out = []
|
||||
for n, b in by_role(role).items():
|
||||
out += [ere(b[k]) for k in ("serial", "mac") if b.get(k)]
|
||||
out.append(f"(^| ){ere(n)}( |$)")
|
||||
return "|".join(out)
|
||||
|
||||
print(f"LIVE_IDS={shlex.quote(ids('live'))}")
|
||||
print(f"BENCH_IDS={shlex.quote(ids('bench'))}")
|
||||
return 0
|
||||
if a.cmd == "list":
|
||||
for n, b in by_role(a.role).items():
|
||||
if a.has and not b.get(a.has):
|
||||
continue
|
||||
if a.field:
|
||||
if b.get(a.field):
|
||||
print(b[a.field])
|
||||
else:
|
||||
print(n)
|
||||
return 0
|
||||
hit = find(a.key)
|
||||
if a.cmd == "serial":
|
||||
val = hit[1].get("serial") if hit else None
|
||||
elif a.cmd == "mac":
|
||||
val = hit[1].get("mac") if hit else None
|
||||
elif a.cmd == "name":
|
||||
val = hit[0] if hit else None
|
||||
else:
|
||||
val = hit[1].get(a.field) if hit else None
|
||||
if not val:
|
||||
return 1
|
||||
print(val)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user