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 <jim.mussared@gmail.com>
This commit is contained in:
parent
263737ecfe
commit
59e3348c10
@ -133,6 +133,17 @@ The full list of supported commands are:
|
|||||||
- ``rmdir <dirs...>`` to remove directories on the device
|
- ``rmdir <dirs...>`` to remove directories on the device
|
||||||
- ``touch <file..>`` to create the files (if they don't already exist)
|
- ``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:
|
- mount the local directory on the remote device:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
@ -19,6 +19,7 @@ MicroPython device over a serial connection. Commands supported are:
|
|||||||
|
|
||||||
import os, sys
|
import os, sys
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
|
import tempfile
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
import serial.tools.list_ports
|
import serial.tools.list_ports
|
||||||
@ -28,6 +29,7 @@ from .console import Console, ConsolePosix
|
|||||||
|
|
||||||
_PROG = "mpremote"
|
_PROG = "mpremote"
|
||||||
|
|
||||||
|
# (need_raw_repl, is_action, num_args_min, help_text)
|
||||||
_COMMANDS = {
|
_COMMANDS = {
|
||||||
"connect": (
|
"connect": (
|
||||||
False,
|
False,
|
||||||
@ -39,6 +41,7 @@ _COMMANDS = {
|
|||||||
or any valid device name/path""",
|
or any valid device name/path""",
|
||||||
),
|
),
|
||||||
"disconnect": (False, False, 0, "disconnect current device"),
|
"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)"),
|
"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"),
|
"soft-reset": (False, True, 0, "perform a soft-reset of the device"),
|
||||||
"mount": (
|
"mount": (
|
||||||
@ -82,6 +85,7 @@ _BUILTIN_COMMAND_EXPANSIONS = {
|
|||||||
"ls": "fs ls",
|
"ls": "fs ls",
|
||||||
"cp": "fs cp",
|
"cp": "fs cp",
|
||||||
"rm": "fs rm",
|
"rm": "fs rm",
|
||||||
|
"touch": "fs touch",
|
||||||
"mkdir": "fs mkdir",
|
"mkdir": "fs mkdir",
|
||||||
"rmdir": "fs rmdir",
|
"rmdir": "fs rmdir",
|
||||||
"df": [
|
"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):
|
def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject):
|
||||||
while True:
|
while True:
|
||||||
console_in.waitchar(pyb.serial)
|
console_in.waitchar(pyb.serial)
|
||||||
@ -587,6 +608,8 @@ def main():
|
|||||||
return ret
|
return ret
|
||||||
elif cmd == "fs":
|
elif cmd == "fs":
|
||||||
do_filesystem(pyb, args)
|
do_filesystem(pyb, args)
|
||||||
|
elif cmd == "edit":
|
||||||
|
do_edit(pyb, args)
|
||||||
elif cmd == "repl":
|
elif cmd == "repl":
|
||||||
do_repl(pyb, args)
|
do_repl(pyb, args)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user