diff --git a/CLAUDE.md b/CLAUDE.md index 77cfb9d..3350d57 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -781,8 +781,10 @@ branch on the platform per frame, and never `from meshterm.platforms import PLAT (`emulator/framebuffer.py`, written ahead of the hardware), a Tk window on the desktop (`emulator/window.py`). The window is the **emulator** for both handhelds — `meshterm emulate cardputer-zero|picocalc-lyra --mock`, after `meshterm emulate - --fetch-fonts` once (Terminus is OFL, so it is fetched and digest-checked, never - shipped). An `EmulatedDevice` (`emulator/devices.py`) is everything that differs per + --fetch-fonts` once (Terminus is OFL, so it is fetched and digest-checked, never in the + repo or the wheel; the store `.deb`, `scripts/cardputer-zero/build-deb.sh`, carries it + beside `OFL.TXT`, since a launcher app has no terminal to fetch it from — JP, + 2026-10-07: "minimize potential problems"). An `EmulatedDevice` (`emulator/devices.py`) is everything that differs per handheld: the panel, a Linux console's top-left grid and bold-as-bright 4-bit palette (the PicoCalc), the lane keys drawn under the panel (the Cardputer's 4–8), and how its keyboard spells Shift+F-keys. It is **not a strict emulator** — it mimics the display's diff --git a/docs/devices/cardputer-zero.md b/docs/devices/cardputer-zero.md index e5e87a0..85b7fb1 100644 --- a/docs/devices/cardputer-zero.md +++ b/docs/devices/cardputer-zero.md @@ -63,7 +63,8 @@ draws each character on the display itself and reads the keys directly. **Not done:** -- A package for M5Stack's app store. Until there is one, you install MeshTerm +- MeshTerm in M5Stack's app store. `scripts/cardputer-zero/build-deb.sh` builds the + package, but the store does not have it yet. Until it does, you install MeshTerm yourself (refer to the next section). - Typing on the real keyboard. Key events for the arrows, Enter, Esc, Fn+4 to Fn+8, and Shift were sent to the keyboard from another computer. They worked. The symbols on the @@ -93,8 +94,9 @@ python3 -m venv .venv scripts/cardputer-zero/launcher-entry.sh ``` -MeshTerm now has an icon in the launcher. To remove the icon, run -`scripts/cardputer-zero/launcher-entry.sh remove`. +MeshTerm now has an icon in the launcher. The icon is a negative of the MeshTerm logo. Thus +it is different from the icon of MeshTerm from the app store, and you can have both. To +remove the icon, run `scripts/cardputer-zero/launcher-entry.sh remove`. ### Typing long text from your computer diff --git a/meshterm/emulator/fetch.py b/meshterm/emulator/fetch.py index fa04f78..81e6ff0 100644 --- a/meshterm/emulator/fetch.py +++ b/meshterm/emulator/fetch.py @@ -1,7 +1,10 @@ # SPDX-License-Identifier: Apache-2.0 """Download the Terminus 12-pixel fonts that the emulator draws in: one time, only on request. -Terminus has the SIL Open Font License. Thus MeshTerm never ships it. This module +Terminus has the SIL Open Font License. Thus the repository and the wheel never carry it. +The Debian package for the Cardputer Zero carries it next to its licence, because an app +from the launcher has no terminal in which to run this download. That build also uses +this module (refer to ``scripts/cardputer-zero/build-deb.sh``). This module downloads the upstream release and checks that it is the correct archive. Then it extracts ``ter-u12n.bdf``, ``ter-u12b.bdf``, and the licence next to them into the directory that the emulator reads (:func:`~.font.font_dir`). ``meshterm emulate diff --git a/packaging/notices.py b/packaging/notices.py index e4c4849..4da56bf 100644 --- a/packaging/notices.py +++ b/packaging/notices.py @@ -36,7 +36,7 @@ import builtins import platform import sys from dataclasses import dataclass -from importlib.metadata import Distribution, PackagePath, distribution +from importlib.metadata import Distribution, PackagePath, distribution, distributions from pathlib import Path from packaging.markers import default_environment @@ -318,16 +318,41 @@ def dependency_closure(root: str = ROOT_DISTRIBUTION) -> list[Distribution]: return sorted(seen.values(), key=lambda d: canonicalize_name(d.metadata["Name"])) -def collect_notices(root: str = ROOT_DISTRIBUTION) -> list[Notice]: +def installed_in(site: str | Path, root: str = ROOT_DISTRIBUTION) -> list[Distribution]: + """Each distribution installed in the directory ``site``, except ``root``. + + The Debian package for the Cardputer Zero installs MeshTerm with ``pip install + --target`` (refer to ``scripts/cardputer-zero/build-deb.sh``). Thus that directory holds + exactly the distributions that the package bundles. They include the ``spi`` extra, + which :func:`dependency_closure` does not follow, because it asks for no extras. The + directory is the true list, so this function reads it and does not walk requirements. + + Returns: + The distributions, in the order of their canonical (PEP 503) name, as + :func:`dependency_closure` returns them. + """ + found = {canonicalize_name(d.metadata["Name"]): d for d in distributions(path=[str(site)])} + found.pop(canonicalize_name(root), None) + return [found[key] for key in sorted(found)] + + +def collect_notices(root: str = ROOT_DISTRIBUTION, site: str | Path | None = None) -> list[Notice]: """All the notices that ``THIRD-PARTY-NOTICES.txt`` of this build must carry. The PSF notice of Python is always first. Python is not a PyPI distribution that ``Requires-Dist`` can name, so it can never appear in :func:`dependency_closure`. Each dependency follows it, in the order of its name. Thus the difference between the notices file of one build and the next is stable, and a person can examine it. + + Args: + root: The distribution that the build ships. + site: A directory that ``pip install --target`` filled. If it is given, the + dependencies are the distributions in it (:func:`installed_in`). If it is + ``None``, they are the closure of ``root`` in this interpreter. """ notices = [_python_notice()] - for dist in dependency_closure(root): + bundled = dependency_closure(root) if site is None else installed_in(site, root) + for dist in bundled: canonical = canonicalize_name(dist.metadata["Name"]) notices.append( Notice( @@ -364,29 +389,32 @@ def render(notices: list[Notice]) -> str: return "\n".join(blocks).rstrip("\n") + "\n" -def write_third_party_notices(path: str | Path, root: str = ROOT_DISTRIBUTION) -> Path: +def write_third_party_notices( + path: str | Path, root: str = ROOT_DISTRIBUTION, site: str | Path | None = None +) -> Path: r"""Generate ``THIRD-PARTY-NOTICES.txt`` and write it to ``path``. The function writes raw UTF-8 bytes. It does not use text mode. Thus a Windows build does not change the ``\n`` line endings to ``\r\n``. The content of the file must be the - same, byte for byte, on each platform that builds it. + same, byte for byte, on each platform that builds it. ``site`` has the meaning that + :func:`collect_notices` gives it. Returns: ``path``, as a :class:`~pathlib.Path`, for the convenience of the caller. """ path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) - path.write_bytes(render(collect_notices(root)).encode("utf-8")) + path.write_bytes(render(collect_notices(root, site)).encode("utf-8")) return path def main(argv: list[str] | None = None) -> int: - """CLI entry point: ``python packaging/notices.py OUTPUT_PATH``.""" + """CLI entry point: ``python packaging/notices.py OUTPUT_PATH [SITE_DIR]``.""" args = sys.argv[1:] if argv is None else argv - if len(args) != 1: - print("usage: notices.py OUTPUT_PATH", file=sys.stderr) + if len(args) not in (1, 2): + print("usage: notices.py OUTPUT_PATH [SITE_DIR]", file=sys.stderr) return 2 - written = write_third_party_notices(args[0]) + written = write_third_party_notices(args[0], site=args[1] if len(args) == 2 else None) print(f"wrote {written}") return 0 diff --git a/scripts/README.md b/scripts/README.md index 6b34cc2..b6ec7af 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -5,7 +5,7 @@ These are tools for the repository. None of them is in the wheel. | Script | What it is for | |---|---| | `basemap-doctor.py` | Finds why a user's map has no basemap: certificates, DNS, proxy, or colour depth | -| `cardputer-zero/` | `launcher-entry.sh` puts a source checkout in the Cardputer Zero's app launcher. `remove` takes it out. This is for the time before there is a store package. `meshterm.png` is the icon: the website's logo, at the launcher's 100×100 size. `type-text.py` types a pasted string (an API key, a Wi-Fi password) on the Cardputer's keyboard from another computer, over SSH | +| `cardputer-zero/` | `build-deb.sh` builds the Debian package for M5Stack's app store. Run it on the Cardputer Zero or on another arm64 Debian 13 system. `store/` holds the listing for the store (`app-builder.json`, which `czdev publish` reads) and its icon. `launcher-entry.sh` puts a source checkout in the Cardputer Zero's app launcher. `remove` takes it out. `meshterm.png` is the package's icon: the website's logo, at the launcher's 100×100 size. `meshterm-dev.png` is its negative, for the checkout's entry, so that the two entries look different. `type-text.py` types a pasted string (an API key, a Wi-Fi password) on the Cardputer's keyboard from another computer, over SSH | | `picocalc-lyra/` | Setup of the Calculinux device: console font, palette, Wi-Fi. `xiao-radio/` is the kit for the XIAO radio (firmware build, flashing, UART setup) | | `uconsole/` | The uConsole's always-on SPI bridge service. MeshTerm can also control that radio directly, with no script (refer to `docs/devices/uconsole.md`) | diff --git a/scripts/cardputer-zero/build-deb.sh b/scripts/cardputer-zero/build-deb.sh new file mode 100755 index 0000000..31ff880 --- /dev/null +++ b/scripts/cardputer-zero/build-deb.sh @@ -0,0 +1,171 @@ +#!/bin/sh +# SPDX-License-Identifier: Apache-2.0 +# build-deb -- build the Debian package of MeshTerm for the Cardputer Zero's app store. +# +# Usage: scripts/cardputer-zero/build-deb.sh [REVISION] +# +# Run it on arm64 Debian 13 ("Trixie") with Python 3.13: on the Cardputer Zero itself, or +# in a debian:trixie container on an arm64 machine. The package holds compiled code +# (spidev, pycryptodome, and others), so it must be built on the system that it is for. +# Build it from a clean checkout of a release tag. pip builds MeshTerm from this tree, and +# it also takes the files in meshterm/ that git does not track. On the Cardputer Zero, /tmp +# is in memory and too small for the build: set TMPDIR to a directory on the card. +# +# The package depends only on python3. All else is inside it: +# +# /usr/lib/meshterm/site MeshTerm and its Python packages, compiled to bytecode +# /usr/lib/meshterm/fonts Terminus 6x12, regular and bold, and its licence (the OFL) +# /usr/share/APPLaunch/ the launcher entry, its icon, and the wrapper that it runs +# /usr/bin/meshterm the command line, for a user on SSH +# /usr/share/doc/meshterm/ LICENSE, NOTICE, THIRD-PARTY-NOTICES.txt, and copyright +# +# We add no other Debian dependency on purpose. When a dependency is not installed, the +# store app runs `apt-get install`, but it never runs `apt-get update`. On a device with +# old package lists, that install can fail. For the same reason the package carries the +# font: Terminus is not in the repository or in the wheel, but a launcher app has no +# terminal in which the user can run `meshterm emulate --fetch-fonts`. +# +# REVISION is the Debian revision, 1 by default. Increase it to publish a new package of +# the same MeshTerm version, because the store refuses a version that is not newer. +# Set TERMINUS_ARCHIVE to the path of terminus-font-4.49.1.tar.gz if the download fails. +# The build checks that archive against the same digest. +set -eu +umask 022 + +HERE=$(cd "$(dirname "$0")" && pwd) +SOURCE=$(cd "$HERE/../.." && pwd) +REVISION=${1:-1} +PYTHON=/usr/bin/python3 + +die() { + echo "build-deb: $*" >&2 + exit 1 +} + +command -v dpkg-deb >/dev/null || die "no dpkg-deb here -- run this on Debian" +[ "$(dpkg --print-architecture)" = arm64 ] || die "this is not arm64 -- the package holds arm64 code" +[ "$("$PYTHON" -c 'import sys; print("%d.%d" % sys.version_info[:2])')" = 3.13 ] || + die "$PYTHON is not Python 3.13 -- the package is for Debian 13" +case "$REVISION" in +*[!0-9]* | "") die "the revision must be a number, not '$REVISION'" ;; +esac + +VERSION=$(sed -n 's/^__version__ = "\(.*\)"$/\1/p' "$SOURCE/meshterm/__init__.py") +[ -n "$VERSION" ] || die "no __version__ in $SOURCE/meshterm/__init__.py" +DEB_VERSION="$VERSION-$REVISION" + +WORK=$(mktemp -d) +trap 'rm -rf "$WORK"' EXIT +STAGE="$WORK/root" +LIB="$STAGE/usr/lib/meshterm" +DOC="$STAGE/usr/share/doc/meshterm" +APPLAUNCH="$STAGE/usr/share/APPLaunch" + +echo "building meshterm $DEB_VERSION from $SOURCE" + +# A venv for the tools of the build only. It has a current pip and the `packaging` library +# that packaging/notices.py imports. Its base is the system Python, so the compiled +# packages that its pip builds are correct for the Python that runs them. +"$PYTHON" -m venv "$WORK/venv" +"$WORK/venv/bin/pip" install --quiet --upgrade pip packaging + +# MeshTerm and its packages, with the radio of the Cap LoRa-1262 (the `spi` extra). +# --no-compile, because the bytecode is compiled below in a different mode. +"$WORK/venv/bin/pip" install --quiet --no-compile --target "$LIB/site" "$SOURCE[spi]" +# pip writes console scripts whose first line names the build venv. The wrappers below +# replace them. +rm -rf "$LIB/site/bin" + +# The licences: MeshTerm's own, and the licence of each package in the site directory. +mkdir -p "$DOC" +cp "$SOURCE/LICENSE" "$SOURCE/NOTICE" "$DOC/" +"$WORK/venv/bin/python" "$SOURCE/packaging/notices.py" "$DOC/THIRD-PARTY-NOTICES.txt" "$LIB/site" +cat >"$DOC/copyright" </dev/null + +# The wrappers. PYTHONPATH is exported, so that a child process of MeshTerm finds the same +# packages. -s keeps the packages of the user's own site directory out. +wrapper() { + cat >"$1" <"$APPLAUNCH/applications/meshterm.desktop" </dev/null +} +check -m meshterm --help +check -m meshterm contacts --help +check -m meshterm specimen +check -c 'from meshterm.emulator.font import find_font; find_font()' + +mkdir -p "$STAGE/DEBIAN" +cat >"$STAGE/DEBIAN/control" < +Installed-Size: $(du -sk "$STAGE" | cut -f1) +Depends: python3 (>= 3.13), python3 (<< 3.14) +Section: hamradio +Priority: optional +Homepage: https://meshterm.net +Description: full-featured MeshCore client for the Cardputer Zero + MeshTerm is a full-featured TUI MeshCore client. On the Cardputer Zero it + draws on the display itself, and it runs the Cap LoRa-1262 as its own radio. + It can also connect to a companion over USB, Bluetooth, or TCP. +EOF + +mkdir -p "$SOURCE/dist" +OUT="$SOURCE/dist/meshterm_${DEB_VERSION}_arm64.deb" +dpkg-deb --root-owner-group -Zxz --build "$STAGE" "$OUT" >/dev/null +echo "built $OUT ($(du -h "$OUT" | cut -f1))" diff --git a/scripts/cardputer-zero/store/app-builder.json b/scripts/cardputer-zero/store/app-builder.json new file mode 100644 index 0000000..2976d8a --- /dev/null +++ b/scripts/cardputer-zero/store/app-builder.json @@ -0,0 +1,35 @@ +{ + "package_name": "meshterm", + "app_name": "MeshTerm", + "bin_name": "meshterm", + "store": { + "summary": "A full-featured MeshCore client. Uses the Cap LoRa-1262 or a companion.", + "description": "MeshTerm is a full-featured MeshCore client. On the Cardputer Zero, it runs the Cap LoRa-1262 as its own radio, or it connects to a MeshCore companion over USB, Bluetooth, or TCP. Chat on channels and in direct messages, see the mesh on a map, trace paths, manage repeaters and rooms, and keep a record of every node heard. Fn+4 to Fn+8 drive the row of labels at the bottom of the display. Hold Esc to quit.", + "categories": [ + "Radio & Comms" + ], + "screenshots": [ + "screenshots/menu.png", + "screenshots/chat.png", + "screenshots/map.png", + "screenshots/contacts.png" + ], + "icon": "icon.png", + "license": "Apache-2.0", + "source_repo": "https://github.com/jpmartineau/MeshTerm", + "author": { + "github": "jpmartineau", + "display_name": "Jean-Pierre Martineau" + }, + "share_code": "MTRM", + "permissions": { + "camera": false, + "microphone": false, + "imu": false, + "network": true, + "additional_hardware": true, + "background_service": false, + "external_display": false + } + } +} diff --git a/scripts/cardputer-zero/store/icon.png b/scripts/cardputer-zero/store/icon.png new file mode 100644 index 0000000..dbf366f Binary files /dev/null and b/scripts/cardputer-zero/store/icon.png differ diff --git a/tests/test_notices.py b/tests/test_notices.py index 3932961..27bd9da 100644 --- a/tests/test_notices.py +++ b/tests/test_notices.py @@ -165,3 +165,34 @@ def test_every_vendored_license_gap_has_its_file() -> None: path = Path(notices.__file__).parent / rel assert path.is_file(), f"{name}: {rel} is missing" assert path.read_text(encoding="utf-8").strip(), f"{name}: {rel} is empty" + + +def _fake_dist(site: Path, name: str, version: str) -> None: + """Write a minimal installed distribution with a licence file into ``site``.""" + info = site / f"{name.replace('-', '_')}-{version}.dist-info" + info.mkdir(parents=True) + (info / "METADATA").write_text( + f"Metadata-Version: 2.1\nName: {name}\nVersion: {version}\nLicense-Expression: MIT\n", + encoding="utf-8", + ) + (info / "LICENSE").write_text(f"The licence of {name}.\n", encoding="utf-8") + (info / "RECORD").write_text( + f"{info.name}/METADATA,,\n{info.name}/LICENSE,,\n{info.name}/RECORD,,\n", + encoding="utf-8", + ) + + +def test_site_mode_lists_the_directory_and_not_the_closure(tmp_path: Path) -> None: + """With ``site``, the notices are the distributions in that directory, less MeshTerm. + + The Debian package for the Cardputer Zero bundles the ``spi`` extra. The walk of the + closure asks for no extras, so the package reads its ``pip install --target`` + directory instead. The Python notice stays first in this mode too. + """ + _fake_dist(tmp_path, "mesh-term", "1.0") + _fake_dist(tmp_path, "zeta-lib", "2.0") + _fake_dist(tmp_path, "alpha-lib", "3.0") + got = notices.collect_notices(site=tmp_path) + assert [n.name for n in got] == ["Python", "alpha-lib", "zeta-lib"] + assert got[1].summary == "MIT" + assert "The licence of alpha-lib." in got[1].text