summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorJos Verlinde <jos_verlinde@hotmail.com>2025-04-07 23:01:06 +0200
committerDamien George <damien@micropython.org>2025-04-09 10:44:45 +1000
commit1aa9b3d94bd66a625173b6182df8a5308279b6d0 (patch)
tree26b1833b09c7987b729ccc17c29c52a17585f755 /tools
parent037f2dad72a7d11d461330873b50bb9ccf4fed69 (diff)
tools/mpremote: Add recursive remove functionality to filesystem cmds.
mpremote now supports `mpremote rm -r`. Addresses #9802 and #16845. Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/mpremote/mpremote/commands.py28
-rw-r--r--tools/mpremote/mpremote/main.py2
-rw-r--r--tools/mpremote/mpremote/transport.py6
3 files changed, 33 insertions, 3 deletions
diff --git a/tools/mpremote/mpremote/commands.py b/tools/mpremote/mpremote/commands.py
index e385d0509..690b2ea72 100644
--- a/tools/mpremote/mpremote/commands.py
+++ b/tools/mpremote/mpremote/commands.py
@@ -1,4 +1,5 @@
import binascii
+import errno
import hashlib
import os
import sys
@@ -300,6 +301,28 @@ def do_filesystem_recursive_cp(state, src, dest, multiple, check_hash):
do_filesystem_cp(state, src_path_joined, dest_path_joined, False, check_hash)
+def do_filesystem_recursive_rm(state, path, args):
+ if state.transport.fs_isdir(path):
+ for entry in state.transport.fs_listdir(path):
+ do_filesystem_recursive_rm(state, _remote_path_join(path, entry.name), args)
+ if path:
+ try:
+ state.transport.fs_rmdir(path)
+ if args.verbose:
+ print(f"removed directory: '{path}'")
+ except OSError as e:
+ if e.errno != errno.EINVAL: # not vfs mountpoint
+ raise CommandError(
+ f"rm -r: cannot remove :{path} {os.strerror(e.errno) if e.errno else ''}"
+ ) from e
+ if args.verbose:
+ print(f"skipped: '{path}' (vfs mountpoint)")
+ else:
+ state.transport.fs_rmfile(path)
+ if args.verbose:
+ print(f"removed: '{path}'")
+
+
def do_filesystem(state, args):
state.ensure_raw_repl()
state.did_action()
@@ -352,7 +375,10 @@ def do_filesystem(state, args):
elif command == "mkdir":
state.transport.fs_mkdir(path)
elif command == "rm":
- state.transport.fs_rmfile(path)
+ if args.recursive:
+ do_filesystem_recursive_rm(state, path, args)
+ else:
+ state.transport.fs_rmfile(path)
elif command == "rmdir":
state.transport.fs_rmdir(path)
elif command == "touch":
diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py
index bdae8f163..b30a1a213 100644
--- a/tools/mpremote/mpremote/main.py
+++ b/tools/mpremote/mpremote/main.py
@@ -182,7 +182,7 @@ def argparse_rtc():
def argparse_filesystem():
cmd_parser = argparse.ArgumentParser(description="execute filesystem commands on the device")
- _bool_flag(cmd_parser, "recursive", "r", False, "recursive copy (for cp command only)")
+ _bool_flag(cmd_parser, "recursive", "r", False, "recursive (for cp and rm commands)")
_bool_flag(
cmd_parser,
"force",
diff --git a/tools/mpremote/mpremote/transport.py b/tools/mpremote/mpremote/transport.py
index c8fdc54a8..8d30c7f51 100644
--- a/tools/mpremote/mpremote/transport.py
+++ b/tools/mpremote/mpremote/transport.py
@@ -24,7 +24,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
-import ast, hashlib, os, sys
+import ast, errno, hashlib, os, sys
from collections import namedtuple
@@ -63,6 +63,10 @@ def _convert_filesystem_error(e, info):
return FileExistsError(info)
if "OSError" in e.error_output and "ENODEV" in e.error_output:
return FileNotFoundError(info)
+ if "OSError" in e.error_output and "EINVAL" in e.error_output:
+ return OSError(errno.EINVAL, info)
+ if "OSError" in e.error_output and "EPERM" in e.error_output:
+ return OSError(errno.EPERM, info)
return e