From 59e3348c101efee4071dd2224d01ccce6075b692 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 18 Aug 2022 12:36:42 +1000 Subject: [PATCH] tools/mpremote: Add "edit" command. This allows a remote file to be edited locally by copying it over, running the local editor, then copying it back. Signed-off-by: Jim Mussared --- docs/reference/mpremote.rst | 11 +++++++++++ tools/mpremote/mpremote/main.py | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index 3927c9badf..eb0233bdda 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -133,6 +133,17 @@ The full list of supported commands are: - ``rmdir `` to remove directories on the device - ``touch `` to create the files (if they don't already exist) +- edit a file on the device: + + .. code-block:: bash + + $ mpremote edit + + The ``edit`` command will copy each file from the device to a local temporary + directory and then launch your editor for each file (defined by the environment + variable ``$EDITOR``). If the editor exits successfully, the updated file will + be copied back to the device. + - mount the local directory on the remote device: .. code-block:: bash diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index 73cd5e153b..e614156dbf 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -19,6 +19,7 @@ MicroPython device over a serial connection. Commands supported are: import os, sys from collections.abc import Mapping +import tempfile from textwrap import dedent import serial.tools.list_ports @@ -28,6 +29,7 @@ from .console import Console, ConsolePosix _PROG = "mpremote" +# (need_raw_repl, is_action, num_args_min, help_text) _COMMANDS = { "connect": ( False, @@ -39,6 +41,7 @@ _COMMANDS = { or any valid device name/path""", ), "disconnect": (False, False, 0, "disconnect current device"), + "edit": (True, True, 1, "edit files on the device"), "resume": (False, False, 0, "resume a previous mpremote session (will not auto soft-reset)"), "soft-reset": (False, True, 0, "perform a soft-reset of the device"), "mount": ( @@ -82,6 +85,7 @@ _BUILTIN_COMMAND_EXPANSIONS = { "ls": "fs ls", "cp": "fs cp", "rm": "fs rm", + "touch": "fs touch", "mkdir": "fs mkdir", "rmdir": "fs rmdir", "df": [ @@ -355,6 +359,23 @@ def do_filesystem(pyb, args): ) +def do_edit(pyb, args): + if not os.getenv("EDITOR"): + raise pyboard.PyboardError("edit: $EDITOR not set") + for src in get_fs_args(args): + src = src.lstrip(":") + dest_fd, dest = tempfile.mkstemp(suffix=os.path.basename(src)) + try: + print("edit :%s" % (src,)) + os.close(dest_fd) + pyb.fs_touch(src) + pyb.fs_get(src, dest, progress_callback=show_progress_bar) + if os.system("$EDITOR '%s'" % (dest,)) == 0: + pyb.fs_put(dest, src, progress_callback=show_progress_bar) + finally: + os.unlink(dest) + + def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject): while True: console_in.waitchar(pyb.serial) @@ -587,6 +608,8 @@ def main(): return ret elif cmd == "fs": do_filesystem(pyb, args) + elif cmd == "edit": + do_edit(pyb, args) elif cmd == "repl": do_repl(pyb, args)