From aae7dc57f61307989c17900e630d78045651372c Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sat, 21 Nov 2026 15:12:22 +0100 Subject: [PATCH] phone_lease, android_ui: credit is optional phone_deploy.sh runs python_pkg.phone_lease with whatever python3 is first on PATH. e9ba82ed made it import kuhy_credit at module load, so from a venv without kuhy-credit every phone deploy died with ModuleNotFoundError. Both phone CLIs now take the epilog from help_epilog(), which returns None when the package is missing. Co-Authored-By: Claude Opus 5.5 --- .../phone-cli-credit-optional-2026-11-21.json | 12 +++++ .../phone-cli-credit-optional-2026-11-21.json | 32 +++++++++++ ...ebcam-c270-fixed-framerate-2026-10-12.json | 43 --------------- python_pkg/_optional_credit.py | 20 +++++++ python_pkg/android_ui/cli.py | 5 +- python_pkg/phone_lease/__main__.py | 5 +- .../phone_lease/tests/test_optional_credit.py | 53 +++++++++++++++++++ 7 files changed, 121 insertions(+), 49 deletions(-) create mode 100644 docs/superpowers/contracts/phone-cli-credit-optional-2026-11-21.json create mode 100644 docs/superpowers/evidence/phone-cli-credit-optional-2026-11-21.json delete mode 100644 docs/superpowers/evidence/webcam-c270-fixed-framerate-2026-10-12.json create mode 100644 python_pkg/_optional_credit.py create mode 100644 python_pkg/phone_lease/tests/test_optional_credit.py diff --git a/docs/superpowers/contracts/phone-cli-credit-optional-2026-11-21.json b/docs/superpowers/contracts/phone-cli-credit-optional-2026-11-21.json new file mode 100644 index 00000000..62a04fc3 --- /dev/null +++ b/docs/superpowers/contracts/phone-cli-credit-optional-2026-11-21.json @@ -0,0 +1,12 @@ +{ + "title": "Phone CLIs survive a missing kuhy-credit", + "objective": "phone_deploy.sh calls python3 -m python_pkg.phone_lease with whatever python3 is on PATH. A missing kuhy-credit package must not crash the lease or the android_ui driver; the credit stays in --help whenever the package is present.", + "acceptance_criteria": [ + "python -m python_pkg.phone_lease --help and python -m python_pkg.android_ui --help exit 0 with kuhy_credit blocked.", + "test_cli_credit.py still finds the credit line in both --help outputs when kuhy_credit is installed." + ], + "out_of_scope": [ + "The other ten credited CLIs, which are not started by phone tooling." + ], + "verifier": "pytest python_pkg/phone_lease/tests/test_optional_credit.py python_pkg/tests/test_cli_credit.py" +} diff --git a/docs/superpowers/evidence/phone-cli-credit-optional-2026-11-21.json b/docs/superpowers/evidence/phone-cli-credit-optional-2026-11-21.json new file mode 100644 index 00000000..fae8e566 --- /dev/null +++ b/docs/superpowers/evidence/phone-cli-credit-optional-2026-11-21.json @@ -0,0 +1,32 @@ +{ + "intent": "phone_lease and android_ui CLIs run under any python3, even one without kuhy-credit, so phone_deploy.sh works from any venv.", + "scope": [ + "python_pkg/_optional_credit.py", + "python_pkg/phone_lease/__main__.py", + "python_pkg/android_ui/cli.py", + "python_pkg/phone_lease/tests/test_optional_credit.py" + ], + "changes": [ + "New help_epilog() imports kuhy_credit lazily and returns None when it is missing.", + "phone_lease and android_ui take their argparse epilog from help_epilog() instead of a top-level kuhy_credit import." + ], + "verification": [ + { + "command": "pytest python_pkg/phone_lease/tests/test_optional_credit.py python_pkg/tests/test_cli_credit.py -k 'phone_lease or android_ui'", + "result": "pass", + "evidence": "2 failed before the change (ModuleNotFoundError: kuhy_credit); 5 passed after." + }, + { + "command": "/home/kuhy/src/todoImplementer/.venv/bin/python3 -m python_pkg.phone_lease status", + "result": "pass", + "evidence": "Printed the lease table instead of ModuleNotFoundError." + } + ], + "risks": [ + "With kuhy-credit absent the two CLIs show --help without the credit line." + ], + "rollback": [ + "git revert the commit", + "Then run phone_deploy.sh from a venv that has kuhy-credit installed." + ] +} diff --git a/docs/superpowers/evidence/webcam-c270-fixed-framerate-2026-10-12.json b/docs/superpowers/evidence/webcam-c270-fixed-framerate-2026-10-12.json deleted file mode 100644 index 32117981..00000000 --- a/docs/superpowers/evidence/webcam-c270-fixed-framerate-2026-10-12.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "intent": "Logitech C270 stays at 30 fps in Google Meet (Helium) in low light instead of dropping to ~15 fps and looking laggy.", - "scope": [ - "linux_configuration/fixes/fix_webcam_c270.sh", - "linux_configuration/fixes/99-c270-fixed-framerate.rules", - "linux_configuration/tests/test_fix_webcam_c270.bats", - "Non-goal: manual exposure lock; auto exposure stays on" - ], - "changes": [ - "udev rule sets exposure_dynamic_framerate=0 on the C270 capture node at every add event", - "Installer script with install/--remove/--status; installs v4l-utils if missing" - ], - "verification": [ - { - "command": "v4l2-ctl -d /dev/video0 -c exposure_dynamic_framerate=0 (live, Meet open)", - "result": "pass", - "evidence": "User confirmed Meet video no longer laggy" - }, - { - "command": "vm run c270 'fix_webcam_c270.sh; --status; --remove; --status'", - "result": "pass", - "evidence": "Rule installed then removed in vmbox, rc=0" - }, - { - "command": "v4l2-ctl -c exposure_dynamic_framerate=1 && sudo udevadm trigger --action=add --settle /sys/class/video4linux/video0", - "result": "pass", - "evidence": "Host control read back exposure_dynamic_framerate: 0 after simulated plug-in" - }, - { - "command": "bats linux_configuration/tests/test_fix_webcam_c270.bats", - "result": "pass", - "evidence": "5/5 ok" - } - ], - "risks": [ - "Darker, noisier picture in dim rooms (fixed 33 ms max exposure)", - "Chromium could start resetting this control in a future version, undoing the rule per stream" - ], - "rollback": [ - "fix_webcam_c270.sh --remove, then replug the camera", - "v4l2-ctl -d /dev/video0 -C exposure_dynamic_framerate reads the firmware value again" - ] -} diff --git a/python_pkg/_optional_credit.py b/python_pkg/_optional_credit.py new file mode 100644 index 00000000..ebeaeff9 --- /dev/null +++ b/python_pkg/_optional_credit.py @@ -0,0 +1,20 @@ +"""The kuhy credit for CLIs that phone tooling starts under any ``python3``. + +phone_deploy.sh and the android_ui driver run with whatever interpreter is +first on PATH, often a project venv without kuhy-credit. The credit is +decoration, so a missing package yields no epilog instead of a crash. +""" + +from __future__ import annotations + +import importlib + + +def help_epilog() -> str | None: + """Return the shared credit line, or None when kuhy_credit is missing.""" + try: + credit = importlib.import_module("kuhy_credit") + except ImportError: + return None + line: str = credit.credit_line() + return line diff --git a/python_pkg/android_ui/cli.py b/python_pkg/android_ui/cli.py index 3b3c9651..e2b4bd49 100644 --- a/python_pkg/android_ui/cli.py +++ b/python_pkg/android_ui/cli.py @@ -18,8 +18,7 @@ from __future__ import annotations import argparse import sys -from kuhy_credit import credit_line - +from python_pkg._optional_credit import help_epilog from python_pkg.android_ui.driver import AndroidUi, UiAutomationError from python_pkg.phone_guard.vd_state import display_owner, own_display from python_pkg.phone_lease import ( @@ -46,7 +45,7 @@ def _build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( prog="android-ui", description="Drive an Android app by element, never by coordinates.", - epilog=credit_line(), + epilog=help_epilog(), ) parser.add_argument("-s", "--serial", help="target device serial") parser.add_argument( diff --git a/python_pkg/phone_lease/__main__.py b/python_pkg/phone_lease/__main__.py index 1ca5afdf..f7c93b51 100644 --- a/python_pkg/phone_lease/__main__.py +++ b/python_pkg/phone_lease/__main__.py @@ -12,8 +12,7 @@ from dataclasses import asdict import json import sys -from kuhy_credit import credit_line - +from python_pkg._optional_credit import help_epilog from python_pkg.phone_lease.lease import ( DEFAULT_TTL, DEFAULT_WAIT, @@ -42,7 +41,7 @@ def _build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( prog="phone-lease", description=__doc__, - epilog=credit_line(), + epilog=help_epilog(), ) sub = parser.add_subparsers(dest="command", required=True) take = sub.add_parser( diff --git a/python_pkg/phone_lease/tests/test_optional_credit.py b/python_pkg/phone_lease/tests/test_optional_credit.py new file mode 100644 index 00000000..78c1b956 --- /dev/null +++ b/python_pkg/phone_lease/tests/test_optional_credit.py @@ -0,0 +1,53 @@ +"""Phone tooling must run when kuhy_credit is not importable. + +phone_deploy.sh and the android_ui driver start these CLIs with whatever +``python3`` is first on PATH (often a project venv without kuhy-credit). +The credit is decoration; a missing package must never block a phone lease. +""" + +from __future__ import annotations + +import subprocess +import sys + +from kuhy_credit import credit_line +import pytest + +from python_pkg._optional_credit import help_epilog + +# Blocks kuhy_credit (sys.modules[name] = None makes import raise +# ImportError), then runs the module as ``python -m --help`` would. +_RUN_WITHOUT_CREDIT = ( + "import runpy, sys; sys.modules['kuhy_credit'] = None; " + "sys.argv = [sys.argv[1], '--help']; " + "runpy.run_module(sys.argv[0], run_name='__main__', alter_sys=True)" +) + +PHONE_CLIS = ("python_pkg.phone_lease", "python_pkg.android_ui") + + +@pytest.mark.parametrize("module", PHONE_CLIS) +def test_help_works_without_kuhy_credit(module: str) -> None: + """``--help`` exits 0 even when kuhy_credit cannot be imported.""" + result = subprocess.run( + [sys.executable, "-c", _RUN_WITHOUT_CREDIT, module], + capture_output=True, + text=True, + timeout=30, + check=False, + ) + assert result.returncode == 0, result.stderr + assert "usage" in result.stdout.lower() + + +def test_help_epilog_none_without_kuhy_credit( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """help_epilog() returns None when kuhy_credit cannot be imported.""" + monkeypatch.setitem(sys.modules, "kuhy_credit", None) + assert help_epilog() is None + + +def test_help_epilog_is_credit_line() -> None: + """help_epilog() returns the shared credit line when it is installed.""" + assert help_epilog() == credit_line()