tools/pyboard.py: Add fs_cp function for direct device-to-device copy.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-08-24 10:26:35 +10:00
parent 24f1161fe2
commit f5fedf4676
1 changed files with 26 additions and 9 deletions

View File

@ -491,6 +491,20 @@ class Pyboard:
) )
self.exec_(cmd, data_consumer=stdout_write_bytes) self.exec_(cmd, data_consumer=stdout_write_bytes)
def fs_cp(self, src, dest, chunk_size=256, progress_callback=None):
if progress_callback:
src_size = int(self.exec_("import os\nprint(os.stat('%s')[6])" % src))
written = 0
self.exec_("fr=open('%s','rb')\nr=fr.read\nfw=open('%s','wb')\nw=fw.write" % (src, dest))
while True:
data_len = int(self.exec_("d=r(%u)\nw(d)\nprint(len(d))" % chunk_size))
if not data_len:
break
if progress_callback:
written += data_len
progress_callback(written, src_size)
self.exec_("fr.close()\nfw.close()")
def fs_get(self, src, dest, chunk_size=256, progress_callback=None): def fs_get(self, src, dest, chunk_size=256, progress_callback=None):
if progress_callback: if progress_callback:
src_size = int(self.exec_("import os\nprint(os.stat('%s')[6])" % src)) src_size = int(self.exec_("import os\nprint(os.stat('%s')[6])" % src))
@ -584,18 +598,21 @@ def filesystem_command(pyb, args, progress_callback=None, verbose=False):
srcs = args[:-1] srcs = args[:-1]
dest = args[-1] dest = args[-1]
if dest.startswith(":"): if dest.startswith(":"):
op = pyb.fs_put op_remote_src = pyb.fs_cp
fmt = "cp %s :%s" op_local_src = pyb.fs_put
dest = fname_remote(dest)
else: else:
op = pyb.fs_get op_remote_src = pyb.fs_get
fmt = "cp :%s %s" op_local_src = lambda src, dest, **_: __import__("shutil").copy(src, dest)
for src in srcs: for src in srcs:
src = fname_remote(src)
dest2 = fname_cp_dest(src, dest)
if verbose: if verbose:
print(fmt % (src, dest2)) print("cp %s %s" % (src, dest))
op(src, dest2, progress_callback=progress_callback) if src.startswith(":"):
op = op_remote_src
else:
op = op_local_src
src2 = fname_remote(src)
dest2 = fname_cp_dest(src2, fname_remote(dest))
op(src2, dest2, progress_callback=progress_callback)
else: else:
op = { op = {
"cat": pyb.fs_cat, "cat": pyb.fs_cat,