Skip to content

installer: add --local (on-printer) mode; fix G29 hijack on commented stock macro - #3

Draft
sammcj wants to merge 2 commits into
grant0013:mainfrom
sammcj:k2-pro-fixes
Draft

installer: add --local (on-printer) mode; fix G29 hijack on commented stock macro#3
sammcj wants to merge 2 commits into
grant0013:mainfrom
sammcj:k2-pro-fixes

Conversation

@sammcj

@sammcj sammcj commented Jul 6, 2026

Copy link
Copy Markdown

What

Two installer changes so KAMP-K2 can be re-applied on the printer after a Creality firmware update:

  1. --local mode - run install_k2.py directly on the printer, no SSH/paramiko/venv.
  2. Fix a G29 hijack crash when the stock [gcode_macro G29] ships commented out (K2 Pro).

Why

  • Firmware updates revert printer.cfg / gcode_macro.cfg, so KAMP has to be re-applied after each one. install.sh can't run on the printer - its buildroot Python fails at ensurepip, so there was no clean on-printer path.
  • On the K2 Pro, stock G29 ships commented (# [gcode_macro G29]). The old hijack ran re.search(r"^\[gcode_macro G29\]...").group(0) on it; re.search returned NoneAttributeError, aborting the install.

Changes

  • paramiko is imported lazily inside connect() (remote --host path only, guarded by TYPE_CHECKING for hints). run / read_remote / write_remote gained a local branch; copy_file and every patch step route through them, so the whole flow works unchanged locally. --host is required only when --local is absent.
  • New idempotent _hijack_macro() for G29 + BED_MESH_CALIBRATE_START_PRINT: handles hijacked / active / commented / absent, matches the header whitespace-tolerantly (so it can never emit a duplicate active section that stops Klipper booting), and inserts the template literally (backslash-safe).
  • README.md + docs/INSTALL_K2.md document the --local re-apply-after-update flow.
# on the printer, after a firmware update:
cd ~/KAMP-K2 && git pull && python3 install_k2.py --local

Testing

  • End-to-end on a K2 Pro (Python 3.9.12, no paramiko installed): python3 install_k2.py --local --dry-run completes every step and is idempotent on an already-installed printer.
  • _hijack_macro checked against a real stock K2 Pro gcode_macro.cfg (commented G29 + inert BED_MESH_CALIBRATE_START_PRINT): exactly one active section per macro, re-run is a no-op, and tab / double-space headers don't duplicate.
  • Remote --host (SSH) path behaviourally unchanged.

Notes

  • Tested on K2 Pro (F021). Logic is variant-agnostic but not retested on F008 / K2 / Combo.
  • An absent BED_MESH_CALIBRATE_START_PRINT is now appended a no-op hijack (previously left alone) - harmless and consistent with the hijack's intent.

… stock macro

install.sh can't run on the printer itself - its buildroot Python has no
working pip/venv (ensurepip fails), so re-applying KAMP after a Creality
firmware update meant reaching for a PC. Add a --local mode that runs the
installer directly on the printer with no SSH/paramiko/venv:

- paramiko is imported lazily inside connect() (guarded by TYPE_CHECKING for
  hints); run/read_remote/write_remote gained local branches (subprocess +
  direct file I/O). copy_file and every patch step route through these, so the
  whole flow works unchanged locally. --host is required only without --local.

Also fix an AttributeError in the G29 hijack: the old
re.search(r'^\[gcode_macro G29\]...').group(0) crashed when the stock
[gcode_macro G29] is commented out (as on the K2 Pro). New idempotent
_hijack_macro() helper handles hijacked/active/commented/absent states, matches
the header whitespace-tolerantly (so it can never emit a duplicate active
section), and inserts the template literally (backslash-safe).

Note: an absent BED_MESH_CALIBRATE_START_PRINT is now appended a no-op hijack
(previously left alone); harmless and consistent with intent.

Docs: README + INSTALL_K2 describe the --local re-apply-after-update flow.

Tested end-to-end on a K2 Pro (Python 3.9.12, no paramiko): --local --dry-run
completes all steps and is idempotent on an already-installed printer.
@sammcj

sammcj commented Jul 6, 2026

Copy link
Copy Markdown
Author

Thought I'd raise this as a DRAFT for comments first @grant0013, I created this off the back of having several Python issues after applying the most recent two K2 Pro firmware updates. Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant