diff options
author | Damien George <damien.p.george@gmail.com> | 2019-12-11 14:56:33 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-12-19 17:00:52 +1100 |
commit | b3b9b11596b5b4c9a3c45185fd839fa3db63fa12 (patch) | |
tree | c1dd5e6ff4b8eb1a2902e107ecc2cd1c7e6e715c /tools/pyboard.py | |
parent | 43b576d88d3a9802bd38a30bb4266fed889db903 (diff) | |
download | micropython-b3b9b11596b5b4c9a3c45185fd839fa3db63fa12.tar.gz micropython-b3b9b11596b5b4c9a3c45185fd839fa3db63fa12.zip |
tools/pyboard.py: Support executing .mpy files directly.
This patch allows executing .mpy files (including native ones) directly on
a target, eg a board over a serial connection. So there's no need to copy
the file to its filesystem to test it.
For example:
$ mpy-cross foo.py
$ pyboard.py foo.mpy
Diffstat (limited to 'tools/pyboard.py')
-rwxr-xr-x | tools/pyboard.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tools/pyboard.py b/tools/pyboard.py index c32fb002ca..2a255e91c0 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -484,6 +484,33 @@ def filesystem_command(pyb, args): pyb.close() sys.exit(1) +_injected_import_hook_code = """\ +import uos, uio +class _FS: + class File(uio.IOBase): + def __init__(self): + self.off = 0 + def ioctl(self, request, arg): + return 0 + def readinto(self, buf): + buf[:] = memoryview(_injected_buf)[self.off:self.off + len(buf)] + self.off += len(buf) + return len(buf) + mount = umount = chdir = lambda *args: None + def stat(self, path): + if path == '_injected.mpy': + return tuple(0 for _ in range(10)) + else: + raise OSError(-2) # ENOENT + def open(self, path, mode): + return self.File() +uos.mount(_FS(), '/_') +uos.chdir('/_') +from _injected import * +uos.umount('/_') +del _injected_buf, _FS +""" + def main(): import argparse cmd_parser = argparse.ArgumentParser(description='Run scripts on the pyboard.') @@ -544,6 +571,9 @@ def main(): for filename in args.files: with open(filename, 'rb') as f: pyfile = f.read() + if filename.endswith('.mpy') and pyfile[0] == ord('M'): + pyb.exec_('_injected_buf=' + repr(pyfile)) + pyfile = _injected_import_hook_code execbuffer(pyfile) # exiting raw-REPL just drops to friendly-REPL mode |