summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/reference/mpremote.rst11
-rw-r--r--tools/mpremote/mpremote/main.py23
2 files changed, 34 insertions, 0 deletions
diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst
index 3927c9bad..eb0233bdd 100644
--- a/docs/reference/mpremote.rst
+++ b/docs/reference/mpremote.rst
@@ -133,6 +133,17 @@ The full list of supported commands are:
- ``rmdir <dirs...>`` to remove directories on the device
- ``touch <file..>`` to create the files (if they don't already exist)
+- edit a file on the device:
+
+ .. code-block:: bash
+
+ $ mpremote edit <files...>
+
+ 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 73cd5e153..e614156db 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)