tools: wasptool: Additional adoption of the run_command wrapper
run_command has particular benefits for handle_binary_download() because we can greatly simplify the code to handle running repr() on the target. Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
parent
326caa3fa5
commit
b734037115
1 changed files with 23 additions and 28 deletions
|
@ -126,7 +126,10 @@ def paste(c, f, verbose=False, chunk=None):
|
||||||
sys.exit(16)
|
sys.exit(16)
|
||||||
|
|
||||||
def print_log(logfile):
|
def print_log(logfile):
|
||||||
lines = logfile.getvalue().split('\n')
|
try:
|
||||||
|
lines = logfile.getvalue().split('\n')
|
||||||
|
except:
|
||||||
|
lines = logfile.split('\n')
|
||||||
lines = [ l.strip('\x04\r') for l in lines ]
|
lines = [ l.strip('\x04\r') for l in lines ]
|
||||||
|
|
||||||
output = [ l for l in lines if l and l != '>>> ' ]
|
output = [ l for l in lines if l and l != '>>> ' ]
|
||||||
|
@ -234,42 +237,35 @@ def check_rtc(c):
|
||||||
def handle_binary_download(c, tname, fname):
|
def handle_binary_download(c, tname, fname):
|
||||||
verbose = bool(c.logfile)
|
verbose = bool(c.logfile)
|
||||||
|
|
||||||
c.sendline('import os')
|
c.run_command('import os')
|
||||||
c.expect('>>>')
|
stat = c.run_command(f'os.stat("{tname}")[6]')
|
||||||
c.sendline(f'os.stat("{tname}")[6]')
|
if 'Error' in stat:
|
||||||
err = c.expect(['>>> ', 'Error'])
|
print('Watch reported error:')
|
||||||
if err:
|
print(stat)
|
||||||
print('Target error, cannot open file')
|
|
||||||
c.expect('>>>')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
lines = c.before.split('\n')
|
|
||||||
sz = eval(lines[1].rstrip())
|
|
||||||
bytes_read = 0
|
|
||||||
|
|
||||||
c.sendline(f'f = open("{tname}", "rb")')
|
|
||||||
c.expect('>>>')
|
|
||||||
|
|
||||||
print(f'Downloading {fname}:')
|
print(f'Downloading {fname}:')
|
||||||
|
|
||||||
|
c.run_command(f'f = open("{tname}", "rb")')
|
||||||
|
sz = eval(stat)
|
||||||
|
bytes_read = 0
|
||||||
|
|
||||||
with open(fname, 'wb') as f:
|
with open(fname, 'wb') as f:
|
||||||
while True:
|
while True:
|
||||||
draw_pbar(100 * bytes_read / sz, verbose)
|
draw_pbar(100 * bytes_read / sz, verbose)
|
||||||
c.sendline('repr(f.read(24))')
|
reply = c.run_command('repr(f.read(24))')
|
||||||
c.expect('>>>')
|
data = eval(reply.replace('\\\\', '\\').strip('"'))
|
||||||
lines = c.before.split('\n')
|
if len(data) == 0:
|
||||||
r = lines[1].rstrip().strip('"').replace('\\\\', '\\')
|
|
||||||
eval(f'f.write({r})')
|
|
||||||
bytes_read += 24
|
|
||||||
if len(r) <= 3:
|
|
||||||
break
|
break
|
||||||
|
bytes_read += len(data)
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
draw_pbar(100, verbose, end=None)
|
draw_pbar(100, verbose, end=None)
|
||||||
|
c.run_command('f.close()')
|
||||||
|
|
||||||
c.sendline('f.close()')
|
# Release as much memory as possible
|
||||||
c.expect('>>> ')
|
c.run_command('del f')
|
||||||
c.sendline('f = None')
|
c.run_command('del os')
|
||||||
c.expect('>>> ')
|
|
||||||
|
|
||||||
def handle_binary_upload(c, fname, tname):
|
def handle_binary_upload(c, fname, tname):
|
||||||
verbose = bool(c.logfile)
|
verbose = bool(c.logfile)
|
||||||
|
@ -307,8 +303,7 @@ def handle_upload(c, fname, tname):
|
||||||
if not tname:
|
if not tname:
|
||||||
tname = os.path.basename(fname)
|
tname = os.path.basename(fname)
|
||||||
|
|
||||||
c.sendline('from shell import upload')
|
c.run_command('from shell import upload')
|
||||||
c.expect('>>> ')
|
|
||||||
|
|
||||||
with open(fname) as f:
|
with open(fname) as f:
|
||||||
if not verbose:
|
if not verbose:
|
||||||
|
|
Loading…
Reference in a new issue