Skip to content

Commit 1aa9b3d

Browse files
Josverldpgeorge
authored andcommitted
tools/mpremote: Add recursive remove functionality to filesystem cmds.
mpremote now supports `mpremote rm -r`. Addresses #9802 and micropython#16845. Signed-off-by: Jos Verlinde <[email protected]>
1 parent 037f2da commit 1aa9b3d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

tools/mpremote/mpremote/commands.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import binascii
2+
import errno
23
import hashlib
34
import os
45
import sys
@@ -300,6 +301,28 @@ def _mkdir(a, *b):
300301
do_filesystem_cp(state, src_path_joined, dest_path_joined, False, check_hash)
301302

302303

304+
def do_filesystem_recursive_rm(state, path, args):
305+
if state.transport.fs_isdir(path):
306+
for entry in state.transport.fs_listdir(path):
307+
do_filesystem_recursive_rm(state, _remote_path_join(path, entry.name), args)
308+
if path:
309+
try:
310+
state.transport.fs_rmdir(path)
311+
if args.verbose:
312+
print(f"removed directory: '{path}'")
313+
except OSError as e:
314+
if e.errno != errno.EINVAL: # not vfs mountpoint
315+
raise CommandError(
316+
f"rm -r: cannot remove :{path} {os.strerror(e.errno) if e.errno else ''}"
317+
) from e
318+
if args.verbose:
319+
print(f"skipped: '{path}' (vfs mountpoint)")
320+
else:
321+
state.transport.fs_rmfile(path)
322+
if args.verbose:
323+
print(f"removed: '{path}'")
324+
325+
303326
def do_filesystem(state, args):
304327
state.ensure_raw_repl()
305328
state.did_action()
@@ -352,7 +375,10 @@ def do_filesystem(state, args):
352375
elif command == "mkdir":
353376
state.transport.fs_mkdir(path)
354377
elif command == "rm":
355-
state.transport.fs_rmfile(path)
378+
if args.recursive:
379+
do_filesystem_recursive_rm(state, path, args)
380+
else:
381+
state.transport.fs_rmfile(path)
356382
elif command == "rmdir":
357383
state.transport.fs_rmdir(path)
358384
elif command == "touch":

tools/mpremote/mpremote/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def argparse_rtc():
182182

183183
def argparse_filesystem():
184184
cmd_parser = argparse.ArgumentParser(description="execute filesystem commands on the device")
185-
_bool_flag(cmd_parser, "recursive", "r", False, "recursive copy (for cp command only)")
185+
_bool_flag(cmd_parser, "recursive", "r", False, "recursive (for cp and rm commands)")
186186
_bool_flag(
187187
cmd_parser,
188188
"force",

tools/mpremote/mpremote/transport.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
# THE SOFTWARE.
2626

27-
import ast, hashlib, os, sys
27+
import ast, errno, hashlib, os, sys
2828
from collections import namedtuple
2929

3030

@@ -63,6 +63,10 @@ def _convert_filesystem_error(e, info):
6363
return FileExistsError(info)
6464
if "OSError" in e.error_output and "ENODEV" in e.error_output:
6565
return FileNotFoundError(info)
66+
if "OSError" in e.error_output and "EINVAL" in e.error_output:
67+
return OSError(errno.EINVAL, info)
68+
if "OSError" in e.error_output and "EPERM" in e.error_output:
69+
return OSError(errno.EPERM, info)
6670
return e
6771

6872

0 commit comments

Comments
 (0)